Advertisement
CrispyPin

string blueprint api

Sep 21st, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.84 KB | None | 0 0
  1. -- Blueprint manager API
  2. -- by CrispyPin
  3. layer_sep = "--"
  4.  
  5. blueprint = {}
  6. keys = {}
  7.  
  8. function string:split(delimiter)
  9.     -- stolen from jaredallard
  10.     local result = {}
  11.     local from  = 1
  12.     local delim_from, delim_to = string.find(self, delimiter, from)
  13.     while delim_from do
  14.         table.insert(result, string.sub(self, from , delim_from-1))
  15.         from = delim_to + 1
  16.         delim_from, delim_to = string.find(self, delimiter, from)
  17.     end
  18.     table.insert(result, string.sub(self, from))
  19.     return result
  20. end
  21.  
  22. local function join(strA, strB)
  23.     return table.concat({strA, strB}, "")
  24. end
  25.  
  26. local function loadLayer(data)
  27.     bp = {}
  28.     row = {}
  29.     x = 1
  30.     y = 1
  31.     for i = 1, string.len(data) do
  32.         b = string.sub(data, i, i)
  33.         if b == "\n" then
  34.             bp[y] = row
  35.             row = {}
  36.             x = 1
  37.             y = y + 1
  38.         else
  39.             row[x] = b
  40.             x = x + 1
  41.         end
  42.     end
  43.     return bp
  44. end
  45.  
  46. function loadBlueprint(filePath)
  47.     f = fs.open(filePath, "r")
  48.     if f == nil then
  49.         return false
  50.     end
  51.     while true do
  52.         local data = f.readLine()
  53.         if data == "--" then
  54.             break
  55.         end
  56.         local k = string.sub(data, 1, 1)
  57.         local id = string.sub(data, 3)
  58.         keys[k] = id
  59.     end
  60.     rawBlueprint = f.readAll()
  61.     print(rawBlueprint)
  62.     f.close()
  63.     local layers = string.split(rawBlueprint, join(join("\n", layer_sep), "\n"))
  64.     for L = 1, table.maxn(layers) do
  65.         blueprint[L] = loadLayer(layers[L])
  66.     end
  67.     return true
  68. end
  69.  
  70. function getBlock(x, y, layer)
  71.     return keys[blueprint[layer][y][x]]
  72. end
  73.  
  74. function getFootprint()
  75.     -- returns width, length
  76.     return table.maxn(blueprint[1][1]), table.maxn(blueprint[1])
  77. end
  78.  
  79. function getLayers()
  80.     return table.maxn(blueprint)
  81. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement