Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add binary space partition and random room placement algorithms for procedural generation #3

Merged
merged 7 commits into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ concat-string = { version = "1.0.1", optional = true }
[features]
default = [
"acreplace",
"binary_space_partition",
"cellularnoise",
"dmi",
"file",
Expand All @@ -69,6 +70,7 @@ default = [
"json",
"log",
"noise",
"random_room_placement",
"sql",
"time",
"toml",
Expand All @@ -77,13 +79,15 @@ default = [

# default features
acreplace = ["aho-corasick"]
binary_space_partition = ["rand", "rayon", "serde", "serde_json", "sha2"]
cellularnoise = ["rand", "rayon"]
dmi = ["png", "image"]
file = []
git = ["git2", "chrono"]
http = ["reqwest", "serde", "serde_json", "once_cell", "jobs"]
json = ["serde", "serde_json"]
log = ["chrono", "flume"]
random_room_placement = ["rand", "rayon", "serde", "serde_json", "sha2"]
sql = ["mysql", "serde", "serde_json", "once_cell", "dashmap", "jobs"]
time = []
toml = ["serde", "serde_json", "toml-dep"]
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ To get additional features, pass a list to `--features`, for example `--features

The default features are:
* acreplace: Aho-Corasick string matching and replacement.
* binary_space_partition: Function to generate "rooms" more or less evenly distributed over a given area.
* cellularnoise: Function to generate cellular automata-based noise.
* dmi: DMI manipulations which are impossible from within BYOND.
Used by the asset cache subsystem to improve load times.
Expand All @@ -97,6 +98,7 @@ The default features are:
* json: Function to check JSON validity.
* log: Faster log output.
* noise: 2d Perlin noise.
* random_room_placement: Function to generate "rooms" randomly placed in a given area, only taking care to not overlap one another.
* sql: Asynchronous MySQL/MariaDB client library.
* time: High-accuracy time measuring.
* toml: TOML parser.
Expand Down
24 changes: 24 additions & 0 deletions dmsrc/binary-space-partition.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* This proc generates rooms in a specified area of random size and placement. Essential for procedurally generated areas, BSP works by cutting a given area in half,
* then cutting one of those subsections in half, and repeating this process until a minimum size is reached, then backtracking to other subsections that are not of
* the minimum size yet. These cuts are offset by small random amounts so that the sections are all varied in size and shape.
*
* BSP excels at creating rooms or areas with a relatively even distribution over an area, so there won't be too much blank open area. However if you discard rooms that
* overlap pre-existing map structures or areas, you may still get blank areas where nothing interesting appears.
*
* Return:
* * a json list of room data to be processed by json_decode in byond and further processed there.
*
* Arguments:
* * width: the width of the area to generate in
* * height: the height of the area to generate in
* * hash: the rng seed the generator will use for this instance
* * map_subsection_min_size: The minimum size of a map subsection. When using this for rooms with walls, the minimum possible square will be a 5x5 room. Barring walls,
* this will be a 3x3 room. The maximum size will be 9x9, because a further cut could reduce this size beneath the minimum size.
* * map_subsection_min_room_width: The minimum room width once the subsections are finalized. Room width and height are random between this amount, and the subsection
* max size
* * map_subsection_min_room_height: The minimum room height once the subsections are finalized. Room width and height are random between this amount, and the subsection
* max size
*/
#define rustg_bsp_generate(width, height, hash, map_subsection_min_size, map_subsection_min_room_width, map_subsection_min_room_height) \
RUSTG_CALL(RUST_G, "bsp_generate")(width, height, hash, map_subsection_min_size, map_subsection_min_room_width, map_subsection_min_room_height)
24 changes: 24 additions & 0 deletions dmsrc/random-room-placement.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* This proc generates rooms in a specified area of random size and placement. Used in procedural generation, but far less intensively than Binary Space Partitioning
* due to Random Room Placement being far more simple and unreliable for area coverage. These rooms will not overlap one another, but that is the only logic
* they do. The room dimensions returned by this call are hardcoded to be the dimensions of maint ruins so that I could sprinkle pre-generated areas over
* the binary space rooms that are random.
* These dimensions are:
* * 3x3
* * 3x5
* * 5x3
* * 5x4
* * 10x5
* * 10x10
*
* Return:
* * a json list of room data to be processed by json_decode in byond and further processed there.
*
* Arguments:
* * width: the width of the area to generate in
* * height: the height of the area to generate in
* * desired_room_count: the number of rooms you want generated and returned
* * hash: the rng seed the generator will use for this instance
*/
#define rustg_random_room_generate(width, height, desired_room_count, hash) \
RUSTG_CALL(RUST_G, "random_room_generate")(width, height, desired_room_count, hash)
Loading
Loading