t8rt0t

fern-data.lua

May 18th, 2024
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.90 KB | None | 0 0
  1. BIOME_METADATA_PATH = "biomes.json"
  2. REGION_METADATA_PATH = "region_assignment.json"
  3. --Turtles store their own biome numbers so they are less reliant on a central computer
  4. local function biomeToNum(biome)
  5.     if type(biome) ~= "string" then error("Biome id must be a string") end
  6.     local data = {}
  7.     if fs.exists(BIOME_METADATA_PATH) then --Read biome data
  8.         local file = fs.open(BIOME_METADATA_PATH,"r")
  9.         data = textutils.unserialiseJSON(file.readAll()) or {}
  10.         file.close()
  11.     end
  12.     if data[biome] then return data[biome] end
  13.     local next_num = 1
  14.     for k,v in pairs(data) do
  15.         if v >= next_num then next_num = v+1 end --New biome number will always be greater than previous biome numbers
  16.     end
  17.     data[biome] = next_num
  18.     local file = fs.open(BIOME_METADATA_PATH,"w+") --Open in rewrite mode
  19.     file.write(textutils.serialiseJSON(data))
  20.     file.close()
  21.     return next_num
  22. end
  23. --Gets the biome
  24. local function numToBiome(num)
  25.     num = tonumber(num) or error("Must provide a number for the biome")
  26.     if not fs.exists(BIOME_METADATA_PATH) then error("No biome metadata to read") end
  27.     local file = fs.open(BIOME_METADATA_PATH,"r")
  28.     local data = textutils.unserialiseJSON(file.readAll()) or {}
  29.     file.close()
  30.     for k,v in pairs(data) do
  31.         if v == num then return k end
  32.     end
  33.     error("No biome found")
  34. end
  35. --Dat files contain bytes
  36. --cX,cZ: number 0-127 telling the chunkX and chunkZ position within the region of the .dat
  37. --Bytes in .dat file:
  38. --0 = no chunk data at position
  39. --1-254 = literal numbers 1-254 (used with the biomes.json metadata path)
  40. --255 = do 255 + the next byte (recursively if the next byte is also 255)
  41. local function readDat(path,cX,cZ)
  42.     if type(path) ~= "string" then error("No path provided") end
  43.     if not fs.exists(path) then
  44.         local file = fs.open(path,"w+")
  45.         file.write(string.rep(string.char(0),16384))
  46.         file.close()
  47.         return 0
  48.     end
  49.     if tonumber(cX) and tonumber(cZ) then
  50.         local file = fs.open(path,"rb")
  51.         local index = (tonumber(cX)*128) + tonumber(cZ)
  52.         local str = file.read(index) --If there are no 255s, this is all the data until the data we want to read
  53.         str,matches = str:gsub(string.char(255),"")
  54.         while matches > 0 do --eating up extra bytes to account for the extra bytes added by each 255
  55.             if file.read() ~= 255 then matches = matches - 1 end --If 255, keep going
  56.         end
  57.         local num,char = 0,0
  58.         repeat --read until not hitting 255
  59.             char = file.read()
  60.             num = num + char
  61.         until char ~= 255
  62.         file.close()
  63.         return num
  64.     end
  65.     --If not specific chunk, then return the entire string
  66.     local file = fs.open(path,"rb")
  67.     local data = file.readAll()
  68.     file.close()
  69.     return data
  70. end
  71.  
  72. local function writeDat(path,cX,cZ,biomeOrNum)
  73.     if type(path) ~= "string" then error("No path provided") end
  74.     if not tonumber(cX) or not tonumber(cZ) then error("No chunk coordinates provided") end
  75.     if not fs.exists(path) then --Filling out default file
  76.         local file = fs.open(path,"w+")
  77.         file.write(string.rep(string.char(0),16384))
  78.         file.close()
  79.     end
  80.     if type(biomeOrNum) == "string" then biomeOrNum = biomeToNum(biomeOrNum) --If string, tries to convert into number, registering the biome if necessary
  81.     elseif type(biomeOrNum) ~= "number" or not numToBiome(biomeOrNum) then error("Invalid biome provided") end --If not a number or not a valid biome, errors
  82.     local file = fs.open(path,"rb")
  83.     local index = (tonumber(cX)*128) + tonumber(cZ)
  84.     local str = file.read(index) --If there are no 255s, this is all the data until the data we want to read
  85.     _,matches = str:gsub(string.char(255),"")
  86.     while matches > 0 do --eating up extra bytes to account for the extra bytes added by each 255
  87.         local char = file.read()
  88.         str = str .. char
  89.         if char ~= 255 then matches = matches - 1 end --If 255, keep going
  90.     end
  91.     --Adding the new biome num
  92.     while biomeOrNum >= 255 do
  93.         str = str .. string.char(255)
  94.         biomeOrNum = biomeOrNum - 255
  95.     end
  96.     str = str .. string.char(biomeOrNum)
  97.     --clearing out the original dat from the file buffer
  98.     local char
  99.     repeat
  100.         char = file.read()
  101.     until char ~= 255
  102.     --Adding the rest of the .dat to the file
  103.     str = str .. file.readAll()
  104.     file.close()
  105.     local file = fs.open(path,"w+")
  106.     file.write(str)
  107.     file.close()
  108. end
  109.  
  110. --Drew all this out, hopefully this is the good formula
  111. local function getNextRegion(tX,tZ)
  112.     if tX+tZ < 0 then
  113.         if tX - tZ > 0 then return tX - 1,tZ
  114.         else return tX, tZ + 1 end
  115.     else
  116.         if tX - tZ < 0 then return tX + 1, tZ
  117.         else return tX, tZ - 1 end
  118.     end
  119. end
  120.  
  121. --turtleID = id of the computer setting the region
  122. --region = the {x,z} data values of the region.
  123. local function setRegionAssignment(turtleID,region)
  124.     if type(region) ~= "table" or not string.match(textutils.serialiseJSON(region) or "","^%[%-?%d+,%-?%d+%]$") or not tonumber(turtleID) then return false end
  125.     local region_data = {}
  126.     if fs.exists(REGION_METADATA_PATH) then
  127.         local file = fs.open(REGION_METADATA_PATH,"r")
  128.         region_data = textutils.unserialiseJSON(file.readAll()) or {}
  129.         file.close()
  130.     end
  131.     region_data[textutils.serialiseJSON(region)] = tonumber(turtleID)
  132.     local file = fs.open(REGION_METADATA_PATH,"w+")
  133.     file.write(textutils.serialiseJSON(region_data))
  134.     file.close()
  135.     return true
  136. end
  137. --Gets the region assigned to a specific turtle
  138. --If no turtleID provided, returns the entire region assignment data as a table
  139. --If turtleID provided as a region (json string or table) then it tries to return the turtleID for that region
  140. --Otherwise, returns the region corresponding to the turtle id
  141. local function getRegionAssignment(turtleID)
  142.     local region_data = {}
  143.     if fs.exists(REGION_METADATA_PATH) then
  144.         local file = fs.open(REGION_METADATA_PATH,"r")
  145.         region_data = textutils.unserialiseJSON(file.readAll()) or {}
  146.         file.close()
  147.     end
  148.     if not turtleID then return region_data
  149.     elseif type(turtleID) == "table" or type(turtleID) == "string" then
  150.         if region_data[turtleID] then return region_data[turtleID] end
  151.         return
  152.     end
  153.     if not tonumber(turtleID) then error("Invalid arguments") end
  154.     local tX,tZ = 0,0
  155.     while true do
  156.         local key = string.format("[%d,%d]",tX,tZ)
  157.         if region_data[key] == turtleID then return {tX,tZ}
  158.         elseif not region_data[key] then
  159.             setRegionAssignment(turtleID,{tX,tZ})
  160.             return {tX,tZ}
  161.         end
  162.         tX,tZ = getNextRegion(tX,tZ)
  163.     end
  164. end
  165.  
  166. return {BIOME_METADATA_PATH=BIOME_METADATA_PATH, biomeToNum=biomeToNum, numToBiome=numToBiome, readDat=readDat, writeDat=writeDat, getRegionAssignment=getRegionAssignment}
Advertisement
Add Comment
Please, Sign In to add comment