tiger_lib/ck3/data/
prov_terrain.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
use std::path::PathBuf;

use crate::block::Block;
use crate::everything::Everything;
use crate::fileset::{FileEntry, FileHandler};
use crate::helpers::{dup_error, TigerHashMap};
use crate::item::Item;
use crate::parse::ParserMemory;
use crate::pdxfile::PdxFile;
use crate::report::{warn, ErrorKey};
use crate::token::{Loc, Token};
use crate::validator::Validator;
use crate::Severity;

use super::provinces::ProvId;

const DEFAULT_TERRAINS: &[&str] = &["default_land", "default_sea", "default_coastal_sea"];

#[derive(Clone, Debug, Default)]
pub struct ProvinceTerrains {
    provinces: TigerHashMap<ProvId, ProvinceTerrain>,
    file_loc: Option<Loc>,
    defaults: [Option<Token>; DEFAULT_TERRAINS.len()],
}

impl ProvinceTerrains {
    fn load_item(&mut self, id: ProvId, key: Token, value: Token) {
        if let Some(province) = self.provinces.get_mut(&id) {
            if province.key.loc.kind >= key.loc.kind {
                dup_error(&key, &province.key, "province");
            }
            *province = ProvinceTerrain::new(key, value);
        } else {
            self.provinces.insert(id, ProvinceTerrain::new(key, value));
        }
    }

    pub fn validate(&self, data: &Everything) {
        for (provid, item) in &self.provinces {
            item.validate(*provid, data);
        }

        // If no file was handled, for example when using `replace_paths`, self.file_loc is None.
        // TODO: Find a way to denote `ErrorLoc` error report when no loc is available.
        if self.file_loc.is_some() {
            for (name, terrains) in DEFAULT_TERRAINS.iter().zip(&self.defaults) {
                if let Some(terrain) = terrains {
                    data.verify_exists(Item::Terrain, terrain);
                } else {
                    let msg = format!("missing default terrain: {name}");
                    warn(ErrorKey::Validation)
                        .msg(msg)
                        // SAFETY: self.file_loc is `Some`
                        .loc(self.file_loc.unwrap())
                        .push();
                }
            }
        }
    }
}

impl FileHandler<Block> for ProvinceTerrains {
    fn subpath(&self) -> std::path::PathBuf {
        PathBuf::from("common/province_terrain")
    }

    fn load_file(&self, entry: &FileEntry, parser: &ParserMemory) -> Option<Block> {
        if !entry.filename().to_string_lossy().ends_with("province_terrain.txt") {
            // Omit _province_properties.txt
            return None;
        }

        PdxFile::read_detect_encoding(entry, parser)
    }

    fn handle_file(&mut self, _entry: &FileEntry, mut block: Block) {
        self.file_loc = Some(block.loc);
        for (key, value) in block.drain_assignments_warn() {
            if let Ok(id) = key.as_str().parse() {
                self.load_item(id, key, value);
            } else if let Some(index) = DEFAULT_TERRAINS.iter().position(|&x| x == key.as_str()) {
                if let Some(default) = &self.defaults[index] {
                    if default.loc.kind >= key.loc.kind {
                        dup_error(&key, default, "default terrain");
                    }
                } else {
                    self.defaults[index] = Some(value);
                }
            } else {
                let msg = "unexpected key, expected only province ids or default terrains";
                warn(ErrorKey::Validation).msg(msg).loc(key).push();
            }
        }
    }
}

#[derive(Clone, Debug)]
pub struct ProvinceTerrain {
    key: Token,
    terrain: Token,
}

impl ProvinceTerrain {
    fn new(key: Token, terrain: Token) -> Self {
        Self { key, terrain }
    }

    fn validate(&self, provid: ProvId, data: &Everything) {
        data.provinces_ck3.verify_exists_provid(provid, &self.key, Severity::Error);
        data.verify_exists(Item::Terrain, &self.terrain);
    }
}

#[derive(Clone, Debug, Default)]
pub struct ProvinceProperties {
    provinces: TigerHashMap<ProvId, ProvinceProperty>,
}

impl ProvinceProperties {
    fn load_item(&mut self, id: ProvId, key: Token, mut block: Block) {
        if let Some(province) = self.provinces.get_mut(&id) {
            // Multiple entries are valid but could easily be a mistake.
            if province.key.loc.kind >= key.loc.kind {
                dup_error(&key, &province.key, "province");
            }
            province.block.append(&mut block);
        } else {
            self.provinces.insert(id, ProvinceProperty::new(key, block));
        }
    }

    pub fn validate(&self, data: &Everything) {
        for (provid, item) in &self.provinces {
            item.validate(*provid, data);
        }
    }
}

impl FileHandler<Block> for ProvinceProperties {
    fn subpath(&self) -> PathBuf {
        PathBuf::from("common/province_terrain")
    }

    fn load_file(&self, entry: &FileEntry, parser: &ParserMemory) -> Option<Block> {
        if !entry.filename().to_string_lossy().ends_with("province_properties.txt") {
            // Omit _province_terrain.txt
            return None;
        }
        PdxFile::read_detect_encoding(entry, parser)
    }

    fn handle_file(&mut self, _entry: &FileEntry, mut block: Block) {
        for (key, block) in block.drain_definitions_warn() {
            if let Ok(id) = key.as_str().parse() {
                self.load_item(id, key, block);
            } else {
                let msg = "unexpected key, expected only province ids";
                warn(ErrorKey::Validation).msg(msg).loc(key).push();
            }
        }
    }
}

#[derive(Clone, Debug)]
pub struct ProvinceProperty {
    key: Token,
    block: Block,
}

impl ProvinceProperty {
    fn new(key: Token, block: Block) -> Self {
        Self { key, block }
    }

    fn validate(&self, provid: ProvId, data: &Everything) {
        data.provinces_ck3.verify_exists_provid(provid, &self.key, Severity::Error);
        let mut vd = Validator::new(&self.block, data);
        if data.provinces_ck3.is_sea_or_river(provid) {
            vd.field_validated_value("winter_severity_bias", |_, mut vd| {
                vd.maybe_is("0.0");
            });
            vd.ban_field("mild_winter_factor_override", || "sea and river province");
            vd.ban_field("normal_winter_factor_override", || "sea and river province");
            vd.ban_field("harsh_winter_factor_override", || "sea and river province");
        } else {
            vd.field_numeric_range("winter_severity_bias", 0.0..=1.0);
            vd.field_numeric_range("mild_winter_factor_override", 0.0..=1.0);
            vd.field_numeric_range("normal_winter_factor_override", 0.0..=1.0);
            vd.field_numeric_range("harsh_winter_factor_override", 0.0..=1.0);
        }
    }
}