Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local CHANCE_TO_START_ALIVE = 450
- local BIRTH_LIMIT = 5
- local DEATH_LIMIT = 5
- local STEPS = 6
- local distance = 40
- local pointSize = 16
- function generateCave()
- if MapFeatureSize then distance = MapFeatureSize*2 end
- x = GetRandom(4096)
- y = GetRandom(2048)
- local world = {}
- initializeWord(world)
- for i=1, STEPS do
- world = simulationStep(world)
- end
- local x = 1
- local y = 1
- while x <= 4096/distance do
- while y <= 2048/distance do
- if world[x][y] then
- AddPoint((x-1) * distance + distance/2, (y-1) * distance + distance/2, pointSize)
- end
- y = y+1
- end
- x = x+1
- y = 1
- end
- local distance2 = distance*2
- x = 1
- y = 1
- while x <= 4096/distance2 do
- while y <= 2048/distance2 do
- local average = 0
- if world[x*2][y*2] then average = average+1 end
- if world[x*2+1][y*2] then average = average+1 end
- if world[x*2+1][y*2+1] then average = average+1 end
- if world[x*2+1][y*2] then average = average+1 end
- if average >= 2 then
- AddPoint((x*2-2) * distance + distance/2, (y*2-1) * distance + distance/2, pointSize/2)
- AddPoint((x*2-2) * distance + distance/2, (y*2-2) * distance + distance/2, pointSize/2)
- AddPoint((x*2-1) * distance + distance/2, (y*2-2) * distance + distance/2, pointSize/2)
- AddPoint((x*2-1) * distance + distance/2, (y*2-1) * distance + distance/2, pointSize/2)
- end
- y = y+1
- end
- x = x+1
- y = 1
- end
- FlushPoints()
- end
- function initializeWord(world)
- local x = 1
- local y = 1
- while x <= 4096/distance do
- table.insert(world, {})
- while y <= 2048/distance do
- if GetRandom(1000) < CHANCE_TO_START_ALIVE then
- table.insert(world[x], true)
- else
- table.insert(world[x], false)
- end
- y = y+1
- end
- x = x+1
- y = 1
- end
- end
- function simulationStep(world)
- local x = 1
- local y = 1
- world2 = {}
- while x <= 4096/distance do
- table.insert(world2, {})
- while y <= 2048/distance do
- local count = countNeigbours(world, x, y)
- if world[x][y] then
- if count < DEATH_LIMIT then
- table.insert(world2[x], false)
- else
- table.insert(world2[x], true)
- end
- else
- if count > BIRTH_LIMIT then
- table.insert(world2[x], true)
- else
- table.insert(world2[x], false)
- end
- end
- y = y+1
- end
- x = x+1
- y = 1
- end
- return world2
- end
- function countNeigbours(world, x, y)
- count = 0
- for dx=1, 3 do
- for dy=1, 3 do
- local x2 = x+dx-2
- local y2 = y+dy-2
- if x2 == 0 or x2 > 4096/distance or y2 == 0 or y2 > 2048/distance or world[x2][y2] then
- count = count + 1
- end
- end
- end
- return count
- end
- function onGameInit()
- MapGen = mgDrawn
- TemplateFilter = 0
- generateCave()
- end
- function onPreviewInit()
- onGameInit()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement