Skip to content
This repository was archived by the owner on May 22, 2025. It is now read-only.

Commit fa6e3bc

Browse files
authored
Merge pull request #3 from Chubbygummibear/procgen
Add binary space partition and random room placement algorithms for procedural generation
2 parents 97c921d + d583aba commit fa6e3bc

File tree

7 files changed

+773
-0
lines changed

7 files changed

+773
-0
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ concat-string = { version = "1.0.1", optional = true }
6161
[features]
6262
default = [
6363
"acreplace",
64+
"binary_space_partition",
6465
"cellularnoise",
6566
"dmi",
6667
"file",
@@ -69,6 +70,7 @@ default = [
6970
"json",
7071
"log",
7172
"noise",
73+
"random_room_placement",
7274
"sql",
7375
"time",
7476
"toml",
@@ -77,13 +79,15 @@ default = [
7779

7880
# default features
7981
acreplace = ["aho-corasick"]
82+
binary_space_partition = ["rand", "rayon", "serde", "serde_json", "sha2"]
8083
cellularnoise = ["rand", "rayon"]
8184
dmi = ["png", "image"]
8285
file = []
8386
git = ["git2", "chrono"]
8487
http = ["reqwest", "serde", "serde_json", "once_cell", "jobs"]
8588
json = ["serde", "serde_json"]
8689
log = ["chrono", "flume"]
90+
random_room_placement = ["rand", "rayon", "serde", "serde_json", "sha2"]
8791
sql = ["mysql", "serde", "serde_json", "once_cell", "dashmap", "jobs"]
8892
time = []
8993
toml = ["serde", "serde_json", "toml-dep"]

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ To get additional features, pass a list to `--features`, for example `--features
8888
8989
The default features are:
9090
* acreplace: Aho-Corasick string matching and replacement.
91+
* binary_space_partition: Function to generate "rooms" more or less evenly distributed over a given area.
9192
* cellularnoise: Function to generate cellular automata-based noise.
9293
* dmi: DMI manipulations which are impossible from within BYOND.
9394
Used by the asset cache subsystem to improve load times.
@@ -97,6 +98,7 @@ The default features are:
9798
* json: Function to check JSON validity.
9899
* log: Faster log output.
99100
* noise: 2d Perlin noise.
101+
* random_room_placement: Function to generate "rooms" randomly placed in a given area, only taking care to not overlap one another.
100102
* sql: Asynchronous MySQL/MariaDB client library.
101103
* time: High-accuracy time measuring.
102104
* toml: TOML parser.

dmsrc/binary-space-partition.dm

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* 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,
3+
* 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
4+
* the minimum size yet. These cuts are offset by small random amounts so that the sections are all varied in size and shape.
5+
*
6+
* 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
7+
* overlap pre-existing map structures or areas, you may still get blank areas where nothing interesting appears.
8+
*
9+
* Return:
10+
* * a json list of room data to be processed by json_decode in byond and further processed there.
11+
*
12+
* Arguments:
13+
* * width: the width of the area to generate in
14+
* * height: the height of the area to generate in
15+
* * hash: the rng seed the generator will use for this instance
16+
* * 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,
17+
* this will be a 3x3 room. The maximum size will be 9x9, because a further cut could reduce this size beneath the minimum size.
18+
* * 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
19+
* max size
20+
* * 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
21+
* max size
22+
*/
23+
#define rustg_bsp_generate(width, height, hash, map_subsection_min_size, map_subsection_min_room_width, map_subsection_min_room_height) \
24+
RUSTG_CALL(RUST_G, "bsp_generate")(width, height, hash, map_subsection_min_size, map_subsection_min_room_width, map_subsection_min_room_height)

dmsrc/random-room-placement.dm

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* 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
3+
* 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
4+
* 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
5+
* the binary space rooms that are random.
6+
* These dimensions are:
7+
* * 3x3
8+
* * 3x5
9+
* * 5x3
10+
* * 5x4
11+
* * 10x5
12+
* * 10x10
13+
*
14+
* Return:
15+
* * a json list of room data to be processed by json_decode in byond and further processed there.
16+
*
17+
* Arguments:
18+
* * width: the width of the area to generate in
19+
* * height: the height of the area to generate in
20+
* * desired_room_count: the number of rooms you want generated and returned
21+
* * hash: the rng seed the generator will use for this instance
22+
*/
23+
#define rustg_random_room_generate(width, height, desired_room_count, hash) \
24+
RUSTG_CALL(RUST_G, "random_room_generate")(width, height, desired_room_count, hash)

0 commit comments

Comments
 (0)