Files
Bubberstation/code/datums/mapgen/biomes/_biome.dm
SkyratBot 23095a406a [MIRROR] Adds Biomes to the Cave Generator, for all of your procedurally-placed cave biome needs! (#27996)
* Adds Biomes to the Cave Generator, for all of your procedurally-placed cave biome needs! (#83138)

## About The Pull Request
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.

## Why It's Good For The Game
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)

## Changelog

🆑 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.
/🆑

* Adds Biomes to the Cave Generator, for all of your procedurally-placed cave biome needs!

---------

Co-authored-by: GoldenAlpharex <58045821+GoldenAlpharex@users.noreply.github.com>
2024-06-06 18:19:22 +05:30

233 lines
7.6 KiB
Plaintext

///This datum handles the transitioning from a turf to a specific biome, and handles spawning decorative structures and mobs.
/datum/biome
///Type of turf this biome creates
var/turf_type
/// Chance of having a structure from the flora types list spawn
var/flora_density = 0
/// Chance of spawning special features, such as geysers.
var/feature_density = 0
/// Chance of having a mob from the fauna types list spawn
var/fauna_density = 0
/// Weighted list of type paths of flora that can be spawned when the
/// turf spawns flora.
var/list/flora_types = list()
/// Weighted list of extra features that can spawn in the biome, such as
/// geysers. Gets expanded automatically.
var/list/feature_types = list()
/// Weighted list of type paths of fauna that can be spawned when the
/// turf spawns fauna.
var/list/fauna_types = list()
/datum/biome/New()
. = ..()
if(length(flora_types))
flora_types = expand_weights(fill_with_ones(flora_types))
if(length(fauna_types))
fauna_types = expand_weights(fill_with_ones(fauna_types))
if(length(feature_types))
feature_types = expand_weights(feature_types)
///This proc handles the creation of a turf of a specific biome type
/datum/biome/proc/generate_turf(turf/gen_turf)
gen_turf.ChangeTurf(turf_type, null, CHANGETURF_DEFER_CHANGE)
if(length(flora_types) && prob(flora_density))
var/obj/structure/flora = pick(flora_types)
new flora(gen_turf)
return
if(length(feature_types) && prob(feature_density))
var/atom/picked_feature = pick(feature_types)
new picked_feature(gen_turf)
return
if(length(fauna_types) && prob(fauna_density))
var/mob/fauna = pick(fauna_types)
new fauna(gen_turf)
/// This proc handles the creation of a turf of a specific biome type, assuming
/// that the turf has not been initialized yet. Don't call this unless you know
/// what you're doing.
/datum/biome/proc/generate_turf_for_terrain(turf/gen_turf)
var/turf/new_turf = new turf_type(gen_turf)
return new_turf
/**
* This proc handles the sequential creation of turfs of a specific biome type
* in order to optimize the generation for large amount of turfs.
*
* Arguments:
* * gen_turfs - List of turfs to use for turf generation.
*
* Returns a new list of turfs that were generated by the biome.
*/
/datum/biome/proc/generate_turfs_for_terrain(list/turf/gen_turfs)
var/list/turf/new_turfs = list()
for(var/turf/gen_turf as anything in gen_turfs)
var/turf/new_turf = new turf_type(gen_turf)
new_turfs += new_turf
if(gen_turf.turf_flags & NO_RUINS)
new_turf.turf_flags |= NO_RUINS
CHECK_TICK
return new_turfs
/// This proc handles populating the given turf based on whether flora,
/// features and fauna are allowed. Does not take megafauna into account.
/datum/biome/proc/populate_turf(turf/target_turf, flora_allowed, features_allowed, fauna_allowed)
if(flora_allowed && length(flora_types) && prob(flora_density))
var/obj/structure/flora = pick(flora_types)
new flora(target_turf)
return TRUE
if(features_allowed && prob(feature_density))
var/can_spawn = TRUE
var/atom/picked_feature = pick(feature_types)
for(var/obj/structure/existing_feature in range(7, target_turf))
if(istype(existing_feature, picked_feature))
can_spawn = FALSE
break
if(can_spawn)
new picked_feature(target_turf)
return TRUE
if(fauna_allowed && length(fauna_types) && prob(fauna_density))
var/mob/picked_mob = pick(fauna_types)
// prevents tendrils spawning in each other's collapse range
if(ispath(picked_mob, /obj/structure/spawner/lavaland))
for(var/obj/structure/spawner/lavaland/spawn_blocker in range(2, target_turf))
return FALSE
// if the random is not a tendril (hopefully meaning it is a mob), avoid spawning if there's another one within 12 tiles
else
var/list/things_in_range = range(12, target_turf)
for(var/mob/living/mob_blocker in things_in_range)
if(ismining(mob_blocker))
return FALSE
new picked_mob(target_turf)
return TRUE
return FALSE
/**
* This proc handles populating the given turfs based on whether flora, features
* and fauna are allowed. Does not take megafauna into account.
*
* Does nothing if `flora_allowed`, `features_allowed` and `fauna_allowed` are
* `FALSE`, or if there's no flora, feature or fauna types for the matching
* allowed type. Aka, we return early if the proc wouldn't do anything anyway.
*/
/datum/biome/proc/populate_turfs(list/turf/target_turfs, flora_allowed, features_allowed, fauna_allowed)
if(!(flora_allowed && length(flora_types)) && !(features_allowed && length(feature_types)) && !(fauna_allowed && length(fauna_types)))
return
for(var/turf/target_turf as anything in target_turfs)
// We do the CHECK_TICK here because there's a bunch of continue calls
// in this.
CHECK_TICK
if(flora_allowed && length(flora_types) && prob(flora_density))
var/obj/structure/flora = pick(flora_types)
new flora(target_turf)
continue
if(features_allowed && prob(feature_density))
var/can_spawn = TRUE
var/atom/picked_feature = pick(feature_types)
for(var/obj/structure/existing_feature in range(7, target_turf))
if(istype(existing_feature, picked_feature))
can_spawn = FALSE
break
if(can_spawn)
new picked_feature(target_turf)
continue
if(fauna_allowed && length(fauna_types) && prob(fauna_density))
var/mob/picked_mob = pick(fauna_types)
// prevents tendrils spawning in each other's collapse range
if(ispath(picked_mob, /obj/structure/spawner/lavaland))
for(var/obj/structure/spawner/lavaland/spawn_blocker in range(2, target_turf))
continue
// if the random is not a tendril (hopefully meaning it is a mob), avoid spawning if there's another one within 12 tiles
else
var/list/things_in_range = range(12, target_turf)
for(var/mob/living/mob_blocker in things_in_range)
if(ismining(mob_blocker))
continue
new picked_mob(target_turf)
/datum/biome/mudlands
turf_type = /turf/open/misc/dirt/jungle/dark
flora_types = list(
/obj/structure/flora/grass/jungle/a/style_random = 1,
/obj/structure/flora/grass/jungle/b/style_random = 1,
/obj/structure/flora/rock/pile/jungle/style_random = 1,
/obj/structure/flora/rock/pile/jungle/large/style_random = 1,
)
flora_density = 3
/datum/biome/plains
turf_type = /turf/open/misc/grass/jungle
flora_types = list(
/obj/structure/flora/grass/jungle/a/style_random = 1,
/obj/structure/flora/grass/jungle/b/style_random = 1,
/obj/structure/flora/tree/jungle/style_random = 1,
/obj/structure/flora/rock/pile/jungle/style_random = 1,
/obj/structure/flora/bush/jungle/a/style_random = 1,
/obj/structure/flora/bush/jungle/b/style_random = 1,
/obj/structure/flora/bush/jungle/c/style_random = 1,
/obj/structure/flora/bush/large/style_random = 1,
/obj/structure/flora/rock/pile/jungle/large/style_random = 1,
)
flora_density = 15
/datum/biome/jungle
turf_type = /turf/open/misc/grass/jungle
flora_types = list(
/obj/structure/flora/grass/jungle/a/style_random = 1,
/obj/structure/flora/grass/jungle/b/style_random = 1,
/obj/structure/flora/tree/jungle/style_random = 1,
/obj/structure/flora/rock/pile/jungle/style_random = 1,
/obj/structure/flora/bush/jungle/a/style_random = 1,
/obj/structure/flora/bush/jungle/b/style_random = 1,
/obj/structure/flora/bush/jungle/c/style_random = 1,
/obj/structure/flora/bush/large/style_random = 1,
/obj/structure/flora/rock/pile/jungle/large/style_random = 1,
)
flora_density = 40
/datum/biome/jungle/deep
flora_density = 65
/datum/biome/wasteland
turf_type = /turf/open/misc/dirt/jungle/wasteland
/datum/biome/water
turf_type = /turf/open/water/jungle
/datum/biome/mountain
turf_type = /turf/closed/mineral/random/jungle