deduplicate location name code

This commit is contained in:
Christof Petig 2023-08-22 23:11:18 +02:00
parent b20ff5144c
commit 24773cb3f1
2 changed files with 20 additions and 28 deletions

View File

@ -331,22 +331,7 @@ impl World {
};
let meta = TerrainChunkMeta::new(
sim_chunk
.sites
.iter()
.filter(|id| {
index.sites[**id]
.get_origin()
.distance_squared(chunk_center_wpos2d) as f32
<= index.sites[**id].radius().powi(2)
})
.min_by_key(|id| {
index.sites[**id]
.get_origin()
.distance_squared(chunk_center_wpos2d)
})
.map(|id| index.sites[*id].name().to_string())
.or_else(|| sim_chunk.poi.map(|poi| self.civs.pois[poi].name.clone())),
sim_chunk.get_location_name(&index.sites, &self.civs.pois, chunk_center_wpos2d),
sim_chunk.get_biome(),
sim_chunk.alt,
sim_chunk.tree_density,
@ -691,16 +676,6 @@ impl World {
pub fn get_location_name(&self, index: IndexRef, wpos2d: Vec2<i32>) -> Option<String> {
let chunk_pos = wpos2d.wpos_to_cpos();
let sim_chunk = self.sim.get(chunk_pos)?;
// TODO: Move this into SimChunk for reuse with above?
sim_chunk
.sites
.iter()
.filter(|id| {
index.sites[**id].get_origin().distance_squared(wpos2d) as f32
<= index.sites[**id].radius().powi(2)
})
.min_by_key(|id| index.sites[**id].get_origin().distance_squared(wpos2d))
.map(|id| index.sites[*id].name().to_string())
.or_else(|| sim_chunk.poi.map(|poi| self.civs.pois[poi].name.clone()))
sim_chunk.get_location_name(&index.sites, &self.civs.pois, wpos2d)
}
}

View File

@ -44,7 +44,7 @@ use common::{
grid::Grid,
lottery::Lottery,
spiral::Spiral2d,
store::Id,
store::{Id, Store},
terrain::{
map::MapConfig, uniform_idx_as_vec2, vec2_as_uniform_idx, BiomeKind, CoordinateConversions,
MapSizeLg, TerrainChunk, TerrainChunkSize,
@ -2662,4 +2662,21 @@ impl SimChunk {
},
}
}
pub fn get_location_name(
&self,
index_sites: &Store<crate::site::Site>,
civs_pois: &Store<PointOfInterest>,
wpos2d: Vec2<i32>,
) -> Option<String> {
self.sites
.iter()
.filter(|id| {
index_sites[**id].get_origin().distance_squared(wpos2d) as f32
<= index_sites[**id].radius().powi(2)
})
.min_by_key(|id| index_sites[**id].get_origin().distance_squared(wpos2d))
.map(|id| index_sites[*id].name().to_string())
.or_else(|| self.poi.map(|poi| civs_pois[poi].name.clone()))
}
}