Skip to content

Commit

Permalink
Adds Biomes to the Cave Generator, for all of your procedurally-place…
Browse files Browse the repository at this point in the history
…d cave biome needs! (#83138)

Implements biomes into the Cave Generator, using some adapted code from
the biomes feature of the Jungle Generator. It's there as a tool for
whomever would want to implement it on /tg/, I simply don't have the
sprites, mobs and motivation to add biomes to anything at this current
point in time, even though I'm fully open to helping anyone that would
be interested in doing so.

Here's how it works:
You supply a 2D list of biomes based on the two arbitrary criteria
'heat' and 'humidity', you can treat these as simply two independent
variables that would affect your biome distribution. There's three
levels of each, `LOW`, `MEDIUM` and `HIGH`, take a look at
`possible_biomes` for a good example of it. Here's what it looks like by
default (yes, that's the default on the jungle generator as well, except
here we use 3x3 instead of 4x4):

![image](https://github.com/tgstation/tgstation/assets/58045821/2c53b46b-f4f9-497f-9647-efc2cc118805)

On the `/datum/biome`, you have three important stats, split into two
each: flora, features and fauna. They are evaluated in this order, so if
a flora spawns, no feature nor fauna will spawn. If a feature spawns, no
fauna will spawn, and if fauna spawns, then that's cool. Each of these
stats have a corresponding `density` (i.e. `flora_density`), which is
simply the probability for that thing to be spawned if it's eligible,
and a `types` list (i.e. `flora_types`), which is a weighted list that
then gets expanded at runtime in order to make the `pick()` operation
faster.

The areas you want to have the biomes in also need to have their
`area_flags` set up to include `FLORA_ALLOWED` for both flora and
features, and `MOB_SPAWN_ALLOWED` for fauna to spawn.

The fauna currently does just about every check that is done in
`cave_generator`'s `populate_terrain()`, except for handling megafauna
differently, or taking megafauna into account. If that's desired, it can
be added easily, I simply chose not to add it because it felt like
wasted processor time over something that would probably not be
pertinent in the majority of cases.

I've run a few tests, and keeping in mind that I've got a high-specs
computer, generating the caves with biomes takes about 1 second for an
entire z-level covered in biomes. For comparison, I compile the repo in
about 36 seconds. ~~It may increase the amount of time spent
initializing the atoms subsystem, however, I'll need to compare that,
I'd really appreciate some help optimizing that if anyone knows how
to.~~ It didn't seem to have an effect, I just had seen things a bit
weird. I optimized things by moving rust-g calls outside of the for
loop, and we gained about 0.3-0.4 seconds, which is pretty nice.

Biomes are cool, and since we use mainly cave generators for z-level
generation, I decided to add biomes to that, so that the biome code
added by floyd lives on.

Here's an example of ice box with jungle caves, just as a proof of
concept, to prove that it works:

![image](https://github.com/tgstation/tgstation/assets/58045821/33b348db-513b-4a2e-b11f-907e80b65177)

:cl: GoldenAlpharex
add: Added Biomes capabilities to the Cave Generator, to allow for
procedurally-placed biomes to be introduced in cave generation. This
feature is not currently used on any map, but the tools are all there
for anyone with the motivation to add biomes to any cave-generating
area, like Lavaland and Ice Box.
code: Biomes can now affect features (which are usually structures), on
top of flora and fauna.
/:cl:
  • Loading branch information
GoldenAlpharex authored and dwasint committed Jun 28, 2024
1 parent f58e1e3 commit 0806a20
Show file tree
Hide file tree
Showing 4 changed files with 375 additions and 13 deletions.
2 changes: 2 additions & 0 deletions code/__DEFINES/maps.dm
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,13 @@ Always compile, always use that verb, and always make sure that it works for wha

#define BIOME_LOW_HEAT "low_heat"
#define BIOME_LOWMEDIUM_HEAT "lowmedium_heat"
#define BIOME_MEDIUM_HEAT "medium_heat"
#define BIOME_HIGHMEDIUM_HEAT "highmedium_heat"
#define BIOME_HIGH_HEAT "high_heat"

#define BIOME_LOW_HUMIDITY "low_humidity"
#define BIOME_LOWMEDIUM_HUMIDITY "lowmedium_humidity"
#define BIOME_MEDIUM_HUMIDITY "medium_humidity"
#define BIOME_HIGHMEDIUM_HUMIDITY "highmedium_humidity"
#define BIOME_HIGH_HUMIDITY "high_humidity"

Expand Down
180 changes: 177 additions & 3 deletions code/datums/mapgen/CaveGenerator.dm
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/// The random offset applied to square coordinates, causes intermingling at biome borders
#define BIOME_RANDOM_SQUARE_DRIFT 2

/datum/map_generator/cave_generator
var/name = "Cave Generator"
///Weighted list of the types that spawns if the turf is open
Expand Down Expand Up @@ -28,7 +31,23 @@
var/list/weighted_feature_spawn_list
///Expanded list of extra features that can spawn in the area. Reads from the weighted list
var/list/feature_spawn_list

/// The turf types to replace with a biome-related turf, as typecache.
/// Leave empty for all open turfs (but not closed turfs) to be hijacked.
var/list/biome_accepted_turfs = list()
/// An associative list of biome type to the list of turfs that were
/// generated of that biome specifically. Helps to improve the efficiency
/// of biome-related operations. Is populated through
/// `generate_terrain_with_biomes()`.
var/list/generated_turfs_per_biome = list()
/// 2D list of all biomes based on heat and humidity combos. Associative by
/// `BIOME_X_HEAT` and then by `BIOME_X_HUMIDITY` (i.e.
/// `possible_biomes[BIOME_LOW_HEAT][BIOME_LOWMEDIUM_HUMIDITY]`).
/// Check /datum/map_generator/cave_generator/jungle for an example
/// of how to set it up properly.
var/list/possible_biomes = list()
/// Used to select "zoom" level into the perlin noise, higher numbers
/// result in slower transitions
var/perlin_zoom = 65

///Base chance of spawning a mob
var/mob_spawn_chance = 6
Expand All @@ -48,6 +67,7 @@
///How little neighbours does a alive cell need to die
var/death_limit = 3


/datum/map_generator/cave_generator/New()
. = ..()
if(!weighted_mob_spawn_list)
Expand Down Expand Up @@ -77,6 +97,9 @@
if(!(generate_in.area_flags & CAVES_ALLOWED))
return

if(length(possible_biomes))
return generate_terrain_with_biomes(turfs, generate_in)

var/start_time = REALTIMEOFDAY
string_gen = rustg_cnoise_generate("[initial_closed_chance]", "[smoothing_iterations]", "[birth_limit]", "[death_limit]", "[world.maxx]", "[world.maxy]") //Generate the raw CA data

Expand All @@ -99,8 +122,102 @@
if(gen_turf.turf_flags & NO_RUINS)
new_turf.turf_flags |= NO_RUINS

if(closed)//Open turfs have some special behavior related to spawning flora and mobs.
CHECK_TICK
var/message = "[name] terrain generation finished in [(REALTIMEOFDAY - start_time)/10]s!"
to_chat(world, span_boldannounce("[message]"))
log_world(message)


/**
* This proc handles including biomes in the cave generation. This is slower than
* `generate_terrain()`, so please use it only if you actually need biomes.
*
* This should only be called by `generate_terrain()`, if you have to call this,
* you're probably doing something wrong.
*/
/datum/map_generator/cave_generator/proc/generate_terrain_with_biomes(list/turfs, area/generate_in)
if(!(generate_in.area_flags & CAVES_ALLOWED))
return

var/humidity_seed = rand(0, 50000)
var/heat_seed = rand(0, 50000)

var/start_time = REALTIMEOFDAY
string_gen = rustg_cnoise_generate("[initial_closed_chance]", "[smoothing_iterations]", "[birth_limit]", "[death_limit]", "[world.maxx]", "[world.maxy]") //Generate the raw CA data

var/humidity_gen = list()
humidity_gen[BIOME_HIGH_HUMIDITY] = rustg_dbp_generate("[humidity_seed]", "60", "75", "[world.maxx]", "-0.1", "1.1")
humidity_gen[BIOME_MEDIUM_HUMIDITY] = rustg_dbp_generate("[humidity_seed]", "60", "75", "[world.maxx]", "-0.3", "-0.1")

var/heat_gen = list()
heat_gen[BIOME_HIGH_HEAT] = rustg_dbp_generate("[heat_seed]", "60", "75", "[world.maxx]", "-0.1", "1.1")
heat_gen[BIOME_MEDIUM_HEAT] = rustg_dbp_generate("[heat_seed]", "60", "75", "[world.maxx]", "-0.3", "-0.1")

var/list/expanded_closed_turfs = src.closed_turf_types
var/list/expanded_open_turfs = src.open_turf_types

for(var/turf/gen_turf as anything in turfs) //Go through all the turfs and generate them
var/closed = string_gen[world.maxx * (gen_turf.y - 1) + gen_turf.x] != "0"
var/new_turf_type = pick(closed ? expanded_closed_turfs : expanded_open_turfs)

var/datum/biome/selected_biome

// Here comes the meat of the biome code.
var/drift_x = clamp((gen_turf.x + rand(-BIOME_RANDOM_SQUARE_DRIFT, BIOME_RANDOM_SQUARE_DRIFT)), 1, world.maxx) // / perlin_zoom
var/drift_y = clamp((gen_turf.y + rand(-BIOME_RANDOM_SQUARE_DRIFT, BIOME_RANDOM_SQUARE_DRIFT)), 2, world.maxy) // / perlin_zoom

// Where we go in the generated string (generated outside of the loop for s p e e d)
var/coordinate = world.maxx * (drift_y - 1) + drift_x

// Type of humidity zone we're in (LOW-MEDIUM-HIGH)
var/humidity_level = text2num(humidity_gen[BIOME_HIGH_HUMIDITY][coordinate]) ? \
BIOME_HIGH_HUMIDITY : text2num(humidity_gen[BIOME_MEDIUM_HUMIDITY][coordinate]) ? BIOME_MEDIUM_HUMIDITY : BIOME_LOW_HUMIDITY
// Type of heat zone we're in (LOW-MEDIUM-HIGH)
var/heat_level = text2num(heat_gen[BIOME_HIGH_HEAT][coordinate]) ? \
BIOME_HIGH_HEAT : text2num(heat_gen[BIOME_MEDIUM_HEAT][coordinate]) ? BIOME_MEDIUM_HEAT : BIOME_LOW_HEAT

selected_biome = possible_biomes[heat_level][humidity_level]

// Currently, we only affect open turfs, because biomes don't currently
// have a definition for biome-specific closed turfs.
if((!length(biome_accepted_turfs) && !closed) || biome_accepted_turfs[new_turf_type])
LAZYADD(generated_turfs_per_biome[selected_biome], gen_turf)

else
// The assumption is this will be faster then changeturf, and changeturf isn't required since by this point
// The old tile hasn't got the chance to init yet
var/turf/new_turf = new new_turf_type(gen_turf)

if(gen_turf.turf_flags & NO_RUINS)
new_turf.turf_flags |= NO_RUINS

CHECK_TICK

for(var/biome in generated_turfs_per_biome)
var/datum/biome/generating_biome = SSmapping.biomes[biome]

var/list/turf/generated_turfs = generating_biome.generate_turfs_for_terrain(generated_turfs_per_biome[biome])

generated_turfs_per_biome[biome] = generated_turfs

var/message = "[name] terrain generation finished in [(REALTIMEOFDAY - start_time)/10]s!"
to_chat(world, span_boldannounce("[message]"))
log_world(message)


/datum/map_generator/cave_generator/populate_terrain(list/turfs, area/generate_in)
if(length(possible_biomes))
return populate_terrain_with_biomes(turfs, generate_in)

// Area var pullouts to make accessing in the loop faster
var/flora_allowed = (generate_in.area_flags & FLORA_ALLOWED) && length(flora_spawn_list)
var/feature_allowed = (generate_in.area_flags & FLORA_ALLOWED) && length(feature_spawn_list)
var/mobs_allowed = (generate_in.area_flags & MOB_SPAWN_ALLOWED) && length(mob_spawn_list)
var/megas_allowed = (generate_in.area_flags & MEGAFAUNA_SPAWN_ALLOWED) && length(megafauna_spawn_list)

var/start_time = REALTIMEOFDAY

for(var/turf/target_turf as anything in turfs)
if(!(target_turf.type in open_turf_types)) //only put stuff on open turfs we generated, so closed walls and rivers and stuff are skipped
continue

// If we've spawned something yet
Expand Down Expand Up @@ -170,3 +287,60 @@
var/message = "[name] finished in [(REALTIMEOFDAY - start_time)/10]s!"
to_chat(world, span_boldannounce("[message]"))
log_world(message)


/**
* This handles the population of terrain with biomes. Should only be called by
* `populate_terrain()`, if you find yourself calling this, you're probably not
* doing it right.
*
* This proc won't do anything if the area we're trying to generate in does not
* have `FLORA_ALLOWED` or `MOB_SPAWN_ALLOWED` in its `area_flags`.
*/
/datum/map_generator/cave_generator/proc/populate_terrain_with_biomes(list/turfs, area/generate_in)
// Area var pullouts to make accessing in the loop faster
var/flora_allowed = (generate_in.area_flags & FLORA_ALLOWED)
var/features_allowed = (generate_in.area_flags & FLORA_ALLOWED)
var/fauna_allowed = (generate_in.area_flags & MOB_SPAWN_ALLOWED)

var/start_time = REALTIMEOFDAY

// No sense in doing anything here if nothing is allowed anyway.
if(!flora_allowed && !features_allowed && !fauna_allowed)
var/message = "[name] terrain population finished in [(REALTIMEOFDAY - start_time)/10]s!"
to_chat(world, span_boldannounce("[message]"))
log_world(message)
return

for(var/biome in generated_turfs_per_biome)
var/datum/biome/generating_biome = SSmapping.biomes[biome]
generating_biome.populate_turfs(generated_turfs_per_biome[biome], flora_allowed, features_allowed, fauna_allowed)

CHECK_TICK

var/message = "[name] terrain population finished in [(REALTIMEOFDAY - start_time)/10]s!"
to_chat(world, span_boldannounce("[message]"))
log_world(message)


/datum/map_generator/cave_generator/jungle
possible_biomes = list(
BIOME_LOW_HEAT = list(
BIOME_LOW_HUMIDITY = /datum/biome/plains,
BIOME_MEDIUM_HUMIDITY = /datum/biome/mudlands,
BIOME_HIGH_HUMIDITY = /datum/biome/water
),
BIOME_MEDIUM_HEAT = list(
BIOME_LOW_HUMIDITY = /datum/biome/plains,
BIOME_MEDIUM_HUMIDITY = /datum/biome/jungle/deep,
BIOME_HIGH_HUMIDITY = /datum/biome/jungle
),
BIOME_HIGH_HEAT = list(
BIOME_LOW_HUMIDITY = /datum/biome/wasteland,
BIOME_MEDIUM_HUMIDITY = /datum/biome/plains,
BIOME_HIGH_HUMIDITY = /datum/biome/jungle/deep
)
)


#undef BIOME_RANDOM_SQUARE_DRIFT
Loading

0 comments on commit 0806a20

Please sign in to comment.