Last active
July 6, 2024 12:47
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Author
matt-dray
commented
May 8, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment