KoBeWi

CellurarCaves

Apr 25th, 2017
89
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local CHANCE_TO_START_ALIVE = 500
  2. local BIRTH_LIMIT = 5
  3. local DEATH_LIMIT = 5
  4. local STEPS = 5
  5.  
  6. local distance = 16
  7. local pointSize = 2
  8.  
  9. function generateCave()
  10.   x = GetRandom(4096)
  11.   y = GetRandom(2048)
  12.  
  13.   local world = {}
  14.  
  15.   initializeWord(world)
  16.  
  17.   for i=1, STEPS do
  18.     world = simulationStep(world)
  19.   end
  20.  
  21.   for x=distance, 4096, distance do
  22.     for y=distance, 2048, distance do
  23.       if world[x / distance][y / distance] then
  24.         AddPoint(x, y, pointSize)
  25.       end
  26.     end
  27.   end
  28.  
  29.     FlushPoints()
  30. end
  31.  
  32. function initializeWord(world)
  33.   for x=1, 4096/distance do
  34.     table.insert(world, {})
  35.    
  36.     for y=1, 2048/distance do
  37.       if GetRandom(1000) < CHANCE_TO_START_ALIVE then
  38.         table.insert(world[x], true)
  39.       else
  40.         table.insert(world[x], false)
  41.       end
  42.     end
  43.   end
  44. end
  45.  
  46. function simulationStep(world)
  47.   world2 = {}
  48.  
  49.   for x=1, 4096/distance do
  50.     table.insert(world2, {})
  51.    
  52.     for y=1, 2048/distance do
  53.       local count = countNeigbours(world, x, y)
  54.      
  55.       if world[x][y] then
  56.         if count < DEATH_LIMIT then
  57.           table.insert(world2[x], false)
  58.         else
  59.           table.insert(world2[x], true)
  60.         end
  61.       else
  62.         if count > BIRTH_LIMIT then
  63.           table.insert(world2[x], true)
  64.         else
  65.           table.insert(world2[x], false)
  66.         end
  67.       end
  68.     end
  69.   end
  70.  
  71.   return world2
  72. end
  73.  
  74. function countNeigbours(world, x, y)
  75.   count = 0
  76.  
  77.   for dx=1, 3 do
  78.     for dy=1, 3 do
  79.       local x2 = x+dx-2
  80.       local y2 = y+dy-2
  81.      
  82.       if x2 == 0 or x2 == 4096/distance+1 or y2 == 0 or y2 == 2048/distance+1 or world[x2][y2] then
  83.         count = count + 1
  84.       end
  85.     end
  86.   end
  87.  
  88.   return count
  89. end
  90.  
  91. function onGameInit()
  92.     MapGen = mgDrawn
  93.     TemplateFilter = 0
  94.     generateCave()
  95. end
  96.  
  97. function onPreviewInit()
  98.     onGameInit()
  99. end
RAW Paste Data