Documentation ¶
Overview ¶
Package dngn is a simple random map generation library primarily made to be used for 2D games. It features a simple API, and a couple of different means to generate maps. The easiest way to kick things off when using dngn is to simply create a Room to represent your overall game map, which can then be manipulated or have a Generate function run on it to actually generate the content on the map.
Index ¶
- type Room
- func (room *Room) Area() int
- func (room *Room) Center() (int, int)
- func (room *Room) ClearSeed()
- func (room *Room) CopyFrom(other *Room, x, y int)
- func (room *Room) DataToString() string
- func (room *Room) DrawLine(x, y, x2, y2 int, fillRune rune, thickness int, stagger bool)
- func (room *Room) GenerateBSP(wallValue, doorValue rune, numSplits int)
- func (room *Room) GenerateDrunkWalk(fillRune rune, percentageFilled float32)
- func (room *Room) GenerateRandomRooms(roomFillRune rune, hallwayFillRune rune, ...) [][]int
- func (room *Room) Get(x, y int) rune
- func (room *Room) MinimumSize() int
- func (room *Room) Resize(width, height int) *Room
- func (room *Room) Rotate()
- func (room *Room) Select() Selection
- func (room *Room) SelectContiguous(x, y int) Selection
- func (room *Room) Set(x, y int, char rune)
- func (room *Room) SetSeed(seed int64)
- type Selection
- func (selection Selection) AddSelection(other Selection) Selection
- func (selection Selection) By(filterFunc func(x, y int) bool) Selection
- func (selection Selection) ByArea(x, y, w, h int) Selection
- func (selection Selection) ByNeighbor(neighborValue rune, minNeighborCount int, diagonals bool) Selection
- func (selection Selection) ByPercentage(percentage float32) Selection
- func (selection Selection) ByRune(value rune) Selection
- func (selection *Selection) Contains(x, y int) bool
- func (selection Selection) Degrade(char rune) Selection
- func (selection Selection) Expand(distance int, diagonal bool) Selection
- func (selection Selection) Fill(char rune) Selection
- func (selection Selection) Invert() Selection
- func (selection Selection) RemoveSelection(other Selection) Selection
- func (selection Selection) Shrink(diagonal bool) Selection
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Room ¶
type Room struct {
Width, Height int
Data [][]rune
CustomSeed bool
// contains filtered or unexported fields
}
Room represents a dungeon map. Width and Height are the width and height of the Room in the layout. This determines the size of the overall Data structure backing the Room layout. Data is the core underlying data structure representing the dungeon. It's a 2D array of runes. Seed is the seed of the Room to use when doing random generation using the Generate* functions below. CustomSeed indicates whether the Seed was customized - if not, then it will default to using the time of the system to have random generation each time you use Generate* functions.
func NewRoomFromRuneArrays ¶
NewRoomFromRuneArrays creates a new Room with the data contained in the provided rune arrays.
func NewRoomFromStringArray ¶
NewRoomFromStringArray creates a new Room with the data contained in the provided string array.
func (*Room) Area ¶
Area returns the overall size of the Room by multiplying the width by the height.
func (*Room) ClearSeed ¶
func (room *Room) ClearSeed()
ClearSeed clears a custom seed set for random generation. When using a clear seed, random generation functions will use the system's Unix time.
func (*Room) CopyFrom ¶
CopyFrom copies the data from the other Room into this Room's data. x and y are the position of the other Room's data in the destination (calling) Room.
func (*Room) DataToString ¶
DataToString returns the underlying data of the overall Room layout in an easily understood visual format. 0's turn into blank spaces when using DataToString, and the column is shown at the left of the map.
func (*Room) DrawLine ¶
DrawLine is used to draw a line from x, y, to x2, y2, placing the rune specified by fillRune in the cells between those points (including) in those points themselves, as well. thickness controls how thick the line is. If stagger is on, then the line will stagger it's vertical movement, allowing a 1-thickness line to actually be pass-able if an object was only able to move in cardinal directions and the line had a diagonal slope.
func (*Room) GenerateBSP ¶
GenerateBSP generates a map using BSP (binary space partitioning) generation, drawing lines of wallValue runes horizontally and vertically across, partitioning the room into pieces. It also will place single cells of doorValue on the walls, creating doorways. Link: http://www.roguebasin.com/index.php?title=Basic_BSP_Dungeon_generation BUG: GenerateBSP doesn't handle pre-existing doorValues well (i.e. if 0's already exist on the map and you try to use a doorValue of 0 to indicate not to place doors, it bugs out; either it might place a wall where a doorway exists, or it won't place anything at all because everywhere could be a door). A workaround is to use another value that you turn into 0's later.
func (*Room) GenerateDrunkWalk ¶
GenerateDrunkWalk generates a map in the bounds of the Room specified using drunk walking. It will pick a random point in the Room and begin walking around at random, placing fillRune in the Room, until at least percentageFilled (0.0 - 1.0) of the Room is filled. Note that it only counts values placed in the cell, not instances where it moves over a cell that already has the value being placed. Link: http://www.roguebasin.com/index.php?title=Random_Walk_Cave_Generation
func (*Room) GenerateRandomRooms ¶
func (room *Room) GenerateRandomRooms(roomFillRune rune, hallwayFillRune rune, roomCount, roomMinWidth, roomMinHeight, roomMaxWidth, roomMaxHeight int, connectRooms bool, allowDiagonal bool) [][]int
GenerateRandomRooms generates a map using random room creation. roomFillRune is the rune to fill the rooms generated with. hallwayFillRune is the rune to fill the hallways with roomCount is how many rooms to place roomMinWidth and Height are how small they can be, minimum, while roomMaxWidth and Height are how large
connectRooms determines if the algorithm should also attempt to connect the rooms using pathways between each room. allowDiagonal determines the shape of the connections. If true, then a direct connection will be made. If false, then only horizontal or vertical connections are made.
The function returns the positions of each room created.
func (*Room) Get ¶
Get returns the rune in the specified position in the Room's Data array. A convenience function stand-in for "value := room.Data[y][x]".
func (*Room) MinimumSize ¶
MinimumSize returns the minimum distance (width or height) for the Room.
func (*Room) Resize ¶
Resize resizes the room to be of the width and height provided. Note that resizing to a smaller Room is destructive (and so, data will be lost if resizing to a smaller Room).
func (*Room) Rotate ¶
func (room *Room) Rotate()
Rotate rotates the entire room 90 degrees clockwise.
func (*Room) SelectContiguous ¶
SelectContiguous generates a Selection containing all contiguous (connected) cells featuring the same value as the cell in the position provided.
type Selection ¶
A Selection represents a selection of cell positions in the Room's data array, and can be filtered down and manipulated using the functions on the Selection struct.
func (Selection) AddSelection ¶
AddSelection adds the cells in the other Selection to the current one if they're not already in it.
func (Selection) By ¶
By simply takes a function that takes the X and Y values of each cell position contained in the Selection, and returns a boolean to indicate whether to include that cell in the Selection or not. This allows you to easily make custom filtering functions to filter down the cells in a Selection.
func (Selection) ByArea ¶
ByArea filters down a selection by only selecting the cells that have X and Y values between X, Y, and X+W and Y+H. It crops the selection, basically.
func (Selection) ByNeighbor ¶
func (selection Selection) ByNeighbor(neighborValue rune, minNeighborCount int, diagonals bool) Selection
ByNeighbor selects the cells that are surrounded at least by minNeighborCount neighbors with a value of neighborValue. If diagonals is true, then diagonals are also checked.
func (Selection) ByPercentage ¶
ByPercentage selects the provided percentage of the cells curently in the Selection.
func (Selection) ByRune ¶
ByRune filters the Selection down to the cells that have the character (rune) provided.
func (*Selection) Contains ¶
Contains returns a boolean indicating if the specified cell is in the list of cells contained in the selection.
func (Selection) Degrade ¶
Degrade applies a formula that randomly sets the cells in the selection to the provided char rune if their neighbors have that same rune value. The more neighbors that have the rune value, the more likely the selected cell will be set to it as well.
func (Selection) Expand ¶
Expand expands the selection outwards by the distance value provided. Diagonal indicates if the expansion should happen diagonally as well, or just on the cardinal 4 directions.
func (Selection) Invert ¶
Invert inverts the selection (selects all non-selected cells from the Selection's source Room).
func (Selection) RemoveSelection ¶
RemoveSelection removes the cells in the other Selection from the current one if they are already in it.