Skip to main content

nil_core/world/
city.rs

1// Copyright (C) Call of Nil contributors
2// SPDX-License-Identifier: AGPL-3.0-only
3
4use crate::city::City;
5use crate::continent::ContinentKey;
6use crate::error::Result;
7use crate::ranking::score::Score;
8use crate::ruler::Ruler;
9use crate::world::World;
10
11impl World {
12  #[inline]
13  pub fn city(&self, key: impl ContinentKey) -> Result<&City> {
14    self.continent.city(key)
15  }
16
17  #[inline]
18  pub(crate) fn city_mut(&mut self, key: impl ContinentKey) -> Result<&mut City> {
19    self.continent.city_mut(key)
20  }
21
22  pub fn count_cities<R>(&self, owner: R) -> u32
23  where
24    R: Into<Ruler>,
25  {
26    self
27      .continent
28      .cities_of(owner)
29      .count()
30      .try_into()
31      .unwrap_or(u32::MAX)
32  }
33
34  pub fn get_city_score(&self, key: impl ContinentKey) -> Result<Score> {
35    let stats = self.stats.infrastructure.as_ref();
36    self.continent.city(key)?.score(stats)
37  }
38
39  pub fn rename_city(&mut self, key: impl ContinentKey, name: &str) -> Result<()> {
40    let coord = key.into_coord(self.continent.size())?;
41    let city = self.continent.city_mut(coord)?;
42    name.clone_into(city.name_mut());
43
44    self.emit_public_city_updated(coord);
45    self.emit_city_updated(coord);
46
47    Ok(())
48  }
49}