tiger_lib/
scopes.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
//! The core [`Scopes`] type which tracks our knowledge about the types of in-game values.

use std::fmt::{Display, Formatter};

use bitflags::bitflags;

use crate::context::ScopeContext;
use crate::everything::Everything;
use crate::game::Game;
use crate::item::Item;
use crate::report::{err, ErrorKey};
use crate::token::Token;

/// vic3 needs more than 64 bits, but the others don't.
#[cfg(feature = "vic3")]
type ScopesBits = u128;
#[cfg(not(feature = "vic3"))]
type ScopesBits = u64;

bitflags! {
    /// This type represents our knowledge about the set of scope types that a script value can
    /// have. In most cases it's narrowed down to a single scope type, but not always.
    ///
    /// The available scope types depend on the game.
    /// They are listed in `event_scopes.log` from the game data dumps.
    // LAST UPDATED CK3 VERSION 1.14.0.2
    // LAST UPDATED VIC3 VERSION 1.8.1
    // LAST UPDATED IR VERSION 2.0.4
    //
    // Each scope type gets one bitflag. In order to keep the bit count down, scope types from
    // the different games have overlapping bitflags. Therefore, scope types from different games
    // should be kept carefully separated.
    #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
    #[rustfmt::skip] // having the cfg and the flag on one line is much more readable
    pub struct Scopes: ScopesBits {
        // Generic scope types
        const None = 0x0000_0001;
        const Value = 0x0000_0002;
        const Bool = 0x0000_0004;
        const Flag = 0x0000_0008;

        // Scope types shared by multiple games

        #[cfg(any(feature = "vic3", feature = "imperator"))]
        const Color = 0x0000_0010;
        #[cfg(any(feature = "vic3", feature = "imperator"))]
        const Country = 0x0000_0020;
        const Character = 0x0000_0040;
        const Culture = 0x0000_0080;
        const Province = 0x0000_0100;
        #[cfg(any(feature = "vic3", feature = "imperator"))]
        const Pop = 0x0000_0200;
        #[cfg(any(feature = "vic3", feature = "imperator"))]
        const Party = 0x0000_0400;
        const Religion = 0x0000_0800;
        #[cfg(any(feature = "vic3", feature = "imperator"))]
        const State = 0x0000_1000;
        const War = 0x0000_2000;

        // Scope types for CK3
        #[cfg(feature = "ck3")] const Accolade = 0x0001_0000;
        #[cfg(feature = "ck3")] const AccoladeType = 0x0002_0000;
        #[cfg(feature = "ck3")] const Activity = 0x0004_0000;
        #[cfg(feature = "ck3")] const ActivityType = 0x0008_0000;
        #[cfg(feature = "ck3")] const Army = 0x0010_0000;
        #[cfg(feature = "ck3")] const Artifact = 0x0020_0000;
        #[cfg(feature = "ck3")] const CasusBelli = 0x0040_0000;
        #[cfg(feature = "ck3")] const CharacterMemory = 0x0080_0000;
        #[cfg(feature = "ck3")] const Combat = 0x0100_0000;
        #[cfg(feature = "ck3")] const CombatSide = 0x0200_0000;
        #[cfg(feature = "ck3")] const CouncilTask = 0x0400_0000;
        #[cfg(feature = "ck3")] const CulturePillar = 0x0800_0000;
        #[cfg(feature = "ck3")] const CultureTradition = 0x1000_0000;
        #[cfg(feature = "ck3")] const Decision = 0x2000_0000;
        #[cfg(feature = "ck3")] const Doctrine = 0x4000_0000;
        #[cfg(feature = "ck3")] const Dynasty = 0x8000_0000;
        #[cfg(feature = "ck3")] const DynastyHouse = 0x0000_0001_0000_0000;
        #[cfg(feature = "ck3")] const Faction = 0x0000_0002_0000_0000;
        #[cfg(feature = "ck3")] const Faith = 0x0000_0004_0000_0000;
        #[cfg(feature = "ck3")] const GovernmentType = 0x0000_0008_0000_0000;
        #[cfg(feature = "ck3")] const GreatHolyWar = 0x0000_0010_0000_0000;
        #[cfg(feature = "ck3")] const HolyOrder = 0x0000_0020_0000_0000;
        #[cfg(feature = "ck3")] const Inspiration = 0x0000_0040_0000_0000;
        #[cfg(feature = "ck3")] const LandedTitle = 0x0000_0080_0000_0000;
        #[cfg(feature = "ck3")] const MercenaryCompany = 0x0000_0100_0000_0000;
        #[cfg(feature = "ck3")] const Scheme = 0x0000_0200_0000_0000;
        #[cfg(feature = "ck3")] const Secret = 0x0000_0400_0000_0000;
        #[cfg(feature = "ck3")] const StoryCycle = 0x0000_0800_0000_0000;
        #[cfg(feature = "ck3")] const Struggle = 0x0000_1000_0000_0000;
        #[cfg(feature = "ck3")] const TitleAndVassalChange = 0x0000_2000_0000_0000;
        #[cfg(feature = "ck3")] const Trait = 0x0000_4000_0000_0000;
        #[cfg(feature = "ck3")] const TravelPlan = 0x0000_8000_0000_0000;
        #[cfg(feature = "ck3")] const VassalContract = 0x0001_0000_0000_0000;
        #[cfg(feature = "ck3")] const VassalObligationLevel = 0x0002_0000_0000_0000;
        // CK3 1.11
        #[cfg(feature = "ck3")] const HoldingType = 0x0004_0000_0000_0000;
        #[cfg(feature = "ck3")] const TaxSlot = 0x0008_0000_0000_0000;
        // CK3 1.12
        #[cfg(feature = "ck3")] const EpidemicType = 0x0010_0000_0000_0000;
        #[cfg(feature = "ck3")] const Epidemic = 0x0020_0000_0000_0000;
        #[cfg(feature = "ck3")] const LegendType = 0x0040_0000_0000_0000;
        #[cfg(feature = "ck3")] const Legend = 0x0080_0000_0000_0000;
        #[cfg(feature = "ck3")] const GeographicalRegion = 0x0100_0000_0000_0000;
        // CK3 1.13
        #[cfg(feature = "ck3")] const Domicile = 0x0200_0000_0000_0000;
        #[cfg(feature = "ck3")] const AgentSlot = 0x0400_0000_0000_0000;
        #[cfg(feature = "ck3")] const TaskContract = 0x0800_0000_0000_0000;
        #[cfg(feature = "ck3")] const TaskContractType = 0x1000_0000_0000_0000;
        #[cfg(feature = "ck3")] const Regiment = 0x2000_0000_0000_0000;
        #[cfg(feature = "ck3")] const CasusBelliType = 0x4000_0000_0000_0000;

        #[cfg(feature = "vic3")] const Battle = 0x0001_0000;
        #[cfg(feature = "vic3")] const BattleSide = 0x0002_0000;
        #[cfg(feature = "vic3")] const Building = 0x0004_0000;
        #[cfg(feature = "vic3")] const BuildingType = 0x0008_0000;
        #[cfg(feature = "vic3")] const CanalType = 0x0010_0000;
        #[cfg(feature = "vic3")] const CivilWar = 0x0020_0000;
        #[cfg(feature = "vic3")] const CulturalCommunity = 0x0040_0000;
        #[cfg(feature = "vic3")] const NewCombatUnit = 0x0080_0000;
        #[cfg(feature = "vic3")] const CommanderOrderType = 0x0100_0000;
        #[cfg(feature = "vic3")] const CountryCreation = 0x0200_0000;
        #[cfg(feature = "vic3")] const CountryDefinition = 0x0400_0000;
        #[cfg(feature = "vic3")] const CountryFormation = 0x0800_0000;
        #[cfg(feature = "vic3")] const Decree = 0x1000_0000;
        #[cfg(feature = "vic3")] const DiplomaticAction = 0x2000_0000;
        #[cfg(feature = "vic3")] const DiplomaticPact = 0x4000_0000;
        #[cfg(feature = "vic3")] const DiplomaticPlay = 0x8000_0000;
        #[cfg(feature = "vic3")] const DiplomaticRelations = 0x0000_0001_0000_0000;
        #[cfg(feature = "vic3")] const Front = 0x0000_0002_0000_0000;
        #[cfg(feature = "vic3")] const Goods = 0x0000_0004_0000_0000;
        #[cfg(feature = "vic3")] const Hq = 0x0000_0008_0000_0000;
        #[cfg(feature = "vic3")] const Ideology = 0x0000_0010_0000_0000;
        #[cfg(feature = "vic3")] const Institution = 0x0000_0020_0000_0000;
        #[cfg(feature = "vic3")] const InstitutionType = 0x0000_0040_0000_0000;
        #[cfg(feature = "vic3")] const InterestMarker = 0x0000_0080_0000_0000;
        #[cfg(feature = "vic3")] const InterestGroup = 0x0000_0100_0000_0000;
        #[cfg(feature = "vic3")] const InterestGroupTrait = 0x0000_0200_0000_0000;
        #[cfg(feature = "vic3")] const InterestGroupType = 0x0000_0400_0000_0000;
        #[cfg(feature = "vic3")] const JournalEntry = 0x0000_0800_0000_0000;
        #[cfg(feature = "vic3")] const Law = 0x0000_1000_0000_0000;
        #[cfg(feature = "vic3")] const LawType = 0x0000_2000_0000_0000;
        #[cfg(feature = "vic3")] const Market = 0x0000_4000_0000_0000;
        #[cfg(feature = "vic3")] const MarketGoods = 0x0000_8000_0000_0000;
        #[cfg(feature = "vic3")] const Objective = 0x0001_0000_0000_0000;
        #[cfg(feature = "vic3")] const PoliticalMovement = 0x0002_0000_0000_0000;
        #[cfg(feature = "vic3")] const PopType = 0x0004_0000_0000_0000;
        #[cfg(feature = "vic3")] const ShippingLane = 0x0008_0000_0000_0000;
        #[cfg(feature = "vic3")] const StateRegion = 0x0010_0000_0000_0000;
        #[cfg(feature = "vic3")] const StateTrait = 0x0020_0000_0000_0000;
        #[cfg(feature = "vic3")] const StrategicRegion = 0x0040_0000_0000_0000;
        #[cfg(feature = "vic3")] const Technology = 0x0080_0000_0000_0000;
        #[cfg(feature = "vic3")] const TechnologyStatus = 0x0100_0000_0000_0000;
        #[cfg(feature = "vic3")] const Theater = 0x0200_0000_0000_0000;
        #[cfg(feature = "vic3")] const TradeRoute = 0x0400_0000_0000_0000;
        #[cfg(feature = "vic3")] const CombatUnitType = 0x1000_0000_0000_0000;
        #[cfg(feature = "vic3")] const MilitaryFormation = 0x2000_0000_0000_0000;
        #[cfg(feature = "vic3")] const Sway = 0x4000_0000_0000_0000;
        #[cfg(feature = "vic3")] const StateGoods = 0x8000_0000_0000_0000;
        #[cfg(feature = "vic3")] const DiplomaticDemand = 0x0000_0000_0000_0001_0000_0000_0000_0000;
        #[cfg(feature = "vic3")] const Company = 0x0000_0000_0000_0002_0000_0000_0000_0000;
        #[cfg(feature = "vic3")] const CompanyType = 0x0000_0000_0000_0004_0000_0000_0000_0000;
        #[cfg(feature = "vic3")] const TravelNode = 0x0000_0000_0000_0008_0000_0000_0000_0000;
        #[cfg(feature = "vic3")] const TravelNodeDefinition = 0x0000_0000_0000_0010_0000_0000_0000_0000;
        #[cfg(feature = "vic3")] const TravelConnection = 0x0000_0000_0000_0020_0000_0000_0000_0000;
        #[cfg(feature = "vic3")] const TravelConnectionDefinition = 0x0000_0000_0000_0040_0000_0000_0000_0000;
        #[cfg(feature = "vic3")] const NavalInvasion = 0x0000_0000_0000_0080_0000_0000_0000_0000;
        #[cfg(feature = "vic3")] const MobilizationOption = 0x0000_0000_0000_0100_0000_0000_0000_0000;
        #[cfg(feature = "vic3")] const PowerBlocPrincipleGroup = 0x0000_0000_0000_0200_0000_0000_0000_0000;
        #[cfg(feature = "vic3")] const DiplomaticPlayType = 0x0000_0000_0000_0400_0000_0000_0000_0000;
        #[cfg(feature = "vic3")] const DiplomaticCatalyst = 0x0000_0000_0000_0800_0000_0000_0000_0000;
        #[cfg(feature = "vic3")] const DiplomaticCatalystType = 0x0000_0000_0000_1000_0000_0000_0000_0000;
        #[cfg(feature = "vic3")] const DiplomaticCatalystCategory = 0x0000_0000_0000_2000_0000_0000_0000_0000;
        #[cfg(feature = "vic3")] const PoliticalLobby = 0x0000_0000_0000_4000_0000_0000_0000_0000;
        #[cfg(feature = "vic3")] const PoliticalLobbyType = 0x0000_0000_0000_8000_0000_0000_0000_0000;
        #[cfg(feature = "vic3")] const PoliticalLobbyAppeasement = 0x0000_0000_0001_0000_0000_0000_0000_0000;
        #[cfg(feature = "vic3")] const PowerBloc = 0x0000_0000_0002_0000_0000_0000_0000_0000;
        #[cfg(feature = "vic3")] const PowerBlocIdentity = 0x0000_0000_0004_0000_0000_0000_0000_0000;
        #[cfg(feature = "vic3")] const PowerBlocPrinciple = 0x0000_0000_0008_0000_0000_0000_0000_0000;
        #[cfg(feature = "vic3")] const HarvestCondition = 0x0000_0000_0010_0000_0000_0000_0000_0000;
        #[cfg(feature = "vic3")] const PoliticalMovementType = 0x0000_0000_0020_0000_0000_0000_0000_0000;
        #[cfg(feature = "vic3")] const HarvestConditionType = 0x0000_0000_0040_0000_0000_0000_0000_0000;

        #[cfg(feature = "imperator")] const Area = 0x0001_0000;
        #[cfg(feature = "imperator")] const CountryCulture = 0x0002_0000;
        #[cfg(feature = "imperator")] const CultureGroup = 0x0004_0000;
        #[cfg(feature = "imperator")] const Deity = 0x0008_0000;
        #[cfg(feature = "imperator")] const Family = 0x0010_0000;
        #[cfg(feature = "imperator")] const Governorship = 0x0020_0000;
        #[cfg(feature = "imperator")] const GreatWork = 0x0040_0000;
        #[cfg(feature = "imperator")] const Job = 0x0080_0000;
        #[cfg(feature = "imperator")] const Legion = 0x0100_0000;
        #[cfg(feature = "imperator")] const LevyTemplate = 0x0200_0000;
        #[cfg(feature = "imperator")] const Region = 0x0400_0000;
        #[cfg(feature = "imperator")] const Siege = 0x0800_0000;
        #[cfg(feature = "imperator")] const SubUnit = 0x1000_0000;
        #[cfg(feature = "imperator")] const Treasure = 0x2000_0000;
        #[cfg(feature = "imperator")] const Unit = 0x4000_0000;
    }
}

// These have to be expressed a bit awkwardly because the binary operators are not `const`.
// TODO: Scopes::all() returns a too-large set if multiple features are enabled.
impl Scopes {
    pub const fn non_primitive() -> Scopes {
        Scopes::all()
            .difference(Scopes::None.union(Scopes::Value).union(Scopes::Bool).union(Scopes::Flag))
    }

    pub const fn primitive() -> Scopes {
        Scopes::Value.union(Scopes::Bool).union(Scopes::Flag)
    }

    pub const fn all_but_none() -> Scopes {
        Scopes::all().difference(Scopes::None)
    }

    /// Read a scope type in string form and return it as a [`Scopes`] value.
    pub fn from_snake_case(s: &str) -> Option<Scopes> {
        match s {
            "none" => return Some(Scopes::None),
            "value" => return Some(Scopes::Value),
            "bool" => return Some(Scopes::Bool),
            "flag" => return Some(Scopes::Flag),
            _ => (),
        };
        match Game::game() {
            #[cfg(feature = "ck3")]
            Game::Ck3 => crate::ck3::scopes::scope_from_snake_case(s),
            #[cfg(feature = "vic3")]
            Game::Vic3 => crate::vic3::scopes::scope_from_snake_case(s),
            #[cfg(feature = "imperator")]
            Game::Imperator => crate::imperator::scopes::scope_from_snake_case(s),
        }
    }

    /// Similar to `from_snake_case`, but allows multiple scopes separated by `|`
    /// Returns None if any of the conversions fail.
    pub fn from_snake_case_multi(s: &str) -> Option<Scopes> {
        let mut scopes = Scopes::empty();
        for part in s.split('|') {
            if let Some(scope) = Scopes::from_snake_case(part) {
                scopes |= scope;
            } else {
                return None;
            }
        }
        // If `scopes` is still empty then probably `s` was empty.
        // Remember that `Scopes::empty()` is different from a bitfield containing `Scopes::None`.
        if scopes == Scopes::empty() {
            return None;
        }
        Some(scopes)
    }
}

impl Display for Scopes {
    fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> {
        if *self == Scopes::all() {
            write!(f, "any scope")
        } else if *self == Scopes::primitive() {
            write!(f, "any primitive scope")
        } else if *self == Scopes::non_primitive() {
            write!(f, "non-primitive scope")
        } else if *self == Scopes::all_but_none() {
            write!(f, "any except none scope")
        } else {
            match Game::game() {
                #[cfg(feature = "ck3")]
                Game::Ck3 => crate::ck3::scopes::display_fmt(*self, f),
                #[cfg(feature = "vic3")]
                Game::Vic3 => crate::vic3::scopes::display_fmt(*self, f),
                #[cfg(feature = "imperator")]
                Game::Imperator => crate::imperator::scopes::display_fmt(*self, f),
            }
        }
    }
}

/// A description of the constraints on a value with a prefix such as `var:` or `list_size:`
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum ArgumentValue {
    /// The value must be an expression that resolves to a scope object of the given type.
    #[cfg(any(feature = "ck3", feature = "vic3"))]
    Scope(Scopes),
    /// The value must be the name of an item of the given item type.
    Item(Item),
    /// The value can be either a Scope or an Item
    #[cfg(feature = "ck3")]
    ScopeOrItem(Scopes, Item),
    /// The value can be a trait name or `trait|track`.
    #[cfg(feature = "ck3")]
    TraitTrack,
    /// The value must be the name of a modif
    #[cfg(feature = "vic3")]
    Modif,
    /// The value can be anything
    UncheckedValue,
}

/// Look up an "event link", which is a script token that looks up something related
/// to a scope value and returns another scope value.
///
/// `name` is the token. `inscopes` is the known scope context of this token.
/// `inscopes` is only used for some special-case event links whose output scope type
/// depends on their input scope type.
///
/// Returns a pair of `Scopes`. The first is the scope types this token can accept as input,
/// and the second is the scope types it may return.
#[allow(unused_variables)] // inscopes is only used for vic3
pub fn scope_to_scope(name: &Token, inscopes: Scopes) -> Option<(Scopes, Scopes)> {
    let scope_to_scope = match Game::game() {
        #[cfg(feature = "ck3")]
        Game::Ck3 => crate::ck3::scopes::scope_to_scope,
        #[cfg(feature = "vic3")]
        Game::Vic3 => crate::vic3::scopes::scope_to_scope,
        #[cfg(feature = "imperator")]
        Game::Imperator => crate::imperator::scopes::scope_to_scope,
    };
    let scope_to_scope_removed = match Game::game() {
        #[cfg(feature = "ck3")]
        Game::Ck3 => crate::ck3::scopes::scope_to_scope_removed,
        #[cfg(feature = "vic3")]
        Game::Vic3 => crate::vic3::scopes::scope_to_scope_removed,
        #[cfg(feature = "imperator")]
        Game::Imperator => crate::imperator::scopes::scope_to_scope_removed,
    };

    let name_lc = name.as_str().to_ascii_lowercase();
    if let scopes @ Some((from, _)) = scope_to_scope(&name_lc) {
        #[cfg(feature = "vic3")]
        if Game::is_vic3() && name_lc == "type" {
            // Special case for "type" because it goes from specific scope types to specific
            // other scope types.
            let mut outscopes = Scopes::empty();
            if inscopes.contains(Scopes::Building) {
                outscopes |= Scopes::BuildingType;
            }
            if inscopes.contains(Scopes::Company) {
                outscopes |= Scopes::CompanyType;
            }
            if inscopes.contains(Scopes::DiplomaticPlay) {
                outscopes |= Scopes::DiplomaticPlayType;
            }
            if inscopes.contains(Scopes::DiplomaticCatalyst) {
                outscopes |= Scopes::DiplomaticCatalystType;
            }
            if inscopes.contains(Scopes::PoliticalLobby) {
                outscopes |= Scopes::PoliticalLobbyType;
            }
            if inscopes.contains(Scopes::Institution) {
                outscopes |= Scopes::InstitutionType;
            }
            if inscopes.contains(Scopes::InterestGroup) {
                outscopes |= Scopes::InterestGroupType;
            }
            if inscopes.contains(Scopes::Law) {
                outscopes |= Scopes::LawType;
            }
            if inscopes.contains(Scopes::PoliticalMovement) {
                outscopes |= Scopes::PoliticalMovementType;
            }
            if inscopes.contains(Scopes::HarvestCondition) {
                outscopes |= Scopes::HarvestConditionType;
            }
            if !outscopes.is_empty() {
                return Some((from, outscopes));
            }
        }
        scopes
    } else if let Some((version, explanation)) = scope_to_scope_removed(&name_lc) {
        let msg = format!("`{name}` was removed in {version}");
        err(ErrorKey::Removed).strong().msg(msg).info(explanation).loc(name).push();
        return Some((Scopes::all(), Scopes::all_but_none()));
    } else {
        None
    }
}

/// Look up a prefixed token that is used to look up items in the game database.
///
/// For example, `character:alexander_the_great` to fetch that character as a scope value.
///
/// Some prefixes have an input scope, and they look up something related to the input scope value.
///
/// Returns a pair of `Scopes` and the type of argument it accepts.
/// The first `Scopes` is the scope types this token can accept as input, and the second one is
/// the scope types it may return. The first will be `Scopes::None` if it needs no input.
pub fn scope_prefix(prefix: &Token) -> Option<(Scopes, Scopes, ArgumentValue)> {
    let scope_prefix = match Game::game() {
        #[cfg(feature = "ck3")]
        Game::Ck3 => crate::ck3::scopes::scope_prefix,
        #[cfg(feature = "vic3")]
        Game::Vic3 => crate::vic3::scopes::scope_prefix,
        #[cfg(feature = "imperator")]
        Game::Imperator => crate::imperator::scopes::scope_prefix,
    };
    let prefix_lc = prefix.as_str().to_ascii_lowercase();
    scope_prefix(&prefix_lc)
}

/// Look up a token that's an invalid target, and see if it might be missing a prefix.
/// Return the prefix if one was found.
///
/// `scopes` should be a singular `Scopes` flag.
///
/// Example: if the token is "irish" and `scopes` is `Scopes::Culture` then return
/// `Some("culture")` to indicate that the token should have been "culture:irish".
pub fn needs_prefix(arg: &str, data: &Everything, scopes: Scopes) -> Option<&'static str> {
    match Game::game() {
        #[cfg(feature = "ck3")]
        Game::Ck3 => crate::ck3::scopes::needs_prefix(arg, data, scopes),
        #[cfg(feature = "vic3")]
        Game::Vic3 => crate::vic3::scopes::needs_prefix(arg, data, scopes),
        #[cfg(feature = "imperator")]
        Game::Imperator => crate::imperator::scopes::needs_prefix(arg, data, scopes),
    }
}

