tiger-lib 1.18.0

Library used by the tools ck3-tiger, vic3-tiger, and imperator-tiger. This library holds the bulk of the code for them. It can be built either for ck3-tiger with the feature ck3, or for vic3-tiger with the feature vic3, or for imperator-tiger with the feature imperator, but not both at the same time.
Documentation
use crate::block::Block;
use crate::db::{Db, DbKind};
use crate::everything::Everything;
use crate::game::GameFlags;
use crate::item::{Item, ItemLoader};
use crate::report::{ErrorKey, warn};
use crate::token::Token;
use crate::validator::Validator;

#[derive(Clone, Debug)]
pub struct GameRule {}

inventory::submit! {
    ItemLoader::Normal(GameFlags::Vic3, Item::GameRule, GameRule::add)
}

impl GameRule {
    pub fn add(db: &mut Db, key: Token, block: Block) {
        db.add(Item::GameRule, key, block, Box::new(Self {}));
    }
}

/// LAST UPDATED VIC3 VERSION 1.12.2
/// Taken from `common/game_rules/_game_rules.info`
const SIMPLE_GAME_RULE_FLAGS: &[&str] = &[
    "blocks_achievements",
    "lenient_ai",
    "harsh_ai",
    "low_ai_aggression",
    "high_ai_aggression",
    "no_subject_flags",
    "no_subject_map_color",
    // undocumented flags follow
    "autonomous_investment",
    "no_pop_consolidation",
    "minor_pop_consolidation",
    "moderate_pop_consolidation",
    "aggressive_pop_consolidation",
    "directly_controlled_investment",
    "give_ruler_selector",
    "loyalties_grace_period_none",
    "loyalties_grace_period_short",
    "loyalties_grace_period_long",
    "loyalties_grace_period_extra_long",
    "no_fantastical_content",
    "use_custom_rng_seed",
    "no_dynamic_naming",
    "free_construction_scaled_player_exemption",
    "free_construction_unscaled",
    "give_fantastical_content",
    "no_ruler_selector",
];

impl DbKind for GameRule {
    fn add_subitems(&self, _key: &Token, block: &Block, db: &mut Db) {
        for (key, _) in block.iter_definitions() {
            db.add_flag(Item::GameRuleSetting, key.clone());
        }
    }

    fn validate(&self, key: &Token, block: &Block, data: &Everything) {
        let mut vd = Validator::new(block, data);

        let loca = format!("rule_{key}");
        data.verify_exists_implied(Item::Localization, &loca, key);

        vd.req_field("default");
        if let Some(token) = vd.field_value("default")
            && block.get_field_block(token.as_str()).is_none()
        {
            let msg = "default value not found among the settings";
            warn(ErrorKey::MissingItem).strong().msg(msg).loc(token).push();
        }

        vd.unknown_block_fields(|key, block| {
            let mut vd = Validator::new(block, data);
            let loca = format!("setting_{key}");
            data.verify_exists_implied(Item::Localization, &loca, key);
            let loca = format!("setting_{key}_desc");
            data.verify_exists_implied(Item::Localization, &loca, key);

            vd.multi_field_validated_value("flag", |_, mut vd| {
                vd.maybe_prefix_item("disable_", Item::ProductionMethod);
                vd.maybe_prefix_item("force_", Item::ProductionMethod);
                vd.choice(SIMPLE_GAME_RULE_FLAGS);
            });
        });
    }
}