Evil Science A whole load of stuff


A C# algorithm to build interesting cave systems for your Roguelike – part 2 class layout

This article describes the layout of the class csCaveGenerator.cs which is used to generate a cave system, and can be viewed on GitHub here.?Within this class are several regions which are used to group together related methods, properties, data structures etc. These regions are:

  1. Properties
  2. Misc
  3. Map Structures
  4. Lookups
  5. Cave Related
  6. Corridor Related
  7. Direction Related
  8. Cell Related

Properties Region

Contains the properties used to control the appearance of the cave system being generated. Discussed in detail in part 1.


Contains the class constructor and the method Build() which generates the caves.

Map Structures Region

This contains the generic lists used to hold cave system data: Caves, Corridors and Map.

Lookups Region

Contains two lists of points which contain directions:

  1. Directions: four points which represent North, South, East and West.
  2. Directions1: 8 points which represent?North, South, East, West, North East, North West, South East and South West.

These lists are used to examine the neighbours of a cell for the cave generation, smoothing and filling operations.

Cave Related Region

Contains the code used to generate caves, and subdivided into three regions:

  1. Make Caves
  2. Locate Caves
  3. Connect Caves

Make Caves

This contains the method BuildCaves() which generates the cave system

Locate Caves

This contains three methods used to locate discrete caves on the map and place them in the generic list?Caves.?The method GetCaves() is called to do this - which uses a recursive flood fill algorithm.

Connect Caves

Contains the method?ConnectCaves(), a brute force method which randomly attempts to connect caves. Dependant upon methods in the Cave Related Region.

Corridor Related Region

Contains the methods:

  1. Corridor_GetEdge()
  2. Corridor_Attempt()
  3. Corridor_PointTest()

Which are all used by the method ConnectCaves().

Direction Related Region

This region contains a series of methods using in locating the neighbours of a cell, generating a randomly direction etc

Cell Related Region

A series of methods used for manipulating map cells.

Filed under: C#, Roguelike Leave a comment
Comments (0) Trackbacks (0)

No comments yet.

Leave a comment

No trackbacks yet.