Skip to content

Instantly share code, notes, and snippets.

@matt-dray
Last active May 11, 2022 07:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matt-dray/fd5bf4b6560638e154353e4ec5c8b98a to your computer and use it in GitHub Desktop.
Save matt-dray/fd5bf4b6560638e154353e4ec5c8b98a to your computer and use it in GitHub Desktop.
Quick demo of perlin noise from the {ambient} R package to generate a dungeon map
print_perlin_dungeon <- function(
m, # matrix of perlin noise via ambient::noise_perlin()
invert = FALSE # flips tile positions (use set.seed before generating noise)
) {
tile_wall = "#"
tile_floor = "."
# Standardise noise values from 0 to 1
m_bin <- round((m - min(m)) / (max(m) - min(m)))
# Lay floor and wall tiles, flip if invert = TRUE
if (!invert) {
m_tiled <- ifelse(m_bin == 1, tile_wall, tile_floor)
} else {
m_tiled <- ifelse(m_bin == 0, tile_wall, tile_floor)
}
# Block off edges with wall tiles
m_tiled[, 1] <- tile_wall
m_tiled[, ncol(m_tiled)] <- tile_wall
m_tiled[1, ] <- tile_wall
m_tiled[nrow(m_tiled), ] <- tile_wall
# Print to console, line-by-line
for (i in seq(nrow(m_tiled))) {
cat(m_tiled[i, ], "\n")
}
}
m <- ambient::noise_perlin(
dim = c(30, 50),
frequency = 0.2,
interpolator = "linear",
fractal = "fbm",
octaves = 2,
lacunarity = 3,
gain = 0.5,
pertubation = "none",
pertubation_amplitude = 1
)
print_perlin_dungeon(m)
@matt-dray
Copy link
Author

perlin-dungeon-demo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment