1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
use crate::{
sim::{SimChunk, WorldSim},
util::{seed_expan, Sampler, UnitChooser},
Canvas,
};
use common::{
generation::EntityInfo,
spot::{SpotCondition, SpotProperties, RON_SPOT_PROPERTIES},
terrain::{BiomeKind, Structure, TerrainChunkSize},
vol::RectVolSize,
};
use rand::prelude::*;
use rand_chacha::ChaChaRng;
use std::ops::Range;
use vek::*;
/// Spots are localised structures that spawn in the world. Conceptually, they
/// fit somewhere between the tree generator and the site generator: an attempt
/// to marry the simplicity of the former with the capability of the latter.
/// They are not globally visible to the game: this means that they do not
/// appear on the map, and cannot interact with rtsim (much).
///
/// To add a new spot, one must:
///
/// 1. Add a new variant to the [`Spot`] enum.
/// 2. Add a new entry to [`Spot::generate`] that tells the system where to
/// generate your new spot.
/// 3. Add a new arm to the `match` expression in [`Spot::apply_spots_to`] that
/// tells the generator how to generate a spot, including the base structure
/// that composes the spot and the entities that should be spawned there.
///
/// Only add spots with randomly spawned NPCs here. Spots that only use
/// EntitySpawner blocks can be added in assets/world/manifests/spots.ron
#[derive(Copy, Clone, Debug)]
pub enum Spot {
DwarvenGrave,
SaurokAltar,
MyrmidonTemple,
GnarlingTotem,
WitchHouse,
GnomeSpring,
WolfBurrow,
Igloo,
//BanditCamp,
//EnchantedRock,
//TowerRuin,
//WellOfLight,
//MerchantOutpost,
//RuinedHuntingCabin, <-- Bears!
// *Random world objects*
LionRock,
TreeStumpForest,
DesertBones,
Arch,
AirshipCrash,
FruitTree,
Shipwreck,
Shipwreck2,
FallenTree,
GraveSmall,
JungleTemple,
SaurokTotem,
JungleOutpost,
RonFile(&'static SpotProperties),
}
impl Spot {
pub fn generate(world: &mut WorldSim) {
use BiomeKind::*;
// Trees/spawn: false => *No* trees around the spot
// Themed Spots -> Act as an introduction to themes of sites
for s in RON_SPOT_PROPERTIES.0.iter() {
Self::generate_spots(
Spot::RonFile(s),
world,
s.freq,
|g, c| is_valid(&s.condition, g, c),
s.spawn,
);
}
Self::generate_spots(
Spot::WitchHouse,
world,
1.0,
|g, c| {
g < 0.25
&& !c.near_cliffs()
&& !c.river.near_water()
&& !c.path.0.is_way()
&& c.sites.is_empty()
&& matches!(
c.get_biome(),
Grassland | Forest | Taiga | Snowland | Jungle
)
},
false,
);
Self::generate_spots(
Spot::Igloo,
world,
2.0,
|g, c| {
g < 0.5
&& !c.near_cliffs()
&& !c.river.near_water()
&& !c.path.0.is_way()
&& c.sites.is_empty()
&& matches!(c.get_biome(), Snowland)
},
false,
);
Self::generate_spots(
Spot::SaurokAltar,
world,
1.0,
|g, c| {
g < 0.25
&& !c.near_cliffs()
&& !c.river.near_water()
&& !c.path.0.is_way()
&& c.sites.is_empty()
&& matches!(c.get_biome(), Jungle | Forest)
},
false,
);
Self::generate_spots(
Spot::SaurokTotem,
world,
1.0,
|g, c| {
g < 0.25
&& !c.near_cliffs()
&& !c.river.near_water()
&& !c.path.0.is_way()
&& c.sites.is_empty()
&& matches!(c.get_biome(), Jungle | Forest)
},
false,
);
Self::generate_spots(
Spot::JungleOutpost,
world,
1.0,
|g, c| {
g < 0.25
&& !c.near_cliffs()
&& !c.river.near_water()
&& !c.path.0.is_way()
&& c.sites.is_empty()
&& matches!(c.get_biome(), Jungle | Forest)
},
false,
);
Self::generate_spots(
Spot::JungleTemple,
world,
0.5,
|g, c| {
g < 0.25
&& !c.near_cliffs()
&& !c.river.near_water()
&& !c.path.0.is_way()
&& c.sites.is_empty()
&& matches!(c.get_biome(), Jungle | Forest)
},
false,
);
Self::generate_spots(
Spot::MyrmidonTemple,
world,
1.0,
|g, c| {
g < 0.1
&& !c.near_cliffs()
&& !c.river.near_water()
&& !c.path.0.is_way()
&& c.sites.is_empty()
&& matches!(c.get_biome(), Desert | Jungle)
},
false,
);
Self::generate_spots(
Spot::GnarlingTotem,
world,
1.5,
|g, c| {
g < 0.25
&& !c.near_cliffs()
&& !c.river.near_water()
&& !c.path.0.is_way()
&& c.sites.is_empty()
&& matches!(c.get_biome(), Forest | Grassland)
},
false,
);
Self::generate_spots(
Spot::FallenTree,
world,
1.5,
|g, c| {
g < 0.25
&& !c.near_cliffs()
&& !c.river.near_water()
&& !c.path.0.is_way()
&& c.sites.is_empty()
&& matches!(c.get_biome(), Forest | Grassland)
},
false,
);
// Random World Objects -> Themed to their Biome and the NPCs that regularly
// spawn there
Self::generate_spots(
Spot::LionRock,
world,
1.5,
|g, c| {
g < 0.25
&& !c.near_cliffs()
&& !c.river.near_water()
&& !c.path.0.is_way()
&& c.sites.is_empty()
&& matches!(c.get_biome(), Savannah)
},
false,
);
Self::generate_spots(
Spot::WolfBurrow,
world,
1.5,
|g, c| {
g < 0.25
&& !c.near_cliffs()
&& !c.river.near_water()
&& !c.path.0.is_way()
&& c.sites.is_empty()
&& matches!(c.get_biome(), Forest | Grassland)
},
false,
);
Self::generate_spots(
Spot::TreeStumpForest,
world,
20.0,
|g, c| {
g < 0.25
&& !c.near_cliffs()
&& !c.river.near_water()
&& !c.path.0.is_way()
&& c.sites.is_empty()
&& matches!(c.get_biome(), Jungle | Forest)
},
true,
);
Self::generate_spots(
Spot::DesertBones,
world,
6.0,
|g, c| {
g < 0.25
&& !c.near_cliffs()
&& !c.river.near_water()
&& !c.path.0.is_way()
&& c.sites.is_empty()
&& matches!(c.get_biome(), Desert)
},
false,
);
Self::generate_spots(
Spot::Arch,
world,
2.0,
|g, c| {
g < 0.25
&& !c.near_cliffs()
&& !c.river.near_water()
&& !c.path.0.is_way()
&& c.sites.is_empty()
&& matches!(c.get_biome(), Desert)
},
false,
);
Self::generate_spots(
Spot::AirshipCrash,
world,
0.7,
|g, c| {
g < 0.25
&& !c.near_cliffs()
&& !c.river.near_water()
&& !c.path.0.is_way()
&& c.sites.is_empty()
&& !matches!(c.get_biome(), Mountain | Void | Ocean)
},
false,
);
Self::generate_spots(
Spot::FruitTree,
world,
20.0,
|g, c| {
g < 0.25
&& !c.near_cliffs()
&& !c.river.near_water()
&& !c.path.0.is_way()
&& c.sites.is_empty()
&& matches!(c.get_biome(), Forest)
},
true,
);
Self::generate_spots(
Spot::GnomeSpring,
world,
1.0,
|g, c| {
g < 0.25
&& !c.near_cliffs()
&& !c.river.near_water()
&& !c.path.0.is_way()
&& c.sites.is_empty()
&& matches!(c.get_biome(), Forest)
},
false,
);
Self::generate_spots(
Spot::Shipwreck,
world,
1.0,
|g, c| {
g < 0.25 && c.is_underwater() && c.sites.is_empty() && c.water_alt > c.alt + 30.0
},
true,
);
Self::generate_spots(
Spot::Shipwreck2,
world,
1.0,
|g, c| {
g < 0.25 && c.is_underwater() && c.sites.is_empty() && c.water_alt > c.alt + 30.0
},
true,
);
// Small Grave
Self::generate_spots(
Spot::GraveSmall,
world,
2.0,
|g, c| {
g < 0.25
&& !c.near_cliffs()
&& !c.river.near_water()
&& !c.path.0.is_way()
&& c.sites.is_empty()
&& matches!(c.get_biome(), Forest | Taiga | Jungle | Grassland)
},
false,
);
// Missing:
/*
Bandit Camp
Hunter Camp
TowerRuinForest
TowerRuinDesert
WellOfLight
Merchant Outpost -> Near a road!
*Quirky:*
TreeHouse (Forest)
EnchantedRock (Forest, Jungle)
*/
}
fn generate_spots(
// What kind of spot are we generating?
spot: Spot,
world: &mut WorldSim,
// How often should this spot appear (per square km, on average)?
freq: f32,
// What tests should we perform to see whether we can spawn the spot here? The two
// parameters are the gradient of the terrain and the [`SimChunk`] of the candidate
// location.
mut valid: impl FnMut(f32, &SimChunk) -> bool,
// Should we allow trees and other trivial structures to spawn close to the spot?
spawn: bool,
) {
let world_size = world.get_size();
for _ in
0..(world_size.product() as f32 * TerrainChunkSize::RECT_SIZE.product() as f32 * freq
/ 1000.0f32.powi(2))
.ceil() as u64
{
let pos = world_size.map(|e| (world.rng.gen_range(0..e) & !0b11) as i32);
if let Some((_, chunk)) = world
.get_gradient_approx(pos)
.zip(world.get_mut(pos))
.filter(|(grad, chunk)| valid(*grad, chunk))
{
chunk.spot = Some(spot);
if !spawn {
chunk.tree_density = 0.0;
chunk.spawn_rate = 0.0;
}
}
}
}
}
pub fn apply_spots_to(canvas: &mut Canvas, _dynamic_rng: &mut impl Rng) {
let nearby_spots = canvas.nearby_spots().collect::<Vec<_>>();
for (spot_wpos2d, spot, seed) in nearby_spots.iter().copied() {
let mut rng = ChaChaRng::from_seed(seed_expan::rng_state(seed));
let units = UnitChooser::new(seed).get(seed).into();
#[derive(Default)]
struct SpotConfig<'a> {
// The manifest containing a list of possible base structures for the spot (one will be
// chosen)
base_structures: Option<&'a str>,
// The maximum distance from the centre of the spot that entities will spawn
entity_radius: f32,
// The entities that should be spawned in the spot, from closest to furthest
// (count_range, spec)
// count_range = number of entities, chosen randomly within this range (not inclusive!)
// spec = Manifest spec for the entity kind
entities: &'a [(Range<i32>, &'a str)],
}
let spot_config = match spot {
// Themed Spots
Spot::DwarvenGrave => SpotConfig {
base_structures: Some("spots_grasslands.dwarven_grave"),
entity_radius: 60.0,
entities: &[(6..12, "common.entity.spot.dwarf_grave_robber")],
},
Spot::SaurokAltar => SpotConfig {
base_structures: Some("spots.jungle.saurok-altar"),
entity_radius: 12.0,
entities: &[
(0..3, "common.entity.wild.aggressive.occult_saurok"),
(0..3, "common.entity.wild.aggressive.sly_saurok"),
(0..3, "common.entity.wild.aggressive.mighty_saurok"),
],
},
Spot::SaurokTotem => SpotConfig {
base_structures: Some("spots.jungle.saurok_totem"),
entity_radius: 20.0,
entities: &[
(0..3, "common.entity.wild.aggressive.occult_saurok"),
(0..3, "common.entity.wild.aggressive.sly_saurok"),
(0..3, "common.entity.wild.aggressive.mighty_saurok"),
],
},
Spot::JungleOutpost => SpotConfig {
base_structures: Some("spots.jungle.outpost"),
entity_radius: 40.0,
entities: &[(6..12, "common.entity.spot.grim_salvager")],
},
Spot::JungleTemple => SpotConfig {
base_structures: Some("spots.jungle.temple_small"),
entity_radius: 40.0,
entities: &[
(2..8, "common.entity.wild.aggressive.occult_saurok"),
(2..8, "common.entity.wild.aggressive.sly_saurok"),
(2..8, "common.entity.wild.aggressive.mighty_saurok"),
],
},
Spot::MyrmidonTemple => SpotConfig {
base_structures: Some("spots.myrmidon-temple"),
entity_radius: 10.0,
entities: &[
(3..5, "common.entity.dungeon.myrmidon.hoplite"),
(3..5, "common.entity.dungeon.myrmidon.strategian"),
(2..3, "common.entity.dungeon.myrmidon.marksman"),
],
},
Spot::WitchHouse => SpotConfig {
base_structures: Some("spots_general.witch_hut"),
entity_radius: 1.0,
entities: &[
(1..2, "common.entity.spot.witch_dark"),
(0..4, "common.entity.wild.peaceful.cat"),
(0..3, "common.entity.wild.peaceful.frog"),
],
},
Spot::Igloo => SpotConfig {
base_structures: Some("spots_general.igloo"),
entity_radius: 2.0,
entities: &[
(3..5, "common.entity.dungeon.adlet.hunter"),
(3..5, "common.entity.dungeon.adlet.icepicker"),
(2..3, "common.entity.dungeon.adlet.tracker"),
],
},
Spot::GnarlingTotem => SpotConfig {
base_structures: Some("site_structures.gnarling.totem"),
entity_radius: 30.0,
entities: &[
(3..5, "common.entity.dungeon.gnarling.mugger"),
(3..5, "common.entity.dungeon.gnarling.stalker"),
(3..5, "common.entity.dungeon.gnarling.logger"),
(2..4, "common.entity.dungeon.gnarling.mandragora"),
(1..3, "common.entity.wild.aggressive.deadwood"),
(1..2, "common.entity.dungeon.gnarling.woodgolem"),
],
},
Spot::FallenTree => SpotConfig {
base_structures: Some("spots_grasslands.fallen_tree"),
entity_radius: 64.0,
entities: &[
(1..2, "common.entity.dungeon.gnarling.mandragora"),
(2..6, "common.entity.wild.aggressive.deadwood"),
(0..2, "common.entity.wild.aggressive.mossdrake"),
],
},
// Random World Objects
Spot::LionRock => SpotConfig {
base_structures: Some("spots_savannah.lion_rock"),
entity_radius: 30.0,
entities: &[
(5..10, "common.entity.spot.female_lion"),
(1..2, "common.entity.wild.aggressive.male_lion"),
],
},
Spot::WolfBurrow => SpotConfig {
base_structures: Some("spots_savannah.wolf_burrow"),
entity_radius: 10.0,
entities: &[(5..8, "common.entity.wild.aggressive.wolf")],
},
Spot::TreeStumpForest => SpotConfig {
base_structures: Some("trees.oak_stumps"),
entity_radius: 30.0,
entities: &[(0..2, "common.entity.wild.aggressive.deadwood")],
},
Spot::DesertBones => SpotConfig {
base_structures: Some("spots.bones"),
entity_radius: 40.0,
entities: &[(4..9, "common.entity.wild.aggressive.hyena")],
},
Spot::Arch => SpotConfig {
base_structures: Some("spots.arch"),
entity_radius: 50.0,
entities: &[],
},
Spot::AirshipCrash => SpotConfig {
base_structures: Some("trees.airship_crash"),
entity_radius: 20.0,
entities: &[(4..9, "common.entity.spot.grim_salvager")],
},
Spot::FruitTree => SpotConfig {
base_structures: Some("trees.fruit_trees"),
entity_radius: 2.0,
entities: &[(0..2, "common.entity.wild.peaceful.bear")],
},
Spot::GnomeSpring => SpotConfig {
base_structures: Some("spots.gnome_spring"),
entity_radius: 40.0,
entities: &[(7..10, "common.entity.spot.gnome.spear")],
},
Spot::Shipwreck => SpotConfig {
base_structures: Some("spots.water.shipwreck"),
entity_radius: 2.0,
entities: &[(0..2, "common.entity.wild.peaceful.clownfish")],
},
Spot::Shipwreck2 => SpotConfig {
base_structures: Some("spots.water.shipwreck2"),
entity_radius: 20.0,
entities: &[(0..3, "common.entity.wild.peaceful.clownfish")],
},
Spot::GraveSmall => SpotConfig {
base_structures: Some("spots.grave_small"),
entity_radius: 2.0,
entities: &[],
},
Spot::RonFile(properties) => SpotConfig {
base_structures: Some(&properties.base_structures),
entity_radius: 1.0,
entities: &[],
},
};
// Blit base structure
if let Some(base_structures) = spot_config.base_structures {
let structures = Structure::load_group(base_structures).read();
let structure = structures.choose(&mut rng).unwrap();
let origin = spot_wpos2d.with_z(
canvas
.col_or_gen(spot_wpos2d)
.map(|c| c.alt as i32)
.unwrap_or(0),
);
canvas.blit_structure(origin, structure, seed, units, true);
}
// Spawn entities
const PHI: f32 = 1.618;
for (spawn_count, spec) in spot_config.entities {
let spawn_count = rng.gen_range(spawn_count.clone()).max(0);
let dir_offset = rng.gen::<f32>();
for i in 0..spawn_count {
let dir = Vec2::new(
((dir_offset + i as f32 * PHI) * std::f32::consts::TAU).sin(),
((dir_offset + i as f32 * PHI) * std::f32::consts::TAU).cos(),
);
let dist = i as f32 / spawn_count as f32 * spot_config.entity_radius;
let wpos2d = spot_wpos2d + (dir * dist).map(|e| e.round() as i32);
let alt = canvas.col_or_gen(wpos2d).map(|c| c.alt as i32).unwrap_or(0);
if let Some(wpos) = canvas
.area()
.contains_point(wpos2d)
.then(|| canvas.find_spawn_pos(wpos2d.with_z(alt)))
.flatten()
{
canvas.spawn(
EntityInfo::at(wpos.map(|e| e as f32) + Vec3::new(0.5, 0.5, 0.0))
.with_asset_expect(spec, &mut rng, None),
);
}
}
}
}
}
pub fn is_valid(condition: &SpotCondition, g: f32, c: &SimChunk) -> bool {
c.sites.is_empty()
&& match condition {
SpotCondition::MaxGradient(value) => g < *value,
SpotCondition::Biome(biomes) => biomes.contains(&c.get_biome()),
SpotCondition::NearCliffs => c.near_cliffs(),
SpotCondition::NearRiver => c.river.near_water(),
SpotCondition::IsWay => c.path.0.is_way(),
SpotCondition::IsUnderwater => c.is_underwater(),
SpotCondition::Typical => {
!c.near_cliffs() && !c.river.near_water() && !c.path.0.is_way()
},
SpotCondition::MinWaterDepth(depth) => {
is_valid(&SpotCondition::IsUnderwater, g, c) && c.water_alt > c.alt + depth
},
SpotCondition::Not(condition) => !is_valid(condition, g, c),
SpotCondition::All(conditions) => conditions.iter().all(|cond| is_valid(cond, g, c)),
SpotCondition::Any(conditions) => conditions.iter().any(|cond| is_valid(cond, g, c)),
}
}