I been on a random creation kind of kick lately. I read about making caves using a technique similar to the premise of conway's game of life. There's still room for improvements. One idea I had was to use a flood fill to find small isolated pockets and delete them for a smoother more user-friendly map. After reading a few differences sources, I found some folks actually use that exact technique already.
Press spacebar to step the algorithm.
// Project: caves
// Created: 2018-08-21
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "caves" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
#constant WIDTH = 80
#constant HEIGHT = 60
#constant CHANCE_ALIVE = 0.4
dim map[WIDTH, HEIGHT] as integer
for x = 0 to WIDTH-1
for y = 0 to HEIGHT-1
if (random(0, 100) / 100.0) < CHANCE_ALIVE
map[x,y] = 1
endif
next y
next x
c = makeColor(0,0,255)
steps = 0
do
size = 8 //size to draw each cell
for x = 0 to WIDTH-1
for y = 0 to HEIGHT-1
if map[x,y] = 1
drawBox(x*size, y*size, x*size+size, y*size+size, c,c,c,c,1)
endif
next y
next x
if getRawKeyPressed(32)
inc steps
map = simStep(map)
endif
mx = getRawMouseX() / size
my = getRawMouseY() / size
t = countAlive(map, mx, my)
print(t)
Print("Steps: "+str(steps))
Sync()
loop
function simStep(map ref as integer[][])
dim n[WIDTH, HEIGHT] as integer
deathLimit = 3
birthLimit = 4
for x = 0 to WIDTH-1
for y = 0 to HEIGHT-1
alives = countAlive(map, x, y)
if map[x,y] = 1
if alives < deathLimit
n[x,y] = 0
else
n[x,y] = 1
endif
else
if alives > birthLimit
n[x,y] = 1
else
n[x,y] = 0
endif
endif
next y
next x
endfunction n
function countAlive(m ref as integer[][], x, y)
alive = 0
for i = -1 to 1
for j = -1 to 1
nx = x+i
ny = y+j
if nx <> x or ny <> y
if nx >= 0 and nx < WIDTH and ny >= 0 and ny < HEIGHT
if m[nx, ny] = 1 then alive = alive + 1
else
alive = alive + 1
endif
endif
next j
next i
endfunction alive