Advertisement
Kitomas

playt1 prototype

Oct 25th, 2024
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.31 KB | None | 0 0
  1. local byte = string.byte --chr, index
  2. local char = string.char
  3. local concat = table.concat --table, separator, [i], [j]
  4. local min = math.min
  5. local unpack = table.unpack
  6.  
  7. local mon = peripheral.find("monitor")
  8. assert(mon, "no monitor found")
  9.  
  10.  
  11.  
  12.  
  13.  
  14. local function resolveInvLine(s)
  15.   local chars = {}
  16.   for i=1, #s do
  17.     chars[#chars+1] = char(319-byte(s,i))
  18.   end
  19.   return concat(chars,"")
  20. end
  21.  
  22.  
  23.  
  24.  
  25.  
  26. local function writeWrap(monitor, str, col, row, w)
  27.   local f = 1
  28.   local len = str:len()
  29.  
  30.   local diff = w-col+1
  31.   local len2 = min(diff,len)
  32.   local line = str:sub(f,f+len2-1)
  33.   monitor.write(line)
  34.  
  35.   f=f+len2
  36.   col=col+len2
  37.  
  38.  
  39.   if len-len2 ~= 0 then
  40.     for i=f, len, w do
  41.       col=1
  42.       row=row+1
  43.       monitor.setCursorPos(1,row)
  44.  
  45.       line = str:sub(i,i+w-1)
  46.       monitor.write(line)
  47.       col=col+line:len()
  48.  
  49.     end
  50.  
  51.     if col > w then
  52.       col=1
  53.       row=row+1
  54.       monitor.setCursorPos(1,row)
  55.     end
  56.  
  57.   end
  58.  
  59.  
  60.   return col, row
  61.  
  62. end
  63.  
  64.  
  65.  
  66.  
  67.  
  68. local function drawFrame(monitor, inputFile, _w, _h)
  69.   if monitor.setTextScale then
  70.     monitor.setTextScale(0.5)
  71.   end
  72.  
  73.   local w, h  =  monitor.getSize()
  74.   if _w then  w = _w  end
  75.   if _h then  h = _h  end
  76.  
  77.   local str = inputFile:read(w*h)
  78.   if not str then return false end
  79.  
  80.  
  81.  
  82.   local length          = str:len()
  83.   local inverted        = byte(str,1) >= 160
  84.   local firstIsInverted = inverted
  85.  
  86.   local   i, j  =  0, 1
  87.   local chunks  =  {}
  88.  
  89.   while i <= length do
  90.     while not inverted do
  91.       i=i+1
  92.       if i > length then break end
  93.       if byte(str,i) >= 160 then
  94.         chunks[#chunks+1] = str:sub(j,i-1)
  95.         j=i
  96.         inverted = true
  97.       end
  98.     end
  99.  
  100.     while inverted do
  101.       i=i+1
  102.       if i > length then break end
  103.       if byte(str,i) < 160 then
  104.         chunks[#chunks+1] = resolveInvLine(str:sub(j,i-1))
  105.         j=i
  106.         inverted = false
  107.       end
  108.     end
  109.  
  110.   end
  111.  
  112.  
  113.   if i ~= j then
  114.     if not inverted then
  115.       chunks[#chunks+1] = str:sub(j,i-1)
  116.     else
  117.       chunks[#chunks+1] = resolveInvLine(str:sub(j,i-1))
  118.     end
  119.   end
  120.  
  121.  
  122.  
  123.   monitor.setBackgroundColor(colors.black)
  124.   monitor.setTextColor(colors.white)
  125.   --causes bottom-side screen artifacts
  126.    --in the event of render lag
  127.   --monitor.clear()
  128.   inverted = false
  129.  
  130.   if firstIsInverted then
  131.     monitor.setBackgroundColor(colors.white)
  132.     monitor.setTextColor(colors.black)
  133.     inverted = true
  134.   end
  135.  
  136.  
  137.  
  138.   local col, row  =  1, 1
  139.   monitor.setCursorPos(1,1)
  140.  
  141.  
  142.   for c=1, #chunks do
  143.     col, row = writeWrap(monitor, chunks[c], col, row, w)
  144.  
  145.     inverted = not inverted
  146.     if not inverted then
  147.       monitor.setBackgroundColor(colors.black)
  148.       monitor.setTextColor(colors.white)
  149.     else
  150.       monitor.setBackgroundColor(colors.white)
  151.       monitor.setTextColor(colors.black)
  152.     end
  153.  
  154.   end
  155.  
  156.  
  157.  
  158.   return true
  159.  
  160. end
  161.  
  162.  
  163.  
  164.  
  165.  
  166. local argv = {...}
  167. if not argv[1] then argv[1] = "konosuba_20.kv3" end
  168.  
  169. local file = io.open(argv[1], "rb")
  170. assert(file, "failed to open file")
  171.  
  172.  
  173. local nolag = true
  174.  
  175. while true do
  176.   local timeStart = os.time()
  177.  
  178.   if not drawFrame(mon, file) then break end
  179.  
  180.   local timeDiff = os.time()-timeStart
  181.   print(nolag, timeDiff)
  182.  
  183.   if timeDiff < 0.000001 then
  184.     os.sleep(0.05) --1 tick
  185.   else
  186.     nolag = false
  187.   end
  188.  
  189. end
  190.  
  191.  
  192. file:close()
  193.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement