tiger_lib/vic3/tables/
iterators.rs

1use std::sync::LazyLock;
2
3use crate::everything::Everything;
4use crate::helpers::TigerHashMap;
5use crate::item::Item;
6use crate::lowercase::Lowercase;
7use crate::report::{ErrorKey, err};
8use crate::scopes::Scopes;
9use crate::token::Token;
10
11const GEOGRAPHIC_REGION_ITERATORS: &[(Scopes, &str, Scopes)] = &[
12    (Scopes::None, "country_in_", Scopes::Country),
13    (Scopes::None, "province_in_", Scopes::Province),
14    (Scopes::None, "state_in_", Scopes::State),
15    (Scopes::None, "state_region_in_", Scopes::StateRegion),
16    (Scopes::None, "strategic_region_in_", Scopes::StrategicRegion),
17];
18
19#[inline]
20pub fn iterator(name_lc: &Lowercase, name: &Token, data: &Everything) -> Option<(Scopes, Scopes)> {
21    for (inscope, pfx, outscope) in GEOGRAPHIC_REGION_ITERATORS.iter().copied() {
22        if let Some(part) = name_lc.strip_prefix_unchecked(pfx) {
23            if !data.item_exists(Item::GeographicRegionShortKey, part.as_str()) {
24                let msg = format!("could not find geographic region short key {part}");
25                let info = format!("so the iterator {name} does not exist");
26                err(ErrorKey::MissingItem).strong().msg(msg).info(info).loc(name).push();
27            }
28            return Some((inscope, outscope));
29        }
30    }
31
32    ITERATOR_MAP.get(name_lc.as_str()).copied()
33}
34
35static ITERATOR_MAP: LazyLock<TigerHashMap<&'static str, (Scopes, Scopes)>> = LazyLock::new(|| {
36    let mut hash = TigerHashMap::default();
37    for (from, s, to) in ITERATOR.iter().copied() {
38        hash.insert(s, (from, to));
39    }
40    hash
41});
42
43/// LAST UPDATED VIC3 VERSION 1.12.0
44/// See `effects.log` from the game data dumps
45/// These are the list iterators. Every entry represents
46/// a every_, ordered_, random_, and any_ version.
47const ITERATOR: &[(Scopes, &str, Scopes)] = &[
48    (Scopes::Country, "active_law", Scopes::Law),
49    (Scopes::Country, "active_party", Scopes::Party),
50    (Scopes::None, "character", Scopes::Character),
51    (Scopes::None, "character_in_exile_pool", Scopes::Character),
52    (Scopes::None, "character_in_void", Scopes::Character),
53    (Scopes::Country, "civil_war", Scopes::CivilWar),
54    (Scopes::Country, "cobelligerent_in_diplo_play", Scopes::Country),
55    (Scopes::Country, "cobelligerent_in_war", Scopes::Country),
56    (
57        Scopes::Battle
58            .union(Scopes::Building)
59            .union(Scopes::Front)
60            .union(Scopes::Hq)
61            .union(Scopes::MilitaryFormation),
62        "combat_unit",
63        Scopes::NewCombatUnit,
64    ),
65    (Scopes::Country, "company", Scopes::Company),
66    (Scopes::None, "country", Scopes::Country),
67    (Scopes::None, "decentralized_country", Scopes::Country),
68    (Scopes::Country, "diplomatic_catalyst", Scopes::DiplomaticCatalyst),
69    (Scopes::None, "diplomatic_play", Scopes::DiplomaticPlay),
70    (Scopes::Country, "diplomatically_relevant_country", Scopes::Country),
71    (Scopes::Country, "direct_subject", Scopes::Country),
72    (Scopes::Country, "enemy_in_diplo_play", Scopes::Country),
73    (Scopes::Country, "enemy_in_war", Scopes::Country),
74    (
75        Scopes::Country
76            .union(Scopes::State)
77            .union(Scopes::StateRegion)
78            .union(Scopes::StrategicRegion),
79        "harvest_condition",
80        Scopes::HarvestConditionType,
81    ),
82    (Scopes::None, "in_global_list", Scopes::all_but_none()),
83    (Scopes::Country, "in_hierarchy", Scopes::Country),
84    (Scopes::None, "in_list", Scopes::all_but_none()),
85    (Scopes::None, "in_local_list", Scopes::all_but_none()),
86    (Scopes::PoliticalMovement, "influenced_interest_group", Scopes::InterestGroup),
87    (Scopes::Country, "interest_group", Scopes::InterestGroup),
88    (Scopes::Country, "law", Scopes::Law),
89    (Scopes::PoliticalLobby, "lobby_member", Scopes::InterestGroup),
90    (Scopes::None, "market", Scopes::Market),
91    (Scopes::Market, "market_goods", Scopes::MarketGoods),
92    (Scopes::Party, "member", Scopes::InterestGroup),
93    (
94        Scopes::Country.union(Scopes::Front).union(Scopes::Hq),
95        "military_formation",
96        Scopes::MilitaryFormation,
97    ),
98    (
99        Scopes::Country
100            .union(Scopes::State)
101            .union(Scopes::StateRegion)
102            .union(Scopes::StrategicRegion),
103        "neighbouring_state",
104        Scopes::State,
105    ),
106    (Scopes::Country, "overlord_or_above", Scopes::Country),
107    (Scopes::DiplomaticPact, "participant", Scopes::Country),
108    (Scopes::Country.union(Scopes::InterestGroup), "political_lobby", Scopes::PoliticalLobby),
109    (Scopes::Country, "political_movement", Scopes::PoliticalMovement),
110    (Scopes::Law, "possible_amendment_type", Scopes::AmendmentType),
111    (Scopes::Country, "potential_party", Scopes::Party),
112    (Scopes::None, "power_bloc", Scopes::PowerBloc),
113    (Scopes::PowerBloc, "power_bloc_member", Scopes::Country),
114    (Scopes::InterestGroup, "preferred_law", Scopes::Law),
115    (Scopes::Country.union(Scopes::CountryDefinition), "primary_culture", Scopes::Culture),
116    // TODO: verify. The docs have State and Province reversed.
117    (Scopes::State, "province", Scopes::Province),
118    (Scopes::Country, "rival_country", Scopes::Country),
119    (Scopes::Country, "rivaling_country", Scopes::Country),
120    // TODO: Scopes::Front is in the docs but doesn't make sense for admirals
121    (
122        Scopes::Country
123            .union(Scopes::Front)
124            .union(Scopes::InterestGroup)
125            .union(Scopes::MilitaryFormation),
126        "scope_admiral",
127        Scopes::Character,
128    ),
129    (Scopes::Country, "scope_ally", Scopes::Country),
130    (Scopes::Law, "scope_amendment", Scopes::Amendment),
131    (Scopes::Treaty, "scope_article", Scopes::TreatyArticle),
132    (
133        Scopes::TreatyOptions.union(Scopes::Treaty),
134        "scope_article_option",
135        Scopes::TreatyArticleOptions,
136    ),
137    (Scopes::Country.union(Scopes::State), "scope_building", Scopes::Building),
138    (
139        Scopes::Country
140            .union(Scopes::Front)
141            .union(Scopes::InterestGroup)
142            .union(Scopes::MilitaryFormation),
143        "scope_character",
144        Scopes::Character,
145    ),
146    (
147        Scopes::Market.union(Scopes::StateRegion).union(Scopes::StrategicRegion),
148        "scope_country",
149        Scopes::Country,
150    ),
151    (Scopes::Country.union(Scopes::State), "scope_culture", Scopes::Culture),
152    (Scopes::Country, "scope_diplomatic_pact", Scopes::DiplomaticPact),
153    (Scopes::War, "scope_front", Scopes::Front),
154    (
155        Scopes::Country
156            .union(Scopes::Front)
157            .union(Scopes::InterestGroup)
158            .union(Scopes::MilitaryFormation),
159        "scope_general",
160        Scopes::Character,
161    ),
162    (Scopes::Country, "scope_held_interest_marker", Scopes::InterestMarker),
163    (Scopes::DiplomaticPlay, "scope_initiator_ally", Scopes::Country),
164    (
165        Scopes::Country.union(Scopes::StateRegion).union(Scopes::StrategicRegion),
166        "scope_interest_marker",
167        Scopes::InterestMarker,
168    ),
169    (Scopes::JournalEntry, "scope_je_involved", Scopes::Country),
170    (Scopes::DiplomaticPlay, "scope_play_involved", Scopes::Country),
171    (
172        Scopes::Country
173            .union(Scopes::Front)
174            .union(Scopes::InterestGroup)
175            .union(Scopes::MilitaryFormation),
176        "scope_politician",
177        Scopes::Character,
178    ),
179    (
180        Scopes::Country.union(Scopes::Culture).union(Scopes::InterestGroup).union(Scopes::State),
181        "scope_pop",
182        Scopes::Pop,
183    ),
184    (Scopes::Company, "scope_regional_hqs", Scopes::Building),
185    (
186        Scopes::Country
187            .union(Scopes::Front)
188            .union(Scopes::StateRegion)
189            .union(Scopes::StrategicRegion)
190            .union(Scopes::Theater),
191        "scope_state",
192        Scopes::State,
193    ),
194    (Scopes::DiplomaticPlay, "scope_target_ally", Scopes::Country),
195    (Scopes::Country, "scope_theater", Scopes::Theater),
196    (Scopes::Country, "scope_treaty", Scopes::Treaty),
197    (Scopes::Country, "scope_violate_sovereignty_interested_parties", Scopes::Country),
198    (Scopes::Country, "scope_violate_sovereignty_wars", Scopes::War),
199    (Scopes::Country, "scope_war", Scopes::War),
200    // TODO: check if the scoped state is a sea node
201    (Scopes::State, "sea_node_adjacent_state", Scopes::State),
202    (Scopes::None, "state", Scopes::State),
203    (Scopes::None, "state_region", Scopes::StateRegion),
204    (Scopes::Country, "strategic_objective", Scopes::State),
205    (Scopes::Country, "subject_of_subject", Scopes::Country),
206    (Scopes::Country, "subject_or_below", Scopes::Country),
207    (Scopes::PoliticalMovement, "supporting_character", Scopes::Character),
208    (Scopes::None, "treaty", Scopes::Treaty),
209    (Scopes::Country, "valid_mass_migration_culture", Scopes::Culture),
210    (Scopes::War, "war_participant", Scopes::Country),
211];
212
213pub fn iterator_removed(name: &str) -> Option<(&'static str, &'static str)> {
214    for (removed_name, version, explanation) in ITERATOR_REMOVED.iter().copied() {
215        if name == removed_name {
216            return Some((version, explanation));
217        }
218    }
219    None
220}
221
222const ITERATOR_REMOVED: &[(&str, &str, &str)] = &[
223    (
224        "scope_cobelligerent",
225        "1.4.0",
226        "replaced with _cobelligerent_in_diplo_play, _cobelligerent_in_war",
227    ),
228    ("supporting_interest_group", "1.8", "replaced with `_influenced_interest_group`"),
229    ("trade_route", "1.9", "replaced by world market system"),
230];