tiger_lib/ck3/data/
climate.rs

1use crate::block::Block;
2use crate::db::{Db, DbKind};
3use crate::everything::Everything;
4use crate::game::GameFlags;
5use crate::helpers::TigerHashSet;
6use crate::item::{Item, ItemLoader, LoadAsFile, Recursive};
7use crate::pdxfile::PdxEncoding;
8use crate::report::{ErrorKey, untidy, warn};
9use crate::token::Token;
10use crate::validator::Validator;
11
12#[derive(Clone, Debug)]
13pub struct Climate {}
14
15inventory::submit! {
16    ItemLoader::Full(GameFlags::Ck3, Item::Climate, PdxEncoding::Utf8OptionalBom, ".txt", LoadAsFile::No, Recursive::No, Climate::add)
17}
18
19impl Climate {
20    pub fn add(db: &mut Db, key: Token, block: Block) {
21        db.add(Item::Climate, key, block, Box::new(Self {}));
22    }
23
24    /// Check if there are any duplicate provinces between climate items.
25    /// The check for duplicate provinces within one climate item is done in [`Climate::validate`].
26    pub fn validate_all(db: &Db, _data: &Everything) {
27        let mut other_seen: TigerHashSet<&Token> = TigerHashSet::default();
28        for (_, block) in db.iter_key_block(Item::Climate) {
29            for value in block.iter_values() {
30                if let Some(&other) = other_seen.get(value) {
31                    let msg = format!("province {value} has two climates");
32                    warn(ErrorKey::Conflict)
33                        .msg(msg)
34                        .loc(value)
35                        .loc_msg(other, "other climate")
36                        .push();
37                }
38            }
39            other_seen.extend(block.iter_values());
40        }
41    }
42}
43
44impl DbKind for Climate {
45    fn validate(&self, _key: &Token, block: &Block, data: &Everything) {
46        let mut vd = Validator::new(block, data);
47        let mut seen: TigerHashSet<&Token> = TigerHashSet::default();
48        for token in vd.values() {
49            data.verify_exists(Item::Province, token);
50            if let Some(&other) = seen.get(token) {
51                let msg = format!("duplicate province {token}");
52                untidy(ErrorKey::Conflict).msg(msg).loc(token).loc_msg(other, "other").push();
53            } else {
54                seen.insert(token);
55            }
56        }
57    }
58}