Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.71 KB | None | 0 0
  1. function fill_region(map, x, y, n, v)
  2.    local i, j
  3.    for i = x, x + n do
  4.       for j = y, y + n do
  5.          map[i][j] = v
  6.       end
  7.    end
  8.    return map
  9. end
  10.  
  11. function check_map(map, size)
  12.    local x, y, t
  13.    t = 0
  14.    for x = 0, size do
  15.       for y = 0, size do
  16.          if map[x][y] == 1 then
  17.             t = t + 1
  18.          end
  19.       end
  20.    end
  21.    return t
  22. end
  23.  
  24. function create_map(map, size)
  25.    map = {}
  26.    for x = 0, size do
  27.       map[x] = {}
  28.       for y = 0, size do
  29.          map[x][y] = 1
  30.       end
  31.    end
  32.    return map
  33. end
  34.  
  35. function start_map(map, size)
  36.    map = create_map(map, size)
  37. --[[
  38.    local x, y  
  39.    for x = 0, size, size/4 do
  40.       for y = 0, size, size/4 do
  41.          if x < size and y < size then
  42.             map = fill_region(map, x, y, size/4, stead.rnd(2) - 1)
  43.          end
  44.       end
  45.    end
  46. ]]--
  47.    local i
  48.    local n = 8
  49.    for i = 0, size, size/n do
  50.       if i < size then
  51.          map = fill_region(map, i, 0, size/n, 0)
  52.          map = fill_region(map, i, size - size/n - 1, size/n, 0)
  53.          map = fill_region(map, 0, i, size/n, 0)
  54.          map = fill_region(map, size - size/n - 1, i, size/n, 0)
  55.       end
  56.    end
  57.    map = fill_region(map, size/n*3, size/n*3, size/n, 0)
  58.    return map
  59. end
  60.  
  61. function build_map(map, size)
  62.    local x, y, n, t
  63.    local i = 32
  64.    map = start_map(map, size)
  65.    n = size/i
  66.    while n > 0 do
  67.       for x = 0, size, n do
  68.          for y = 0, size, n do
  69.             local dx, dy = 0, 0
  70.             t = stead.rnd(2)
  71.             if t == 1 then
  72.                dx = math.floor((x + n) / (n * 2) * n * 2)
  73.             else
  74.                dx = math.floor((x - n) / (n * 2) * n * 2)
  75.             end
  76.             t = stead.rnd(2)
  77.             if t == 1 then
  78.                dy = math.floor((y + n) / (n * 2) * n * 2)
  79.             else
  80.                dy = math.floor((y - n) / (n * 2) * n * 2)
  81.             end
  82.             if dx >= size then dx = 0 end
  83.             if dx < 0 then dx = size - n end
  84.             if dy >= size then dy = 0 end
  85.             if dy < 0 then dy = size - n end
  86.             if x < size and y < size then
  87.                map = fill_region(map, x, y, n, map[dx][dy])
  88.             end
  89.          end
  90.       end
  91.       n = math.floor(n / 2)
  92.    end
  93.    return map
  94. end
  95.  
  96. function world_goodiser(map, size, i)
  97.    local nmap = {}
  98.    local n
  99.    nmap = create_map(nmap, size)
  100.    for n = 1, i do
  101.       for x = 0, size do
  102.          for y = 0, size do
  103.             local c = 0
  104.             local dx, dy
  105.             for dx = x - 3, x + 3 do
  106.                for dy = y - 3, y + 3 do
  107.                   if dx >= 0 and dy >= 0 and dx <= size and dy <= size then
  108.                      c = c + map[dx][dy]
  109.                      if c > 30 then
  110.                         nmap[x][y] = 1
  111.                      else
  112.                         nmap[x][y] = 0
  113.                      end
  114.                   end
  115.                end
  116.             end
  117.          end
  118.       end
  119.    end
  120.    return nmap
  121. end
  122.  
  123. function generate_world(map, size)
  124.    local t
  125.    t = 0
  126.    while t < size*size/2.1 or t > size*size/1.9 do
  127.       print(t)
  128.       map = build_map(map, size)
  129.       t = check_map(map, size)
  130.    end
  131.    map = world_goodiser(map, size, 3)
  132.    return map
  133. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement