Advertisement
blunty666

map

Jul 1st, 2014
457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.53 KB | None | 0 0
  1. local maps = {}
  2.  
  3. local function toGridCode(tVector)
  4.     return math.floor(tVector.x/16), math.floor(tVector.y/16), math.floor(tVector.z/16), tVector.x % 16, tVector.y % 16, tVector.z % 16
  5. end
  6.  
  7. local function setGrid(tMap, x, y, z, grid)
  8.     if not tMap.map[x] then
  9.         tMap.map[x] = {}
  10.     end
  11.     if not tMap.map[x][y] then
  12.         tMap.map[x][y] = {}
  13.     end
  14.     tMap.map[x][y][z] = grid
  15.     return tMap.map[x][y][z]
  16. end
  17.  
  18. local function getGrid(tMap, x, y, z)
  19.     if not tMap.map[x] or not tMap.map[x][y] or not tMap.map[x][y][z] then
  20.         return tMap:load(x, y, z)
  21.     else
  22.         return tMap.map[x][y][z]
  23.     end
  24. end
  25.  
  26. local methods = {
  27.  
  28.     load = function(self, tVector, y, z)
  29.         local gX, gY, gZ
  30.         if y and z then
  31.             gX, gY, gZ = tVector, y, z
  32.         else
  33.             gX, gY, gZ = toGridCode(tVector)
  34.         end
  35.         if self.name then
  36.             if fs.exists(".maps/"..self.name.."/"..gX..","..gY..","..gZ) then
  37.                 local handle = fs.open(".maps/"..self.name.."/"..gX..","..gY..","..gZ, "r")
  38.                 if handle then
  39.                     local grid = handle.readAll()
  40.                     handle.close()
  41.                     for i = 15, 0, -1 do
  42.                         grid = string.gsub(grid, tostring(i).."=", "%["..tostring(i).."%]=")
  43.                     end
  44.                     grid = textutils.unserialize(grid)
  45.                     if type(grid) == "table" then
  46.                         return setGrid(self, gX, gY, gZ, grid)
  47.                     end
  48.                 end
  49.             end
  50.         end
  51.         return setGrid(self, gX, gY, gZ, {})
  52.     end,
  53.  
  54.     loadAll = function(self)
  55.         if self.name and fs.exists(".maps/"..self.name) and fs.isDir(".maps/"..self.name) then
  56.             for _, gridFile in ipairs(fs.list(".maps/"..self.name)) do
  57.                 local _, _, gX, gY, gZ = string.find(gridFile, "(.+)%,(.+)%,(.+)")
  58.                 if gX and gY and gX then
  59.                     self:load(tonumber(gX), tonumber(gY), tonumber(gZ))
  60.                 end
  61.             end
  62.         end
  63.     end,
  64.  
  65.     save = function(self)
  66.         if self.name then
  67.             local saveDir = ".maps/"..self.name
  68.             for x, YZmap in pairs(self.map) do
  69.                 for y, Zmap in pairs(YZmap) do
  70.                     for z, grid in pairs(Zmap) do
  71.                         if next(grid) then
  72.                             local handle = fs.open(fs.combine(saveDir, x..","..y..","..z), "w")
  73.                             local data = textutils.serialize(grid)
  74.                             data = string.gsub(data, " ", "")
  75.                             for i = 0, 15 do
  76.                                 data, num = string.gsub(data, "%["..tostring(i).."%]=", tostring(i).."=")
  77.                             end
  78.                             handle.write(data)
  79.                             handle.close()
  80.                         end
  81.                     end
  82.                 end
  83.             end
  84.         end
  85.     end,
  86.  
  87.     get = function(self, tVector)
  88.         local gX, gY, gZ, pX, pY, pZ = toGridCode(tVector)
  89.         local grid = getGrid(self, gX, gY, gZ)
  90.         if grid[pX] and grid[pX][pY] then
  91.             return grid[pX][pY][pZ]
  92.         end
  93.     end,
  94.  
  95.     set = function(self, tVector, value)
  96.         local gX, gY, gZ, pX, pY, pZ = toGridCode(tVector)
  97.         local grid = getGrid(self, gX, gY, gZ)
  98.         if not grid[pX] then
  99.             grid[pX] = {}
  100.         end
  101.         if not grid[pX][pY] then
  102.             grid[pX][pY] = {}
  103.         end
  104.         grid[pX][pY][pZ] = value
  105.         return grid[pX][pY][pZ]
  106.     end,
  107.  
  108.     getOrSet = function(self, tVector, value)
  109.         local gX, gY, gZ, pX, pY, pZ = toGridCode(tVector)
  110.         local grid = getGrid(self, gX, gY, gZ)
  111.         if grid[pX] and grid[pX][pY] and grid[pX][pY][pZ] then
  112.             return grid[pX][pY][pZ], false
  113.         else
  114.             if not grid[pX] then
  115.                 grid[pX] = {}
  116.             end
  117.             if not grid[pX][pY] then
  118.                 grid[pX][pY] = {}
  119.             end
  120.             grid[pX][pY][pZ] = value
  121.             return grid[pX][pY][pZ], true
  122.         end
  123.     end,
  124.  
  125. }
  126.  
  127. function new(name)
  128.     local tMap = {}
  129.     if name and type(name) == "string" then
  130.         if maps[name] then
  131.             return maps[name]
  132.         end
  133.         tMap.name = name
  134.         if not fs.exists(".maps/"..name) then
  135.             fs.makeDir(".maps/"..name)
  136.         end
  137.         maps[name] = tMap
  138.     end
  139.     tMap.map = {}
  140.     setmetatable(tMap, {__index = methods})
  141.     return tMap
  142. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement