Skip to main content

nil_core/world/infrastructure/
storage.rs

1// Copyright (C) Call of Nil contributors
2// SPDX-License-Identifier: AGPL-3.0-only
3
4use crate::continent::ContinentKey;
5use crate::error::Result;
6use crate::infrastructure::storage::{
7  OverallStorageCapacity,
8  OverallStorageCapacityWeight,
9  StorageCapacityWeight,
10};
11use crate::ruler::Ruler;
12use crate::world::World;
13
14impl World {
15  pub fn get_storage_capacity<R>(&self, ruler: R) -> Result<OverallStorageCapacity>
16  where
17    R: Into<Ruler>,
18  {
19    let stats = &self.stats.infrastructure;
20    self
21      .continent
22      .cities_of(ruler)
23      .try_fold(OverallStorageCapacity::default(), |mut acc, city| {
24        acc += city.storage_capacity(stats)?;
25        Ok(acc)
26      })
27  }
28
29  pub fn get_storage_capacity_weight<K>(&self, key: K) -> Result<OverallStorageCapacityWeight>
30  where
31    K: ContinentKey,
32  {
33    let stats = &self.stats.infrastructure;
34    let city = key
35      .into_coord(self.continent.size())
36      .and_then(|coord| self.city(coord))?;
37
38    let capacity = city.storage_capacity(stats)?;
39    let total = self.get_storage_capacity(city.owner().clone())?;
40
41    let mut weight = OverallStorageCapacityWeight::new(city.coord());
42
43    macro_rules! set_weight {
44      ($($storage:ident),+ $(,)?) => {
45        $(
46          if *total.$storage > 0u32 {
47            let value = f64::from(capacity.$storage) / f64::from(total.$storage);
48            weight.$storage = StorageCapacityWeight::from(value);
49            debug_assert!(value.is_normal());
50          }
51        )+
52      };
53    }
54
55    set_weight!(silo, warehouse);
56
57    Ok(weight)
58  }
59}