Difference between revisions of "Diffusion-limited aggregation"

From RogueBasin
Jump to navigation Jump to search
(sections)
Line 1: Line 1:
(This page is under construction! More examples may be added soon)
(This page is under construction! More examples may be added soon)
== Introduction ==


Many [[roguelike]]s use rectangular rooms connected by straight corridors for their [[level|maps]], which after a while might get boring (or might not). Relatively few alternatives have been documented (apart from [[Cellular Automata Method for Generating Random Cave-Like Levels|cellular automata]], an excellent article), so here's another radically different method.
Many [[roguelike]]s use rectangular rooms connected by straight corridors for their [[level|maps]], which after a while might get boring (or might not). Relatively few alternatives have been documented (apart from [[Cellular Automata Method for Generating Random Cave-Like Levels|cellular automata]], an excellent article), so here's another radically different method.
Line 10: Line 12:
  ##### ##### ##### ##### ##### ##### ##### #####  
  ##### ##### ##### ##### ##### ##### ##### #####  
etc.
etc.
== 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:
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:
Line 114: Line 118:
* resizing the grid as the level grows (this could be difficult),
* resizing the grid as the level grows (this could be difficult),
* using blocks of tiles instead of single tiles as particles.
* 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.
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.

Revision as of 13:16, 27 September 2010

(This page is under construction! More examples may be added soon)

Introduction

Many roguelikes use rectangular rooms connected by straight corridors for their maps, which after a while might get boring (or might not). Relatively few alternatives have been documented (apart from cellular automata, an excellent article), so here's another radically different method.

Diffusion-limited aggregation is a process which grows tree-like fractal structures from a central seed "." . Particles "&" are placed at random on the grid, and go on a random walk (they repeatedly move one step in a random direction) until they hit the seed or a "frozen" particle, at which point they "freeze" in place and become a floor grid:

##### ##### ##### ##&## ##### ##&## ##### ##### 
&#### #&### ##### ##### ##&## ##### ##&## ##.## 
##.## ##.## #&.## #..## #..## #..## #..## #..## 
##### ##### ##### ##### ##### ##### ##### ####& 
##### ##### ##### ##### ##### ##### ##### ##### 

etc.

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:

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

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

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

Obviously, the orthogonal-only version produces 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

Related articles

See also