Difference between revisions of "Diffusion-limited aggregation"

From RogueBasin
Jump to navigation Jump to search
(Added a javascript example →‎Code)
Line 282: Line 282:


== Code ==
== Code ==
Pseudocode for block aggregation:
=== Pseudocode for block aggregation ===
 
* Fill a level with wall
* Fill a level with wall
* Dig a seed tile or block, probably in the centre
* Dig a seed tile or block, probably in the centre
Line 294: Line 293:
** Dig out the tiles covered by the block
** Dig out the tiles covered by the block
* Add any other desired features, eg. extra corridors to increase connectivity
* Add any other desired features, eg. extra corridors to increase connectivity
=== Simple Javascript example ===
Most browsers hit 'f5' to refresh and regenerate the map. The map will have a random floor color with some opacity to make it look interesting.
<div style="background-color: #EEEEEE; border-style: dotted; padding: 0.3em">
<syntaxhighlight lang="javascript">
<!DOCTYPE html>
<html>
<head>
<meta charset = "utf-8">
<title>testing code</title>
<style type = "text/css">
body {background-color:black;}
table {background-color:#222222;}
td {width:30px;height:30px;}
th {color:white;}
div#title      {background-color:#555555;width:100%;height:100%;}
div#title:hover {background-color:#999999;}
div#wall        {background-color:#666666;width:85%;
height:90%;border:3px solid #363636;}
div#floor      {width:100%;height:100%;opacity: .7;}
</style>
<script>
var size = 24,x,y,cx,cy;
var map = new Array(size);
// Set the map array
for (var i = 0; i <= size * size; ++i){
map[i] = new Array(size);
}
// Initialize the Map Array to Zeros
for (x=1;x<=size;++x){
for (y=1;y<=size;++y){
map[x][y]=0;
} //end for
} //end for
// Initialize variables used in the process
var builderSpawned=0,builderMoveDirection=0;
var allocatedBlocks=0,failedBlocks=0,pawnLocationX=3,pawnLocationY=3;
// Loop to generate map
while (allocatedBlocks<size*(size/4)){
if (builderSpawned!=1){
//Spawn at random position
cx = 2+Math.floor(Math.random()*size-2)
cy = 2+Math.floor(Math.random()*size-2)
//See if spawn is within 1 block of pawn after spawn
if (Math.abs(pawnLocationX - cx)<=1 && Math.abs(pawnLocationY-cy)<=1){
//builder was spawned too close to spawn, clear that floor and respawn
if (map[cx][cy]!=1){
map[cx][cy]=1;
allocatedBlocks++;
} //end if
} else {
builderSpawned = 1;
builderMoveDirection = Math.floor(Math.random()*8);
} //end if
} else { //builder already spawned and knows it's direction, move builder
//NORTH
if (builderMoveDirection==0 && cy>0){cy--;
//EAST
} else if (builderMoveDirection==1 && cx<size){cx++;
//SOUTH
} else if (builderMoveDirection==2 && cy<size){cy++;
//WEST
} else if (builderMoveDirection==3 && cx>0){cx++;
//NORTHEAST
} else if (builderMoveDirection==4 && cx<size && cy>0){cy--;cx++;
//SOUTHEAST
} else if (builderMoveDirection==5 && cx<size && cy<size){cy++;cx++;
//SOUTHWEST
} else if (builderMoveDirection==6 && cx>0 && cy<size){cy++;cx--;
//NORTHWEST
} else if (builderMoveDirection==7 && cx>0 && cy>0){cy--;cx--;
} //end if
// ensure that the builder is touching an existing spot
if (cx<size && cy<size && cx>1 && cy>1){
//TEST EAST
if (map[cx+1][cy]==1){if (map[cx][cy]!=1){
map[cx][cy]=1;allocatedBlocks++;}
//TEST WEST
} else if (map[cx][cy]==1){if (map[cx][cy]!=1){
map[cx][cy]=1;allocatedBlocks++;}
//TEST SOUTH
} else if (map[cx][cy+1]==1){if (map[cx][cy]!=1){
map[cx][cy]=1;allocatedBlocks++;}
//TEST NORTH
} else if (map[cx][cy-1]==1){if (map[cx][cy]!=1){
map[cx][cy]=1;allocatedBlocks++;}
//TEST NORTHEAST
} else if (map[cx+1][cy-1]==1){if (map[cx][cy]!=1){
map[cx][cy]=1;map[cx+1][cy]=1;allocatedBlocks++;}
//TEST SOUTHEAST
} else if (map[cx+1][cy+1]==1){if (map[cx][cy]!=1){
map[cx][cy]=1;map[cx+1][cy]=1;allocatedBlocks++;}
//TEST SOUTHWEST
} else if (map[cx-1][cy+1]==1){if (map[cx][cy]!=1){
map[cx][cy]=1;map[cx-1][cy]=1;allocatedBlocks++;}
//TEST NORTHWEST
} else if (map[cx-1][cy-1]==1){if (map[cx][cy]!=1){
map[cx][cy]=1;map[cx-1][cy]=1;allocatedBlocks++;}
} //end if
} else { builderSpawned=0; }
} //end if
} //end while
// Draw the map
document.writeln("<table><tr>");
// Generate random color for the floor so it looks pretty every time
var c1=Math.floor(Math.random()*255);
var c2=Math.floor(Math.random()*255);
var c3=Math.floor(Math.random()*255);
for (x=1;x<=size;++x){
for (y=1;y<=size;++y){
if (map[x][y]==0){
document.writeln("<td id='cellx"+x+"y"+y+"'><div id='wall'></div></td>");
} else if (map[x][y]==1){
document.writeln("<td id='cellx"+x+"y"+y+"'><div id='floor' style='background-color:rgb("+c1+","+c2+","+c3+");'></div></td>");
} //end if
} //end for
document.writeln("</tr><tr>");
} //end for
document.writeln("<th colspan='"+size+"'><div id='title'>Diffusion Limited Aggregation</div></th></tr></table>");
</script>
</head>
<body></body>
</html>
</syntaxhighlight>


== Related articles ==
== Related articles ==

Revision as of 16:37, 11 October 2012

Diffusion-limited aggregation is a natural phenomenon in which particles undergoing Brownian motion cluster into aggregates of such particles. The process can be simulated in map generation to create rough tree-like structures; the resulting dungeons can make nice caves.

Introduction

The early roguelikes used rectangular rooms connected by straight corridors for their maps, which can eventually become boring. Relatively few alternatives have been documented (apart from cellular automata, an excellent article), so here's another radically different method.

The basic idea of DLA dungeon generation is this:

  1. Create a grid full of walls.
  2. Carve out a "seed," a small cluster of floor tiles near the center of the map.
  3. Create a "walker" on the map that moves around randomly.
  4. Move the walker around until it collides with a floor tile; then carve out another floor tile where that walker was.
  5. Return to step 3 and repeat ad infinitum.

Flexibility

Varying parameters and details of the algorithm can produce different-looking maps; for instance, here's one generated by random walkers who move both diagonally and orthogonally (like a chess king):

##########################################
#######.#########.########################
#####.#.########.#############.#.#########
###.##.#.########.#############..#########
####.#....######.################.####.###
#####.##....##...##############.#..#.....#
######..##..##..################..##....##
#######.###...###############.#........###
######.#####....########..##....#..##...##
############.#.########...##.#...###..##.#
##########......#######..##.#..##..##.####
##########....#.#####.#.####...#..####.###
########...##..##.###....#.......#########
#######...#........###....#...#.##########
########..#.##.....####...#..##.##########
########...#######.#.....#....############
########..######.##.....##...#############
#######...#.####...#..#####..####.########
#######....###.#..#..#.#.##...##.#.#######
########.######.#####.#..#...#.......#####
#################.##...#.............#####
#################.....##...###.#..#.######
################....#..##.....#.#....#####
############.###...#...####...#.....######
#########.#.##.##..##...###...##.#########
#########.#.....#.###...###..#############
##.#####..#...#..#.#......#.##############
##..#.##......##.#..#.#.......############
####....#..###..#.####..#.....############
###..###.###..#.#####....#...#############
#######.####...#.####..##...##############
####..#......#....####.#..#....###..######
####.#.#..#..#.########...##....##..######
#######........#########.#.#.#....########
########.#...#.########.#.#..#.#.#########
######..#.###.##########.###.#..##########
##.#...#.####..###########################
#........###..############################
#.######.#################################
##########################################

And here's one produced by walkers who can only move orthogonally:

#############################################
############################.################
#########################.##.################
########################..#..################
##############.#########.....################
##############..#######.......###############
#############....#######.......##############
###############..####........################
##############.#..##.....#.##################
#############........##..####################
###################..#...####################
###############......##..###.################
################......##..#..#...############
######.#.########.#...##.##..#...############
###..#....###.##.....##..##.###..##########.#
#...........#..##..#.#..........###.#.#.##..#
####...###.....###.........#.#....#.#......##
####.##........#.#...........#.#.........####
####.####..............##.##.....####...#####
########....#......##...#........############
######......#.#..##...#.##..##.##############
#######.....######.......#..#.###############
########...#####.#.............##############
#########.#####..#...#..##......##..#########
################........#..#######.##########
###############....##........#.##....#.######
##############...####.....#............######
##########......####...##.#.#....#...########
###########....####...#####.#..#.#....#######
###########...#######...##.....##..##.#######
##################....#.####...##...#########
##################..###..##.....#..##########
###################.###..###.#...#.##########
##################..###..###......###########
#######################.########.############
##################......#####################
#################....#..#####################
######################...####################
#####################...#####################
####################..#######################
#####################.#######################
#############################################

The orthogonal-only version seems to produce much "neater" maps, and wider tunnels. You could also try freezing particles whenever they pass next to the dug-out section, rather than just when they try to walk into it (as was used in the above examples). This would probably speed execution up slightly. A few other things are immediately noticeable (they may or may not be good things, depending on what you want from your gameplay):

  • The algorithm is guaranteed to produce a connected map.
  • Loops are quite infrequent, large loops are generally not present, and dead ends are common. If you want more loops, they can be added by placing a tunnelling random walker or forcing placement of extra corridors (according to the level's flavour).
  • There are lots of places to corner a player or monster. (Or to hide in!)
  • There are no well-defined rooms, but a vague impression of corridors
  • There are lots of obstacles to ranged combat.

It's not visible in the above because I trimmed it, but there may be a lot of unused space on the perimeter of the level. You can fix this by tailoring the number of particles to your level size, or by running regular checks that the cave hasn't yet reached the edge.

However, building a whole dungeon by putting every floor tile on its own random walk can take a lot of time, especially at the beginning when a particle won't stop until it hits the precise spot in the centre of the map. This can be reduced by:

  • making the program more eager to freeze particles (waiting for adjacency rather than collision),
  • beginning with a larger "seed", for instance a room,
  • weighting the random walk so that they drift towards the centre (this may change the look of your levels significantly!),
  • resizing the grid as the level grows (this could be difficult),
  • using blocks of tiles instead of single tiles as particles.

Block Aggregation

This last option can have great effect on the appearance of the level, depending on the set of blocks used and the weights assigned to them; for instance, if the blocks are relatively large rectangles, long rows and columns, this method could be used to make traditional roguelike dungeons (and collision-checking would reduce to checking the edges of the block). Placing Angband-style vaults would also be easy in this model: simply make sure they have a wall surrounding them, and change the way particles are initially placed so that they don't interfere with the vault's innards. Block aggregation will look very different depending on whether particles are allowed to spawn in a position where they would immediately freeze.

Some ideas for block aggregation:

############################################################
#######################################..#.......###########
#####...###############################..#.#####.###########
#####.#.#########################........#.#####.###########
#####.#.###############.......###........#.#####.###########
#####.#.###############.#####.#####.##...#.#####.###########
#####.#.###############.#####.#####.##...#.#####.###########
#####.#.###############.#####.#####.##...#.#####.###########
#####.#.......#########.......#####......#.......###########
#####....####.################..######........##############
########.####.################..######.###....##############
########.####.#######........#..######.###.##.##############
########.####.#######.######.#..######.###.##.##############
########.####.#######.######.#..######.###....##############
########.####.#######.######.#..######...####.##############
########......#######....###.#..######........##############
##########.....#.............#..##.................#########
##########.....#.........#####...........##.######.#########
############......###.....####..............######.#########
############........#.###.######...##..#.##.######.#########
############.####.#.#.###.#..............##........#########
############......#.#.#...........###.....##################
############.######.#.#....#####..###.....##################
####.....###.######.#.....######..####.#..#####...##########
####.##.......#####.######........####.#..#####.#.##########
####.##........####.######.####...####.......##.#.##########
####.##....................####...##.........##.#.##########
####...........#####.##.........####.###........#.##########
######...##.......................##.###...###..#.##########
######.#.##.##.##..##.#...#.#..##.##.###...###....##########
######.#.##........##.#...#.#..................#############
######.#.#####.##.........#.#...#........###################
######...####.........#.#.#........#.#...###################
######..........................#........###################
######...###........##.##.....#..........###################
######.......#.....##.........#..........###################
##############.....##..#####....#........###########.....###
##############.....##........##.......#..#########....##.###
############.......###.###...##.##.##.#........###.#..##.###
############....#..###........#.##.##.....##.#.###.#..##.###
##########..............##.#..#....#####.....#.###.#.....###
##########.#...#...####.......####.................##.######
##########.#...#.#########.#.#####..##.##.###.###..##.######
##########.........#######.#.#####........###.###..##.######
########..##....##.#######...#####.###.######.........######
########..##.......###############.###.#####################
########..###.#.##################.###.#####################
########..###.#.######....########.....#####################
########..###.#........##.##################################
####...#..###.#........##.##################################
#.......#####...######.##.##################################
#.##.#..##############....##################################
#.##.#..####################################################
#.##.#..####################################################
#.##.#..####################################################
#.......####################################################
############################################################
  • Corridors only
############################################################
###############################################.############
######################################.########.############
###################################....########.############
######################################.########.############
###########...########################........#.############
###########......###################....####....############
###########.#####......###############.######.##############
###########.######.##########################.##############
###########.######.################.....##....##############
###########.#.####.#################.#.....##.##....###.....
###########.#.####.############.#.##.########.####.##.#.####
###########.#.####.############.#.##.....####.####.#..#.####
##########....#.#.#############.#.##..#######.####.#....####
#############.#.#.###.#########.#.##..#######.###....#..####
#############.#.#.###.##......#....#..#######.###..#.#...###
#############.#.#.###.########.........##########..###.#.###
#############.......#.##.......#...##........####..###....##
#############.######......#####.......######.####.####....##
#############.######...########...##########.####.####....##
#############.######...########...#.....###...............##
#############.##........####.##...###.#####.#####..#####..##
########.......#.....#.#####.##.#####.#####.#####..#.####.##
###############....#.......#.##.##.##.#####.####.....#######
####################.##.###..........####....#######.#######
####################.##.###.....#..#.###.#.......###.#######
#######################.#####........###.###########.#######
#######################......####....###.###########.#######
#######################.####.........###.###########.#######
############################.#####...###############.#######
############################.#####....##############.#######
############################.#####.#..##############.#######
############################.#####.#..##############.#######
############################.###......####.#################
#############################...##.#...###.#################
###########################........##...##.#################
##################################.####......###############
##################################.#########################
################################....########################
############################################################
############################################################
########################################.###################
#######################################.####################
###################.##################.#####################
########.###########.################.######################
#########.#########.#..##.###.######.#######################
##########.#########...#.###.####...########################
######.####.########..#.###.####...#########################
#######.###..######.##.###.####...##########################
########.###..#######.#.#.#.##...###########################
#########.###..######.##.###..##############################
########.#####..######..###.#.##############################
#########.#####..######.##.###.########.####################
#########..#####..####.#..####..######.########.############
########.##.#####..##.##.####.#...###.##.#####.#.###########
#######.####.#####.###..######.#...#..###.###.###.##########
######..###.###.#.####..#######.###...##.#.#.#####.#########
###.#.##.#.##.##..###..#.#####.#.#...#.##.#.#######.########
##.#.####.####....#..##.#.###.#.#...#.#..#.#.#######.#######
#.######...###..##.####..###.###.....#..####..#######.######
#######.##..#..#..####.##.#.###.#.#.....#####.##############
######.####.......###.####.###.#.#.#..########..############
#.###.#####..#...#.#.##.#.###.#.#.##.##########..###########
##.#.#####.##.......##.#..##.#.##########.#######.##########
###.#####.###..#....#.###..##.#######.##.#########.#########
########.###.#####...###.##...######.##.###########.########
###########.#######...#.###..#.####.##.######.#####.########
##########.#######.##..##.###.#.##..#.######.##.##.#########
#########.#####.#.###.##.#####.#...#.#.####.####..######.###
################.###.#...######...###.#.#..#####..#####.####
##############..#.#.#...####.##..#.###.#.#..###.##.##..#####
##############..##.#...####.#...#.#.###.#.#..#.####..#.#.###
#############..#.##...####.##..#.###.##..###...####...#.####
############...###...###..##.##.#####.##.####...##.#...#####
############.##.#.#.#.#..##.##########.##.######..##########
###########.####...###..####.####.#####.##..####..##########
##########.######..#.#.####..#####.#####.#.#####.#.#########
#########.######......####.##.#####.#####.#####.##..########
##########.####......#.##.####.#####.###.#####.##.##########
#########..###.#......#..############.#.#####.##.###########
########..#####.#..#.#...#############.#.###.###############
######...#####.#..###..################.#.##################
##.###..##.####..#####.###.##############..#################
###.#.##..####..######..#..#############.##.################
#.##.###...#.#.######.##..#############.####.###############
##..#.#.#...#.######.###.#############.#####################
##..##.#.###.#.####.###.####################################
####..#.#####.#.##.#########################################
#####..#.#####.#..##.#######################################
###############.#.#.########################################
##################.#########################################
#################.##########################################
############################################################


Code

Pseudocode for block aggregation

  • Fill a level with wall
  • Dig a seed tile or block, probably in the centre
  • Do until the level is considered sufficiently full (predefined number of blocks, edge proximity or number of tiles dug):
    • Choose a block, either from a given set or using a function
    • Choose a location from which to begin the block's walk
      • If necessary, limit this choice to locations that don't interfere with already-dug sections
    • Do until the block neighbours or collides with the dug-out section:
      • Move the block one step in a random direction
    • Dig out the tiles covered by the block
  • Add any other desired features, eg. extra corridors to increase connectivity

Simple Javascript example

Most browsers hit 'f5' to refresh and regenerate the map. The map will have a random floor color with some opacity to make it look interesting.

<!DOCTYPE html>
<html>
	<head>
		<meta charset = "utf-8">
		<title>testing code</title>
		<style type = "text/css">
			body {background-color:black;}
			table {background-color:#222222;}
			td {width:30px;height:30px;}
			th {color:white;}
			div#title       {background-color:#555555;width:100%;height:100%;}
			div#title:hover {background-color:#999999;}
			div#wall        {background-color:#666666;width:85%;
					height:90%;border:3px solid #363636;}
			div#floor       {width:100%;height:100%;opacity: .7;}
		</style>
		<script>
			var size = 24,x,y,cx,cy;
			var map = new Array(size);
			// Set the map array
			for (var i = 0; i <= size * size; ++i){
				map[i] = new Array(size);
			}
			// Initialize the Map Array to Zeros 
			for (x=1;x<=size;++x){
				for (y=1;y<=size;++y){
					map[x][y]=0;
				} //end for
			} //end for
			// Initialize variables used in the process
			var builderSpawned=0,builderMoveDirection=0;
			var allocatedBlocks=0,failedBlocks=0,pawnLocationX=3,pawnLocationY=3;
			// Loop to generate map
			while (allocatedBlocks<size*(size/4)){
				if (builderSpawned!=1){
					//Spawn at random position
					cx = 2+Math.floor(Math.random()*size-2)
					cy = 2+Math.floor(Math.random()*size-2)
					//See if spawn is within 1 block of pawn after spawn
					if (Math.abs(pawnLocationX - cx)<=1 && Math.abs(pawnLocationY-cy)<=1){
						//builder was spawned too close to spawn, clear that floor and respawn
						if (map[cx][cy]!=1){
							map[cx][cy]=1;
							allocatedBlocks++;
						} //end if
					} else {
						builderSpawned = 1;
						builderMoveDirection = Math.floor(Math.random()*8);
					} //end if
				} else { //builder already spawned and knows it's direction, move builder
					//NORTH
					if (builderMoveDirection==0 && cy>0){cy--;
					//EAST
					} else if (builderMoveDirection==1 && cx<size){cx++;
					//SOUTH
					} else if (builderMoveDirection==2 && cy<size){cy++;
					//WEST
					} else if (builderMoveDirection==3 && cx>0){cx++;
					//NORTHEAST
					} else if (builderMoveDirection==4 && cx<size && cy>0){cy--;cx++;
					//SOUTHEAST
					} else if (builderMoveDirection==5 && cx<size && cy<size){cy++;cx++;
					//SOUTHWEST
					} else if (builderMoveDirection==6 && cx>0 && cy<size){cy++;cx--;
					//NORTHWEST
					} else if (builderMoveDirection==7 && cx>0 && cy>0){cy--;cx--;
					} //end if
					// ensure that the builder is touching an existing spot
					if (cx<size && cy<size && cx>1 && cy>1){
						//TEST EAST
						if (map[cx+1][cy]==1){if (map[cx][cy]!=1){
							map[cx][cy]=1;allocatedBlocks++;}
						//TEST WEST
						} else if (map[cx][cy]==1){if (map[cx][cy]!=1){
							map[cx][cy]=1;allocatedBlocks++;} 
						//TEST SOUTH
						} else if (map[cx][cy+1]==1){if (map[cx][cy]!=1){
							map[cx][cy]=1;allocatedBlocks++;}
						//TEST NORTH
						} else if (map[cx][cy-1]==1){if (map[cx][cy]!=1){
							map[cx][cy]=1;allocatedBlocks++;}
						//TEST NORTHEAST
						} else if (map[cx+1][cy-1]==1){if (map[cx][cy]!=1){
							map[cx][cy]=1;map[cx+1][cy]=1;allocatedBlocks++;}
						//TEST SOUTHEAST
						} else if (map[cx+1][cy+1]==1){if (map[cx][cy]!=1){
							map[cx][cy]=1;map[cx+1][cy]=1;allocatedBlocks++;}
						//TEST SOUTHWEST
						} else if (map[cx-1][cy+1]==1){if (map[cx][cy]!=1){
							map[cx][cy]=1;map[cx-1][cy]=1;allocatedBlocks++;}
						//TEST NORTHWEST
						} else if (map[cx-1][cy-1]==1){if (map[cx][cy]!=1){
							map[cx][cy]=1;map[cx-1][cy]=1;allocatedBlocks++;}
						} //end if
					} else { builderSpawned=0; }
				} //end if
			} //end while
			// Draw the map
			document.writeln("<table><tr>");
			// Generate random color for the floor so it looks pretty every time
			var c1=Math.floor(Math.random()*255);
			var c2=Math.floor(Math.random()*255);
			var c3=Math.floor(Math.random()*255);
			for (x=1;x<=size;++x){
				for (y=1;y<=size;++y){
					if (map[x][y]==0){
						document.writeln("<td id='cellx"+x+"y"+y+"'><div id='wall'></div></td>");
					} else if (map[x][y]==1){
						document.writeln("<td id='cellx"+x+"y"+y+"'><div id='floor' style='background-color:rgb("+c1+","+c2+","+c3+");'></div></td>");
					} //end if
				} //end for
				document.writeln("</tr><tr>");
			} //end for
			document.writeln("<th colspan='"+size+"'><div id='title'>Diffusion Limited Aggregation</div></th></tr></table>");
		</script>
	</head>
	<body></body>
</html>

Related articles

See also