starting site tweaks

This commit is contained in:
crabman 2024-02-24 17:51:35 +01:00
parent 69e0653683
commit b2e38d7e6d
No known key found for this signature in database
5 changed files with 128 additions and 62 deletions

View File

@ -17,3 +17,37 @@ pub enum BiomeKind {
Savannah,
Taiga,
}
impl BiomeKind {
/// Roughly represents the difficulty of a biome (value between 1 and 5)
pub fn difficulty(&self) -> i32 {
match self {
BiomeKind::Void => 1,
BiomeKind::Lake => 1,
BiomeKind::Grassland => 2,
BiomeKind::Ocean => 1,
BiomeKind::Mountain => 1,
BiomeKind::Snowland => 2,
BiomeKind::Desert => 5,
BiomeKind::Swamp => 2,
BiomeKind::Jungle => 3,
BiomeKind::Forest => 1,
BiomeKind::Savannah => 2,
BiomeKind::Taiga => 2,
}
}
}
#[cfg(test)]
#[test]
fn test_biome_difficulty() {
use strum::IntoEnumIterator;
for biome_kind in BiomeKind::iter() {
assert!(
(1..=5).contains(&biome_kind.difficulty()),
"Biome {biome_kind:?} has invalid difficulty {}",
biome_kind.difficulty()
);
}
}

View File

