Advertisement
Hachem16

Pattern Based Map Generator

Oct 30th, 2015
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.85 KB | None | 0 0
  1. local blockwidth        = 25
  2. local blockheight       = 15
  3. local map_width         = math.floor(  800  /blockwidth)
  4. local map_height        = math.floor(  400  /blockheight)
  5.  
  6. pattrns = {
  7.     [1] = {
  8.             {0,0,0,0,0},
  9.             {0,1,1,1,0},
  10.             {0,1,0,1,0},
  11.             {0,1,0,1,0},
  12.             {0,1,1,1,0},
  13.             {0,0,0,0,0},
  14.     },
  15.     [2] = {
  16.             {0,0,0,0,0},
  17.             {0,1,1,0,0},
  18.             {0,0,1,0,0},
  19.             {0,0,1,0,0},
  20.             {0,1,1,1,0},
  21.             {0,0,0,0,0},
  22.     },
  23.     [3] = {
  24.             {0,0,0,0,0},
  25.             {0,1,1,1,0},
  26.             {0,0,0,1,0},
  27.             {0,1,1,1,0},
  28.             {0,1,0,0,0},
  29.             {0,1,1,1,0},
  30.             {0,0,0,0,0},
  31.     },
  32. }
  33.  
  34. wood = {
  35.     type            = 0,
  36.     width           = 40,
  37.     height          = 40,
  38.     foreground      = false,
  39.     friction        = 0.3,
  40.     restitution     = 0.2,
  41.     angle           = 0,
  42.     miceCollision       = true,
  43.     groundCollision     = true,
  44.     dynamic         = false,
  45.     fixedRotation       = false,
  46.     mass            = 0,
  47.     linearDamping       = 0,
  48.     angularDamping      = 0,
  49. }
  50.  
  51. xml             = [[<C><P /><Z><S><S L="840" o="324aa0" H="28" X="409" Y="413" T="12" P="0,0,0.3,0.2,0,0,0,0" /></S><D /><O /></Z></C>]]
  52.  
  53. function new_map( width, height, patterns, seed)
  54.     local width     = width
  55.     local height    = height
  56.     local diverse   = diverse
  57.     local pnum      = #patterns
  58.  
  59.     local seed  = seed or tonumber(math.random(0,99999)..math.random(0,99999))
  60.     local tiles = {}
  61.     local used  = {}
  62.     math.randomseed(seed)
  63.  
  64.     local function pattern(pattern, x, y)
  65.         for Y,v1 in ipairs(pattern) do
  66.             for X,v in ipairs(v1) do
  67.                 if X+x-1>height then
  68.                     return false
  69.                 elseif Y+y-1>width then
  70.                     return false
  71.                 end
  72.             end
  73.         end
  74.  
  75.         for Y,v1 in ipairs(pattern) do
  76.             for X,v in ipairs(v1) do
  77.  
  78.  
  79.                 if type(tiles[X+x]) == "table" then
  80.                         tiles[X+x-1][Y+y-1] = v
  81.                         used[X+x-1][Y+y-1]  = true
  82.                 end
  83.  
  84.  
  85.             end
  86.         end
  87.     end
  88.  
  89.     for x = 0, height do
  90.         tiles[x] = {}
  91.         used[x] = {}
  92.         for y = 0, width do
  93.             used[x][y] = false
  94.         end
  95.     end
  96.  
  97.     for y = 0, width do
  98.         for x = 0, height do
  99.             if not used[x][y] then
  100.                 local pat = math.random(1,pnum)
  101.                 pattern(patterns[pat], x, y)
  102.             end
  103.         end
  104.     end
  105.  
  106.     math.randomseed(tonumber(string.reverse(seed)))
  107.     return tiles
  108. end
  109.  
  110. function load_map(tiles, x, y, w, h, prot)
  111.     local prot  = prot or wood
  112.     local x     = x or 0
  113.     local y     = y or 0
  114.     local tiles = type(tiles)=="table" and tiles or error("table expected at #1, got "..type(tiles))
  115.     local w     = w or 40
  116.     local h     = h or 40
  117.  
  118.  
  119.     prot.width  = w
  120.     prot.height = h
  121.  
  122.  
  123.     id = 1
  124.  
  125.  
  126.     for x,gab in pairs(tiles) do
  127.         for y,val in pairs(gab) do
  128.             if val ~= 0 then
  129.                 local w2 = w/2
  130.                 local h2 = h/2
  131.                 local cx = x*w + w2
  132.                 local cy = y*h + h2
  133.  
  134.                 tfm.exec.addPhysicObject(id , cx, cy, prot)
  135.                 id = id + 1
  136.             end
  137.         end
  138.  
  139.         if (x+1)%6 == 0 then
  140.             coroutine.yield(x, y)
  141.         end
  142.  
  143.     end
  144.  
  145. end
  146.  
  147.  
  148. tfm.exec.newGame(xml)
  149.  
  150.  
  151. local map = new_map(map_height, map_width, pattrns)
  152.  
  153. co = coroutine.create(load_map)
  154.  
  155. function eventLoop(np, lp)
  156.     coroutine.resume(co, map, 0, 0, blockwidth, blockheight, wood)
  157. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement