t8rt0t

fern-renderer.lua

Jul 3rd, 2024 (edited)
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.64 KB | None | 0 0
  1. --constants
  2. local datlib = require("datlib")
  3. local colorMap = require("colorMap")
  4. local gpu = peripheral.find("tm_gpu")
  5. if not gpu then
  6.     local tName = "monitor_3"
  7.     local bName = "monitor_4"
  8.     local redirect = "monitor_5"
  9.     if not peripheral.wrap(tName) or not peripheral.wrap(bName) then error("Please make sure you have an attached gpu or two attached monitors") end
  10.     tm = peripheral.wrap(tName)
  11.     bm = peripheral.wrap(bName)
  12.     if peripheral.wrap(redirect) then
  13.         local oldTerm = term.current()
  14.         term.redirect(peripheral.wrap(redirect))
  15.     end
  16. end
  17. local inverseColors = {}
  18. for k,v in pairs(colorMap) do
  19.     inverseColors[v] = k
  20. end
  21.  
  22.  
  23. local maxX,maxY
  24. local currentDat,currentKey,currentRegion,currentBiome,cache
  25.  
  26. if gpu then
  27.     gpu.setSize(64)
  28.     gpu.refreshSize()
  29.     gpu.sync()
  30.     gpu.clear()
  31. else
  32.     tm.setTextScale(0.5)
  33.     tm.setBackgroundColor(colors.black)
  34.     tm.clear()
  35.     bm.setTextScale(0.5)
  36.     bm.setBackgroundColor(colors.black)
  37.     bm.clear()
  38.     maxX,maxY = tm.getSize()
  39. end
  40.  
  41.  
  42.    
  43.  
  44. local loaded_dats,loaded_biomes = {},{}
  45.  
  46.  
  47. local function drawBiome(x,y,biome)
  48.     if gpu then
  49.         return gpu.filledRectangle(x,y,1,1,colorMap[biome] or 0xFFFFFFFF)
  50.     else
  51.         x,y = x+1,y+1
  52.         if y <= maxY then
  53.             -- print("tm",x,y,biome)
  54.             tm.setCursorPos(x,y)
  55.             tm.setBackgroundColor(colorMap.rgbaToColor(colorMap[biome] or 0xFFFFFFFF))
  56.             tm.write(" ")
  57.         else
  58.             -- print("bm",x,y-maxY,biome,y)
  59.             bm.setCursorPos(x,y-maxY)
  60.             bm.setBackgroundColor(colorMap.rgbaToColor(colorMap[biome] or 0xFFFFFFFF))
  61.             bm.write(" ")
  62.         end
  63.     end
  64. end
  65.  
  66. --Region path looks like disk82/regions/0_0.dat
  67. --Its corresponding biome path would look like disk82/metadata/biomes.json
  68. local function getBiomeMap(regionPath)
  69.     local file = fs.open(regionPath:match("^%w+").."/metadata/biomes.json","r")
  70.     local map = textutils.unserialiseJSON(file and file.readAll() or "{}")
  71.     if file then file.close() end
  72.     return map
  73. end
  74.  
  75. local regions = fs.find("*/regions/*.dat")
  76. local loaded_regions = {}
  77.  
  78. for k,v in pairs(regions) do
  79.     local key = v:match("/(%-?%d+_%-?%d+)%.dat")
  80.     loaded_dats[key] = datlib:open(v)
  81.     loaded_biomes[key] = getBiomeMap(v)
  82.     table.insert(loaded_regions,key)
  83. end
  84.  
  85. local function renderRegion(regionKey)
  86.     local rX,rZ = regionKey:match("(%-?%d+)_(%-?%d+)")
  87.     if not tonumber(rX) or not tonumber(rZ) then return false end
  88.     local x,z = tonumber(rX)*128,tonumber(rZ)*128
  89.     local biomeMap = loaded_biomes[regionKey]
  90.     local dat = loaded_dats[regionKey]
  91.     for i=0,127 do
  92.         for j=0,127 do
  93.             local biome = dat:getBiome(i,j,biomeMap)
  94.             if biome then
  95.                 if gpu then
  96.                     drawBiome(x+i,z+j,biome)
  97.                 else
  98.                     drawBiome(i,j,biome)
  99.                     cache[string.format("%d_%d",i,j)] = biome
  100.                 end
  101.             end
  102.         end
  103.     end
  104.     --dat:close()
  105.     return true
  106. end
  107.  
  108. local function refresh()
  109.     gpu.clear()
  110.     gpu.refreshSize()
  111.     for k,v in pairs(loaded_regions) do
  112.         renderRegion(v)
  113.         sleep(0)
  114.     end
  115.     gpu.sync()
  116. end
  117.  
  118. local function gpuClickWatcher()
  119.     while true do
  120.         local _,x,y,sneaking = os.pullEvent("tm_monitor_touch")
  121.         local rX,rZ = math.floor(x/128),math.floor(y/128)
  122.         local key = string.format("%d_%d.dat",rX,rZ)
  123.         local dat = loaded_dats[key]
  124.         local biome = dat:getBiome(x%128,y%128,loaded_biomes[key])
  125.         term.setTextColor(colorMap.rgbaToColor(colorMap[biome]))
  126.         print(biome)
  127.     end
  128. end
  129.  
  130. local function monitorClickWatcher(cache)
  131.     -- print(regionKey)
  132.     -- local dat = loaded_dats[regionKey]
  133.     -- local biomeMap = loaded_biomes[regionKey]
  134.     while true do
  135.         _,mon,x,y = os.pullEvent("monitor_touch")
  136.         x,y = math.min(x-1,127),math.min(y-1,127)
  137.         cX,cY = currentRegion[1]*128 + x, currentRegion[2]*128 + y
  138.         local key = string.format("%d_%d",x,y)
  139.         if mon == tName then
  140.             -- local biome = dat:getBiome(x,y,biomeMap)
  141.             local biome = cache[key]
  142.             term.setTextColor(colorMap.rgbaToColor(colorMap[biome] or 0xFFFFFFFF))
  143.             print(cX*16,cY*16,biome)
  144.         else
  145.             -- local biome = dat:getBiome(x,math.min(y+maxY,127),biomeMap)
  146.             local biome = cache[key]
  147.             term.setTextColor(colorMap.rgbaToColor(colorMap[biome] or 0xFFFFFFFF))
  148.             print(cX*16,cY*16,biome)
  149.         end
  150.     end
  151. end
  152.  
  153. local function refreshLoop()
  154.     while true do
  155.         refresh()
  156.         sleep(60)
  157.     end
  158. end
  159. local args = {...}
  160. local s,err = pcall(function()
  161.         if gpu then
  162.             parallel.waitForAny(gpuClickWatcher,refreshLoop)
  163.         else
  164.             if tonumber(args[1]) and tonumber(args[2]) and loaded_dats[string.format("%d_%d",tonumber(args[1]),tonumber(args[2]))] then
  165.                 currentKey = string.format("%d_%d",tonumber(args[1]),tonumber(args[2]))
  166.                 cache = {}
  167.                 renderRegion(currentKey)
  168.                 currentDat = loaded_dats[currentKey]
  169.                 currentRegion = {tonumber(args[1]),tonumber(args[2])}
  170.                 currentBiome = loaded_biomes[currentKey]
  171.                 monitorClickWatcher(cache)
  172.             else
  173.                 error("Please provide a region to scan in the format `x (number)` `z (number)`")
  174.             end
  175.         end
  176. end)
  177. if not s then
  178.     for k,v in pairs(loaded_dats) do
  179.         pcall(function() v:close() end)
  180.     end
  181.     if oldTerm then term.redirect(oldTerm) end
  182.     error(err)
  183. end
Advertisement
Add Comment
Please, Sign In to add comment