@ -316,17 +316,7 @@ impl Data {
// Try a few times to find a location that's not underwater
if let Some((wpos, chunk)) = (0..100)
.map(|_| world.sim().get_size().map(|sz| rng.gen_range(0..sz as i32)))
.find_map(|pos| {
Some((
pos,
world
.sim()
.get(pos)
// This is currently a workaround to force Frost Gigas spawning in cold areas
// TODO: Once more Gigas are implemented remove this
.filter(|c| !c.is_underwater() && c.temp < CONFIG.snow_temp)?,
))
})
.find_map(|pos| Some((pos, world.sim().get(pos).filter(|c| !c.is_underwater())?)))
.map(|(pos, chunk)| {
let wpos2d = pos.cpos_to_wpos_center();
(

View File

@ -32,6 +32,7 @@ use common::{
};
use common_net::msg::world_msg::{SiteId, SiteInfo};
use i18n::{Localization, LocalizationHandle};
use rand::{thread_rng, Rng};
//ImageFrame, Tooltip,
use crate::settings::Settings;
//use std::time::Duration;
@ -194,7 +195,7 @@ enum Mode {
/// mode as opposed to create mode.
// TODO: Something less janky? Express the problem domain better!
character_id: Option<CharacterId>,
start_site_idx: usize,
start_site_idx: Option<usize>,
},
}
@ -248,7 +249,7 @@ impl Mode {
prev_starting_site_button: Default::default(),
next_starting_site_button: Default::default(),
character_id: None,
start_site_idx: 0,
start_site_idx: None,
}
}
@ -278,7 +279,7 @@ impl Mode {
prev_starting_site_button: Default::default(),
next_starting_site_button: Default::default(),
character_id: Some(character_id),
start_site_idx: 0,
start_site_idx: None,
}
}
}
@ -1362,10 +1363,12 @@ impl Controls {
//TODO: Add text-outline here whenever we updated iced to a version supporting
// this
let map = if let Some(info) = self.possible_starting_sites.get(*start_site_idx)
let map = if let Some(info) = self
.possible_starting_sites
.get(start_site_idx.unwrap_or_default())
{
let site_name = Text::new(
self.possible_starting_sites[*start_site_idx]
self.possible_starting_sites[start_site_idx.unwrap_or_default()]
.name
.as_deref()
.unwrap_or("Unknown"),
@ -1405,12 +1408,16 @@ impl Controls {
if self.possible_starting_sites.is_empty() {
vec![map]
} else {
let selected = start_site_idx.get_or_insert_with(|| {
thread_rng().gen_range(0..self.possible_starting_sites.len())
});
let site_slider = starter_slider(
i18n.get_msg("char_selection-starting_site").into_owned(),
30,
&mut sliders.starting_site,
self.possible_starting_sites.len() as u32 - 1,
*start_site_idx as u32,
*selected as u32,
|x| Message::StartingSite(x as usize),
imgs,
);
@ -1801,7 +1808,7 @@ impl Controls {
body: comp::Body::Humanoid(*body),
start_site: self
.possible_starting_sites
.get(*start_site_idx)
.get(start_site_idx.unwrap_or_default())
.map(|info| info.id),
});
self.mode = Mode::select(Some(InfoContent::CreatingCharacter));
@ -1863,7 +1870,6 @@ impl Controls {
//Todo: Add species and body type to randomization.
Message::RandomizeCharacter => {
if let Mode::CreateOrEdit { body, .. } = &mut self.mode {
use rand::Rng;
let body_type = body.body_type;
let species = body.species;
let mut rng = rand::thread_rng();
@ -1930,24 +1936,30 @@ impl Controls {
},
Message::StartingSite(idx) => {
if let Mode::CreateOrEdit { start_site_idx, .. } = &mut self.mode {
*start_site_idx = idx;
*start_site_idx = Some(idx);
}
},
Message::PrevStartingSite => {
if let Mode::CreateOrEdit { start_site_idx, .. } = &mut self.mode {
if !self.possible_starting_sites.is_empty() {
*start_site_idx = (*start_site_idx + self.possible_starting_sites.len()
*start_site_idx = Some(
(start_site_idx.unwrap_or_default()
+ self.possible_starting_sites.len()
- 1)
% self.possible_starting_sites.len();
% self.possible_starting_sites.len(),
);
}
}
},
Message::NextStartingSite => {
if let Mode::CreateOrEdit { start_site_idx, .. } = &mut self.mode {
if !self.possible_starting_sites.is_empty() {
*start_site_idx =
(*start_site_idx + self.possible_starting_sites.len() + 1)
% self.possible_starting_sites.len();
*start_site_idx = Some(
(start_site_idx.unwrap_or_default()
+ self.possible_starting_sites.len()
+ 1)
% self.possible_starting_sites.len(),
);
}
}
},

View File

@ -278,7 +278,7 @@ impl Civs {
let world_dims = ctx.sim.get_aabr();
for _ in 0..initial_civ_count * 3 {
attempt(5, || {
let (loc, kind) = match ctx.rng.gen_range(0..115) {
let (loc, kind) = match ctx.rng.gen_range(0..90) {
0..=4 => {
if index.features().site2_giant_trees {
(
@ -304,7 +304,7 @@ impl Civs {
)
}
},
5..=10 => (
5..=15 => (
find_site_loc(
&mut ctx,
&ProximityRequirementsBuilder::new()
@ -314,7 +314,7 @@ impl Civs {
)?,
SiteKind::Gnarling,
),
11..=16 => (
16..=20 => (
find_site_loc(
&mut ctx,
&ProximityRequirementsBuilder::new()
@ -324,7 +324,7 @@ impl Civs {
)?,
SiteKind::ChapelSite,
),
17..=22 => (
21..=27 => (
find_site_loc(
&mut ctx,
&ProximityRequirementsBuilder::new()
@ -334,7 +334,7 @@ impl Civs {
)?,
SiteKind::Adlet,
),
23..=35 => (
28..=38 => (
find_site_loc(
&mut ctx,
&ProximityRequirementsBuilder::new()
@ -344,7 +344,7 @@ impl Civs {
)?,
SiteKind::PirateHideout,
),
36..=42 => (
39..=45 => (
find_site_loc(
&mut ctx,
&ProximityRequirementsBuilder::new()
@ -354,7 +354,7 @@ impl Civs {
)?,
SiteKind::JungleRuin,
),
43..=49 => (
46..=55 => (
find_site_loc(
&mut ctx,
&ProximityRequirementsBuilder::new()
@ -364,7 +364,7 @@ impl Civs {
)?,
SiteKind::RockCircle,
),
50..=59 => (
56..=66 => (
find_site_loc(
&mut ctx,
&ProximityRequirementsBuilder::new()
@ -374,7 +374,7 @@ impl Civs {
)?,
SiteKind::TrollCave,
),
60..=69 => (
67..=72 => (
find_site_loc(
&mut ctx,
&ProximityRequirementsBuilder::new()
@ -384,7 +384,7 @@ impl Civs {
)?,
SiteKind::Camp,
),
70..=74 => (
73..=76 => (
find_site_loc(
&mut ctx,
&ProximityRequirementsBuilder::new()
@ -394,7 +394,7 @@ impl Civs {
)?,
SiteKind::Haniwa,
),
75..=85 => (
77..=79 => (
find_site_loc(
&mut ctx,
&ProximityRequirementsBuilder::new()
@ -404,7 +404,7 @@ impl Civs {
)?,
SiteKind::Terracotta,
),
/*86..=91 => (
/*80..=86 => (
find_site_loc(
&mut ctx,
&ProximityRequirementsBuilder::new()
@ -414,7 +414,7 @@ impl Civs {
)?,
SiteKind::DwarvenMine,
),
92..=97 => (
86..=97 => (
find_site_loc(
&mut ctx,
&ProximityRequirementsBuilder::new()
@ -945,10 +945,10 @@ impl Civs {
fn birth_civ(&mut self, ctx: &mut GenCtx<impl Rng>) -> Option<Id<Civ>> {
// TODO: specify SiteKind based on where a suitable location is found
let kind = match ctx.rng.gen_range(0..64) {
0..=10 => SiteKind::CliffTown,
11..=12 => SiteKind::DesertCity,
13..=18 => SiteKind::SavannahPit,
19..=36 => SiteKind::CoastalTown,
0..=8 => SiteKind::CliffTown,
9..=17 => SiteKind::DesertCity,
18..=23 => SiteKind::SavannahPit,
24..=33 => SiteKind::CoastalTown,
_ => SiteKind::Refactor,
};
let world_dims = ctx.sim.get_aabr();
@ -1558,11 +1558,7 @@ impl Civs {
}
fn tree_enemies(&self) -> impl Iterator<Item = Vec2<i32>> + '_ {
self.sites().filter_map(|s| match s.kind {
SiteKind::Castle => Some(s.center),
_ if s.is_settlement() => Some(s.center),
_ => None,
})
self.sites().map(|s| s.center)
}
fn castle_enemies(&self) -> impl Iterator<Item = Vec2<i32>> + '_ {

View File

@ -36,7 +36,7 @@ pub use block::BlockGen;
use civ::WorldCivStage;
pub use column::ColumnSample;
pub use common::terrain::site::{DungeonKindMeta, SettlementKindMeta};
use common::terrain::CoordinateConversions;
use common::{spiral::Spiral2d, terrain::CoordinateConversions};
pub use index::{IndexOwned, IndexRef};
use sim::WorldSimStage;
@ -249,7 +249,7 @@ impl World {
}))
.collect(),
possible_starting_sites: {
const STARTING_SITE_COUNT: usize = 4;
const STARTING_SITE_COUNT: usize = 5;
let mut candidates = self
.civs()
@ -258,21 +258,55 @@ impl World {
.filter_map(|(_, civ_site)| Some((civ_site, civ_site.site_tmp?)))
.map(|(civ_site, site_id)| {
// Score the site according to how suitable it is to be a starting site
let mut score = 0.0;
if let SiteKind::Refactor(site2) = &index.sites[site_id].kind {
// Strongly prefer towns
score += 1000.0;
// Prefer sites of a medium size
score += 2.0 / (1.0 + (site2.plots().len() as f32 - 20.0).abs() / 10.0);
let (site2, mut score) = match &index.sites[site_id].kind {
SiteKind::Refactor(site2) => (site2, 2.0),
// Non-town sites should not be chosen as starting sites and get a score of 0
_ => return (site_id.id(), 0.0)
};
// Prefer sites in hospitable climates
if let Some(chunk) = self.sim().get(civ_site.center) {
score += 1.0 / (1.0 + chunk.temp.abs());
score += 1.0 / (1.0 + (chunk.humidity - CONFIG.forest_hum).abs() * 2.0);
}
/// Optimal number of plots in a starter town
const OPTIMAL_STARTER_TOWN_SIZE: f32 = 30.0;
// Prefer sites of a medium size
let plots = site2.plots().len() as f32;
let size_score = if plots > OPTIMAL_STARTER_TOWN_SIZE {
1.0 + (1.0 / (1.0 + ((plots - OPTIMAL_STARTER_TOWN_SIZE) / 15.0).powi(3)))
} else {
(2.05 / (1.0 + ((OPTIMAL_STARTER_TOWN_SIZE - plots) / 15.0).powi(5))) - 0.05
}.max(0.01);
score *= size_score;
// Prefer sites that are close to the centre of the world
score += 4.0 / (1.0 + civ_site.center.map2(self.sim().get_size(), |e, sz| (e as f32 / sz as f32 - 0.5).abs() * 2.0).reduce_partial_max());
let pos_score = (
10.0 / (
1.0 + (
civ_site.center.map2(self.sim().get_size(),
|e, sz|
(e as f32 / sz as f32 - 0.5).abs() * 2.0).reduce_partial_max()
).powi(6) * 25.0
)
).max(0.02);
score *= pos_score;
// Check if neighboring biomes are beginner friendly
let mut chunk_scores = 2.0;
for (chunk, distance) in Spiral2d::with_radius(10)
.filter_map(|rel_pos| {
let chunk_pos = civ_site.center + rel_pos * 2;
self.sim().get(chunk_pos).zip(Some(rel_pos.as_::<f32>().magnitude()))
})
{
let weight = 1.0 / (distance * std::f32::consts::TAU + 1.0);
let chunk_difficulty = 20.0 / (20.0 + chunk.get_biome().difficulty().pow(4) as f32 / 5.0);
// let chunk_difficulty = 1.0 / chunk.get_biome().difficulty() as f32;
chunk_scores *= 1.0 - weight + chunk_difficulty * weight;
}
score *= chunk_scores;
(site_id.id(), score)
})
.collect::<Vec<_>>();