/// Look up an iterator, which is a script element that executes its block multiple times, once for
/// each applicable scope value. Iterators may be builtin (the usual case) or may be scripted lists.
///
/// `name` is the name of the iterator, without its `any_`, `every_`, `random_` or `ordered_` prefix.
/// `sc` is a [`ScopeContext`], only used for validating scripted lists.
///
/// Returns a pair of `Scopes`. The first is the scope types this token can accept as input,
/// and the second is the scope types it may return.
/// The first will be `Scopes::None` if it needs no input.
pub fn scope_iterator(
    name: &Token,
    data: &Everything,
    sc: &mut ScopeContext,
) -> Option<(Scopes, Scopes)> {
    let scope_iterator = match Game::game() {
        #[cfg(feature = "ck3")]
        Game::Ck3 => crate::ck3::scopes::scope_iterator,
        #[cfg(feature = "vic3")]
        Game::Vic3 => crate::vic3::scopes::scope_iterator,
        #[cfg(feature = "imperator")]
        Game::Imperator => crate::imperator::scopes::scope_iterator,
    };
    let scope_iterator_removed = match Game::game() {
        #[cfg(feature = "ck3")]
        Game::Ck3 => crate::ck3::scopes::scope_iterator_removed,
        #[cfg(feature = "vic3")]
        Game::Vic3 => crate::vic3::scopes::scope_iterator_removed,
        #[cfg(feature = "imperator")]
        Game::Imperator => crate::imperator::scopes::scope_iterator_removed,
    };

    let name_lc = name.as_str().to_ascii_lowercase();
    if let scopes @ Some(_) = scope_iterator(&name_lc) {
        return scopes;
    } else if let Some((version, explanation)) = scope_iterator_removed(&name_lc) {
        let msg = format!("`{name}` iterators were removed in {version}");
        err(ErrorKey::Removed).strong().msg(msg).info(explanation).loc(name).push();
        return Some((Scopes::all(), Scopes::all()));
    } else if data.scripted_lists.exists(name.as_str()) {
        data.scripted_lists.validate_call(name, data, sc);
        return data
            .scripted_lists
            .base(name)
            .and_then(|base| scope_iterator(&base.as_str().to_ascii_lowercase()));
    }
    None
}