SHARE
TWEET

CellurarCaves (better)

KoBeWi Apr 26th, 2017 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local CHANCE_TO_START_ALIVE = 450
  2. local BIRTH_LIMIT = 5
  3. local DEATH_LIMIT = 5
  4. local STEPS = 6
  5.  
  6. local distance = 40
  7. local pointSize = 16
  8.  
  9. function generateCave()
  10.     if MapFeatureSize then distance = MapFeatureSize*2 end
  11.  
  12.   x = GetRandom(4096)
  13.   y = GetRandom(2048)
  14.  
  15.   local world = {}
  16.  
  17.   initializeWord(world)
  18.  
  19.   for i=1, STEPS do
  20.     world = simulationStep(world)
  21.   end
  22.  
  23.   local x = 1
  24.   local y = 1
  25.  
  26.   while x <= 4096/distance do
  27.     while y <= 2048/distance do
  28.       if world[x][y] then
  29.         AddPoint((x-1) * distance + distance/2, (y-1) * distance + distance/2, pointSize)
  30.       end
  31.      
  32.       y = y+1
  33.     end
  34.    
  35.     x = x+1
  36.     y = 1
  37.   end
  38.  
  39.   local distance2 = distance*2
  40.  
  41.   x = 1
  42.   y = 1
  43.  
  44.   while x <= 4096/distance2 do
  45.     while y <= 2048/distance2 do
  46.       local average = 0
  47.       if world[x*2][y*2] then average = average+1 end
  48.       if world[x*2+1][y*2] then average = average+1 end
  49.       if world[x*2+1][y*2+1] then average = average+1 end
  50.       if world[x*2+1][y*2] then average = average+1 end
  51.    
  52.       if average >= 2 then
  53.         AddPoint((x*2-2) * distance + distance/2, (y*2-1) * distance + distance/2, pointSize/2)
  54.         AddPoint((x*2-2) * distance + distance/2, (y*2-2) * distance + distance/2, pointSize/2)
  55.         AddPoint((x*2-1) * distance + distance/2, (y*2-2) * distance + distance/2, pointSize/2)
  56.         AddPoint((x*2-1) * distance + distance/2, (y*2-1) * distance + distance/2, pointSize/2)
  57.       end
  58.      
  59.       y = y+1
  60.     end
  61.    
  62.     x = x+1
  63.     y = 1
  64.   end
  65.  
  66.     FlushPoints()
  67. end
  68.  
  69. function initializeWord(world)
  70.   local x = 1
  71.   local y = 1
  72.  
  73.   while x <= 4096/distance do
  74.     table.insert(world, {})
  75.    
  76.     while y <= 2048/distance do
  77.       if GetRandom(1000) < CHANCE_TO_START_ALIVE then
  78.         table.insert(world[x], true)
  79.       else
  80.         table.insert(world[x], false)
  81.       end
  82.      
  83.       y = y+1
  84.     end
  85.    
  86.     x = x+1
  87.     y = 1
  88.   end
  89. end
  90.  
  91. function simulationStep(world)
  92.   local x = 1
  93.   local y = 1
  94.   world2 = {}
  95.  
  96.   while x <= 4096/distance do
  97.     table.insert(world2, {})
  98.    
  99.     while y <= 2048/distance do
  100.       local count = countNeigbours(world, x, y)
  101.      
  102.       if world[x][y] then
  103.         if count < DEATH_LIMIT then
  104.           table.insert(world2[x], false)
  105.         else
  106.           table.insert(world2[x], true)
  107.         end
  108.       else
  109.         if count > BIRTH_LIMIT then
  110.           table.insert(world2[x], true)
  111.         else
  112.           table.insert(world2[x], false)
  113.         end
  114.       end
  115.      
  116.       y = y+1
  117.     end
  118.    
  119.     x = x+1
  120.     y = 1
  121.   end
  122.  
  123.   return world2
  124. end
  125.  
  126. function countNeigbours(world, x, y)
  127.   count = 0
  128.  
  129.   for dx=1, 3 do
  130.     for dy=1, 3 do
  131.       local x2 = x+dx-2
  132.       local y2 = y+dy-2
  133.      
  134.       if x2 == 0 or x2 > 4096/distance or y2 == 0 or y2 > 2048/distance or world[x2][y2] then
  135.         count = count + 1
  136.       end
  137.     end
  138.   end
  139.  
  140.   return count
  141. end
  142.  
  143. function onGameInit()
  144.     MapGen = mgDrawn
  145.     TemplateFilter = 0
  146.     generateCave()
  147. end
  148.  
  149. function onPreviewInit()
  150.     onGameInit()
  151. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top