anvil-sdk 0.1.8

Safe Rust API for plugins of Anvil, the plugin loader for Europa Universalis IV
Documentation
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
//! `anvil-game.world`: the world of the running session, read straight from
//! the game (anvil-game 1.11.0; modifiers, units and the selection 1.15.0;
//! rulers, heirs, consorts, flags and variables 1.20.0).
//!
//! **Thread.** Call it only on the game's main thread after an outer frame:
//! from a handler of an anvil-game event ([`game`](crate::game)) or from
//! [`Game::run_on_main_thread`](crate::game::Game::run_on_main_thread) work,
//! or from a panel click. Anywhere else, and outside a session, a call answers
//! [`ErrorCode::WrongPhase`] and reads nothing.
//!
//! **Results.** [`ErrorCode::NotFound`] for an unknown province ID, tag, war
//! or unit; [`ErrorCode::NotDeclared`] for a group of calls whose symbols a
//! port declared absent; [`ErrorCode::Unsupported`] for a call the running
//! anvil-game is too old to have.
//!
//! **What it shows.** Everything, also under terra incognita and in
//! multiplayer, where that is more than the player sees. A read changes
//! nothing, so it cannot cause a desync. Values marked "× 1000" are as the
//! game stores them, as [`Milli`].
//!
//! [`ErrorCode::WrongPhase`]: crate::ErrorCode::WrongPhase
//! [`ErrorCode::NotFound`]: crate::ErrorCode::NotFound
//! [`ErrorCode::NotDeclared`]: crate::ErrorCode::NotDeclared
//! [`ErrorCode::Unsupported`]: crate::ErrorCode::Unsupported

use std::fmt;
use std::mem::size_of;

use crate::error::{check, Result};
use crate::game::Date;
use crate::host::Host;
use crate::sys::{
    AnvilGameCountry, AnvilGameDate, AnvilGameFlag, AnvilGameMonarch, AnvilGameProvince,
    AnvilGameTag, AnvilGameUnit, AnvilGameUnitRef, AnvilGameWar, AnvilGameWorldService,
    ANVIL_GAME_COUNTRY_OBSERVER, ANVIL_GAME_MONARCH_CONSORT, ANVIL_GAME_MONARCH_FEMALE,
    ANVIL_GAME_MONARCH_HEIR, ANVIL_GAME_MONARCH_REGENCY, ANVIL_GAME_MONARCH_RULER,
    ANVIL_GAME_PROVINCE_LAND, ANVIL_GAME_PROVINCE_WASTELAND, ANVIL_GAME_UNIT_ARMY,
    ANVIL_GAME_UNIT_NAVY, ANVIL_GAME_VALUE_MANPOWER, ANVIL_GAME_VALUE_MONTHLY_INCOME,
    ANVIL_GAME_VALUE_PRESTIGE, ANVIL_GAME_VALUE_STABILITY, ANVIL_GAME_VALUE_TREASURY,
    ANVIL_GAME_WAR_ATTACKERS, ANVIL_GAME_WAR_DEFENDERS, ANVIL_GAME_WORLD_SERVICE_VERSION,
};
use crate::text::{read_list, read_text, Tag};

/// The service's name.
pub const SERVICE: &std::ffi::CStr = c"anvil-game.world";
/// The service's major version.
pub const VERSION: u32 = ANVIL_GAME_WORLD_SERVICE_VERSION;

/// A value the game stores × 1000: `Milli(4500)` is 4.5.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Milli(pub i32);

impl Milli {
    /// The value as a float.
    pub fn get(self) -> f64 {
        f64::from(self.0) / 1000.0
    }
}

impl fmt::Display for Milli {
    /// With up to three decimals, trailing zeros dropped: `4.5`, `-12`, `0.125`.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let sign = if self.0 < 0 { "-" } else { "" };
        let value = self.0.unsigned_abs();
        let (whole, part) = (value / 1000, value % 1000);
        if part == 0 {
            return write!(f, "{sign}{whole}");
        }
        let decimals = format!("{part:03}");
        write!(f, "{sign}{whole}.{}", decimals.trim_end_matches('0'))
    }
}

/// A province.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Province {
    /// Its ID.
    pub id: i32,
    /// Its owner; `None` for none.
    pub owner: Option<Tag>,
    /// Its controller.
    pub controller: Option<Tag>,
    /// Base tax, without the capital's bonus.
    pub base_tax: Milli,
    /// Base production.
    pub base_production: Milli,
    /// Base manpower.
    pub base_manpower: Milli,
    /// Not a sea or a lake.
    pub land: bool,
    /// A wasteland.
    pub wasteland: bool,
}

/// A country. A value this game build does not have is `None`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Country {
    /// Its tag.
    pub tag: Option<Tag>,
    /// Its capital province; `None` for none.
    pub capital: Option<i32>,
    /// The provinces it owns; above 0 when it exists on the map.
    pub province_count: i32,
    /// The observer's pseudo-country.
    pub observer: bool,
    /// Ducats.
    pub treasury: Option<Milli>,
    /// Stability.
    pub stability: Option<Milli>,
    /// Prestige.
    pub prestige: Option<Milli>,
    /// Manpower in the game's thousands: `Milli(25000)` is 25 000 men.
    pub manpower: Option<Milli>,
    /// The income total of the economy view, before expenses, as of the last
    /// monthly update.
    pub monthly_income: Option<Milli>,
}

/// A running war.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct War {
    /// Its id: the same while the war runs (a later war may reuse it).
    pub id: u64,
    /// Who started it.
    pub attacker: Option<Tag>,
    /// Whom it was declared on.
    pub defender: Option<Tag>,
}

/// A side of a war.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Side {
    /// The attacker and its allies.
    Attackers,
    /// The defender and its allies.
    Defenders,
}

/// An army or a navy.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum UnitKind {
    /// An army.
    Army,
    /// A navy.
    Navy,
    /// A value this crate does not know.
    Other(u32),
}

impl UnitKind {
    fn from_raw(kind: u32) -> Self {
        match kind {
            ANVIL_GAME_UNIT_ARMY => Self::Army,
            ANVIL_GAME_UNIT_NAVY => Self::Navy,
            other => Self::Other(other),
        }
    }
}

/// What a unit does on its own, by the game's numbers (`ANVIL_GAME_MISSION_*`).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Mission(pub i32);

impl Mission {
    /// Not one the service could read.
    pub const UNKNOWN: Self = Self(crate::sys::ANVIL_GAME_MISSION_UNKNOWN);
    /// No mission: the player moves it.
    pub const NONE: Self = Self(crate::sys::ANVIL_GAME_MISSION_NONE as i32);
    /// Besiege the enemy's provinces on its own.
    pub const SIEGE: Self = Self(crate::sys::ANVIL_GAME_MISSION_SIEGE as i32);
    /// Hunt rebels.
    pub const HUNT_REBELS: Self = Self(crate::sys::ANVIL_GAME_MISSION_HUNT_REBELS as i32);
    /// Drill.
    pub const DRILL: Self = Self(crate::sys::ANVIL_GAME_MISSION_DRILL as i32);
}

/// A unit: an army or a navy.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Unit {
    /// Army or navy.
    pub kind: UnitKind,
    /// The game's id: the same while the unit exists.
    pub id: u64,
    /// The country it belongs to.
    pub country: Option<Tag>,
    /// The province it is in or at; `None` for none.
    pub location: Option<i32>,
    /// What it does on its own.
    pub mission: Mission,
    /// Regiments or ships.
    pub subunits: u32,
    /// The sum of the regiments' or ships' strength (men for an army).
    pub strength: Milli,
    /// Its leader's id; `None` for no leader.
    pub leader: Option<u64>,
}

/// One unit of the player's selection.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct UnitRef {
    /// The unit's id.
    pub id: u64,
    /// The country it belongs to (a player may select units of others to look).
    pub country: Option<Tag>,
    /// Army or navy.
    pub kind: UnitKind,
}

/// The id of a modifier, from its script key ([`World::modifier_id`]).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct ModifierId(pub i32);

/// A country's ruler, heir or consort (anvil-game 1.20.0).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum MonarchKind {
    /// The ruler.
    Ruler,
    /// The heir.
    Heir,
    /// The consort.
    Consort,
}

impl MonarchKind {
    fn raw(self) -> u32 {
        match self {
            Self::Ruler => ANVIL_GAME_MONARCH_RULER,
            Self::Heir => ANVIL_GAME_MONARCH_HEIR,
            Self::Consort => ANVIL_GAME_MONARCH_CONSORT,
        }
    }

    fn from_raw(kind: u32) -> Self {
        match kind {
            ANVIL_GAME_MONARCH_HEIR => Self::Heir,
            ANVIL_GAME_MONARCH_CONSORT => Self::Consort,
            _ => Self::Ruler,
        }
    }
}

/// A ruler, heir or consort, as the game stores them.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Monarch {
    /// Ruler, heir or consort.
    pub kind: MonarchKind,
    /// The game's id of the person.
    pub id: u64,
    /// The country asked about.
    pub country: Option<Tag>,
    /// Female.
    pub female: bool,
    /// A regency council or a regent rules for them.
    pub regency: bool,
    /// Administrative skill, without modifiers.
    pub adm: i32,
    /// Diplomatic skill.
    pub dip: i32,
    /// Military skill.
    pub mil: i32,
    /// An heir's claim strength; 0 for a ruler or a consort.
    pub claim: Milli,
    /// The birth date.
    pub birth: Date,
    /// The id of the leader they also are ([`Unit::leader`]); `None` for none.
    pub leader: Option<u64>,
}

/// The id of a flag, from its name ([`World::flag_id`]).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct FlagId(pub i32);

/// A flag that is set, and when it was set.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Flag {
    /// Its id; [`World::flag_name`] gives the name.
    pub id: FlagId,
    /// When it was set.
    pub date: Date,
}

/// The `anvil-game.world` service.
#[derive(Clone, Copy)]
pub struct World {
    table: &'static AnvilGameWorldService,
}

impl fmt::Debug for World {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("World")
            .field("size", &self.table.size)
            .finish()
    }
}

fn sized<T>() -> T {
    // SAFETY: the world's structs are plain data for which zero is valid.
    let mut value: T = unsafe { std::mem::zeroed() };
    let size = size_of::<T>() as u32;
    // SAFETY: each of them starts with `uint32_t size`.
    unsafe { std::ptr::from_mut(&mut value).cast::<u32>().write(size) };
    value
}

fn known(value: i32, values: u32, bit: u32) -> Option<Milli> {
    (values & bit != 0).then_some(Milli(value))
}

const EMPTY_TAG: AnvilGameTag = AnvilGameTag { text: [0; 4] };

impl World {
    /// The service; `None` without anvil-game 1.11.0 or on a game build whose
    /// symbols for it are declared absent.
    pub fn get(host: &Host) -> Option<Self> {
        // SAFETY: the table type of anvil-game.world version 1.
        let table = unsafe { host.service::<AnvilGameWorldService>(SERVICE, VERSION) }?;
        Some(Self { table })
    }

    /// The C table, for what this crate does not wrap.
    pub fn c_api(&self) -> &'static AnvilGameWorldService {
        self.table
    }

    /// The country the game treats as the player's (the observed one in observe mode).
    pub fn player(&self) -> Result<Option<Tag>> {
        let f = function!(self.table, AnvilGameWorldService, player)?;
        let mut tag = EMPTY_TAG;
        // SAFETY: the service's function with an out pointer.
        check(unsafe { f(&mut tag) }, || "world player")?;
        Ok(Tag::from_raw(&tag))
    }

    /// Valid province IDs are `1..limit`; 0 where a call is not allowed.
    pub fn province_limit(&self) -> i32 {
        // SAFETY: the service's function.
        function!(self.table, AnvilGameWorldService, province_limit).map_or(0, |f| unsafe { f() })
    }

    /// A province.
    pub fn province(&self, id: i32) -> Result<Province> {
        let f = function!(self.table, AnvilGameWorldService, province)?;
        let mut raw: AnvilGameProvince = sized();
        // SAFETY: the service's function; `size` says how much it may write.
        check(unsafe { f(id, &mut raw) }, || {
            format!("world province {id}")
        })?;
        Ok(Province {
            id: raw.id,
            owner: Tag::from_raw(&raw.owner),
            controller: Tag::from_raw(&raw.controller),
            base_tax: Milli(raw.base_tax),
            base_production: Milli(raw.base_production),
            base_manpower: Milli(raw.base_manpower),
            land: raw.flags & ANVIL_GAME_PROVINCE_LAND != 0,
            wasteland: raw.flags & ANVIL_GAME_PROVINCE_WASTELAND != 0,
        })
    }

    /// The name the game displays (it follows the owner's culture).
    pub fn province_name(&self, id: i32) -> Result<String> {
        let f = function!(self.table, AnvilGameWorldService, province_name)?;
        // SAFETY: the service fills at most `capacity` bytes.
        read_text(
            || format!("world province_name {id}"),
            |buffer, capacity, length| unsafe { f(id, buffer, capacity, length) },
        )
    }

    /// The tags with a core on the province.
    pub fn province_cores(&self, id: i32) -> Result<Vec<Tag>> {
        let f = function!(self.table, AnvilGameWorldService, province_cores)?;
        // SAFETY: the service fills at most `capacity` tags.
        let raw = read_list(
            EMPTY_TAG,
            || format!("world province_cores {id}"),
            |out, capacity, count| unsafe { f(id, out, capacity, count) },
        )?;
        Ok(raw.iter().filter_map(Tag::from_raw).collect())
    }

    /// A country (the tag ignores case).
    pub fn country(&self, tag: Tag) -> Result<Country> {
        let f = function!(self.table, AnvilGameWorldService, country)?;
        let text = tag.c_text();
        let mut raw: AnvilGameCountry = sized();
        // SAFETY: a NUL-terminated tag; `size` says how much it may write.
        check(unsafe { f(text.as_ptr(), &mut raw) }, || {
            format!("world country {tag}")
        })?;
        let v = raw.values;
        Ok(Country {
            tag: Tag::from_raw(&raw.tag),
            capital: (raw.capital != 0).then_some(raw.capital),
            province_count: raw.province_count,
            observer: raw.flags & ANVIL_GAME_COUNTRY_OBSERVER != 0,
            treasury: known(raw.treasury, v, ANVIL_GAME_VALUE_TREASURY),
            stability: known(raw.stability, v, ANVIL_GAME_VALUE_STABILITY),
            prestige: known(raw.prestige, v, ANVIL_GAME_VALUE_PRESTIGE),
            manpower: known(raw.manpower, v, ANVIL_GAME_VALUE_MANPOWER),
            monthly_income: known(raw.monthly_income, v, ANVIL_GAME_VALUE_MONTHLY_INCOME),
        })
    }

    /// The IDs of the provinces a country owns, in the game's order.
    pub fn country_provinces(&self, tag: Tag) -> Result<Vec<i32>> {
        let f = function!(self.table, AnvilGameWorldService, country_provinces)?;
        let text = tag.c_text();
        // SAFETY: a NUL-terminated tag; the service fills at most `capacity` IDs.
        read_list(
            0i32,
            || format!("world country_provinces {tag}"),
            |out, capacity, count| unsafe { f(text.as_ptr(), out, capacity, count) },
        )
    }

    /// The ids of the running wars.
    pub fn war_ids(&self) -> Result<Vec<u64>> {
        let f = function!(self.table, AnvilGameWorldService, war_ids)?;
        // SAFETY: the service fills at most `capacity` ids.
        read_list(
            0u64,
            || "world war_ids".to_owned(),
            |out, capacity, count| unsafe { f(out, capacity, count) },
        )
    }

    /// A running war.
    pub fn war(&self, id: u64) -> Result<War> {
        let f = function!(self.table, AnvilGameWorldService, war)?;
        let mut raw: AnvilGameWar = sized();
        // SAFETY: `size` says how much it may write.
        check(unsafe { f(id, &mut raw) }, || format!("world war {id:#x}"))?;
        Ok(War {
            id: raw.id,
            attacker: Tag::from_raw(&raw.attacker),
            defender: Tag::from_raw(&raw.defender),
        })
    }

    /// A war's name as the game shows it.
    pub fn war_name(&self, id: u64) -> Result<String> {
        let f = function!(self.table, AnvilGameWorldService, war_name)?;
        // SAFETY: the service fills at most `capacity` bytes.
        read_text(
            || format!("world war_name {id:#x}"),
            |buffer, capacity, length| unsafe { f(id, buffer, capacity, length) },
        )
    }

    /// The countries on one side of a war now.
    pub fn war_side(&self, id: u64, side: Side) -> Result<Vec<Tag>> {
        let f = function!(self.table, AnvilGameWorldService, war_side)?;
        let side_raw = match side {
            Side::Attackers => ANVIL_GAME_WAR_ATTACKERS,
            Side::Defenders => ANVIL_GAME_WAR_DEFENDERS,
        };
        // SAFETY: the service fills at most `capacity` tags.
        let raw = read_list(
            EMPTY_TAG,
            || format!("world war_side {id:#x}"),
            |out, capacity, count| unsafe { f(id, side_raw, out, capacity, count) },
        )?;
        Ok(raw.iter().filter_map(Tag::from_raw).collect())
    }

    /// The id of a modifier from its script key (`land_morale`,
    /// `local_tax_modifier`), case included. It holds as long as the game
    /// runs: ask once, as this walks every definition.
    pub fn modifier_id(&self, key: &str) -> Result<ModifierId> {
        let f = function!(self.table, AnvilGameWorldService, modifier_id)?;
        let key_c = crate::text::c_string(key);
        let mut id = 0i32;
        // SAFETY: a NUL-terminated key and an out pointer.
        check(unsafe { f(key_c.as_ptr(), &mut id) }, || {
            format!("world modifier_id {key}")
        })?;
        Ok(ModifierId(id))
    }

    /// A country's modifier as the game uses it (a yes-or-no one is 1000 or 0).
    pub fn country_modifier(&self, tag: Tag, id: ModifierId) -> Result<Milli> {
        let f = function!(self.table, AnvilGameWorldService, country_modifier)?;
        let text = tag.c_text();
        let mut value = 0i32;
        // SAFETY: a NUL-terminated tag and an out pointer.
        check(unsafe { f(text.as_ptr(), id.0, &mut value) }, || {
            format!("world country_modifier {tag} {}", id.0)
        })?;
        Ok(Milli(value))
    }

    /// A province's own modifier (not its owner's values).
    pub fn province_modifier(&self, province: i32, id: ModifierId) -> Result<Milli> {
        let f = function!(self.table, AnvilGameWorldService, province_modifier)?;
        let mut value = 0i32;
        // SAFETY: an out pointer.
        check(unsafe { f(province, id.0, &mut value) }, || {
            format!("world province_modifier {province} {}", id.0)
        })?;
        Ok(Milli(value))
    }

    /// The ids of a country's armies and navies, in the game's order.
    pub fn country_units(&self, tag: Tag) -> Result<Vec<u64>> {
        let f = function!(self.table, AnvilGameWorldService, country_units)?;
        let text = tag.c_text();
        // SAFETY: a NUL-terminated tag; the service fills at most `capacity` ids.
        read_list(
            0u64,
            || format!("world country_units {tag}"),
            |out, capacity, count| unsafe { f(text.as_ptr(), out, capacity, count) },
        )
    }

    /// A unit by its country and id.
    pub fn unit(&self, tag: Tag, id: u64) -> Result<Unit> {
        let f = function!(self.table, AnvilGameWorldService, unit)?;
        let text = tag.c_text();
        let mut raw: AnvilGameUnit = sized();
        // SAFETY: a NUL-terminated tag; `size` says how much it may write.
        check(unsafe { f(text.as_ptr(), id, &mut raw) }, || {
            format!("world unit {tag} {id:#x}")
        })?;
        Ok(Unit {
            kind: UnitKind::from_raw(raw.kind),
            id: raw.id,
            country: Tag::from_raw(&raw.country),
            location: (raw.location != 0).then_some(raw.location),
            mission: Mission(raw.mission),
            subunits: raw.subunits,
            strength: Milli(raw.strength),
            leader: (raw.leader != 0).then_some(raw.leader),
        })
    }

    /// The name the game shows for a unit ("1st Army").
    pub fn unit_name(&self, tag: Tag, id: u64) -> Result<String> {
        let f = function!(self.table, AnvilGameWorldService, unit_name)?;
        let text = tag.c_text();
        // SAFETY: a NUL-terminated tag; the service fills at most `capacity` bytes.
        read_text(
            || format!("world unit_name {tag} {id:#x}"),
            |buffer, capacity, length| unsafe { f(text.as_ptr(), id, buffer, capacity, length) },
        )
    }

    /// A unit's leader's name; `NotFound` also for a unit without one.
    pub fn unit_leader_name(&self, tag: Tag, id: u64) -> Result<String> {
        let f = function!(self.table, AnvilGameWorldService, unit_leader_name)?;
        let text = tag.c_text();
        // SAFETY: as in unit_name.
        read_text(
            || format!("world unit_leader_name {tag} {id:#x}"),
            |buffer, capacity, length| unsafe { f(text.as_ptr(), id, buffer, capacity, length) },
        )
    }

    /// The armies and navies the player has selected, in the game's order.
    pub fn selected_units(&self) -> Result<Vec<UnitRef>> {
        let f = function!(self.table, AnvilGameWorldService, selected_units)?;
        let empty = AnvilGameUnitRef {
            id: 0,
            country: EMPTY_TAG,
            kind: 0,
        };
        // SAFETY: the service fills at most `capacity` entries.
        let raw = read_list(
            empty,
            || "world selected_units".to_owned(),
            |out, capacity, count| unsafe { f(out, capacity, count) },
        )?;
        Ok(raw
            .iter()
            .map(|r| UnitRef {
                id: r.id,
                country: Tag::from_raw(&r.country),
                kind: UnitKind::from_raw(r.kind),
            })
            .collect())
    }

    /// A country's ruler, heir or consort. `NotFound` when it has none: the
    /// game then holds a placeholder that every such country shares, and the
    /// service never reads it. A country no longer on the map keeps its last
    /// ruler: check [`Country::province_count`] when that matters.
    pub fn monarch(&self, tag: Tag, kind: MonarchKind) -> Result<Monarch> {
        let f = function!(self.table, AnvilGameWorldService, monarch)?;
        let text = tag.c_text();
        let mut raw: AnvilGameMonarch = sized();
        // SAFETY: a NUL-terminated tag; `size` says how much it may write.
        check(unsafe { f(text.as_ptr(), kind.raw(), &mut raw) }, || {
            format!("world monarch {tag} {kind:?}")
        })?;
        Ok(Monarch {
            kind: MonarchKind::from_raw(raw.kind),
            id: raw.id,
            country: Tag::from_raw(&raw.country),
            female: raw.flags & ANVIL_GAME_MONARCH_FEMALE != 0,
            regency: raw.flags & ANVIL_GAME_MONARCH_REGENCY != 0,
            adm: raw.adm,
            dip: raw.dip,
            mil: raw.mil,
            claim: Milli(raw.claim),
            birth: Date::from_raw(&raw.birth),
            leader: (raw.leader != 0).then_some(raw.leader),
        })
    }

    /// Their name as the game keeps it; empty in an interregnum.
    pub fn monarch_name(&self, tag: Tag, kind: MonarchKind) -> Result<String> {
        let f = function!(self.table, AnvilGameWorldService, monarch_name)?;
        let text = tag.c_text();
        // SAFETY: a NUL-terminated tag; the service fills at most `capacity` bytes.
        read_text(
            || format!("world monarch_name {tag} {kind:?}"),
            |buffer, capacity, length| unsafe {
                f(text.as_ptr(), kind.raw(), buffer, capacity, length)
            },
        )
    }

    /// Their dynasty as the game shows it (a regency council shows the heir's).
    pub fn monarch_dynasty(&self, tag: Tag, kind: MonarchKind) -> Result<String> {
        let f = function!(self.table, AnvilGameWorldService, monarch_dynasty)?;
        let text = tag.c_text();
        // SAFETY: as in monarch_name.
        read_text(
            || format!("world monarch_dynasty {tag} {kind:?}"),
            |buffer, capacity, length| unsafe {
                f(text.as_ptr(), kind.raw(), buffer, capacity, length)
            },
        )
    }

    /// The id of a flag from its name, case included. It holds as long as the
    /// game runs: ask once. `NotFound` for a name nothing has set yet in this
    /// run of the game.
    pub fn flag_id(&self, name: &str) -> Result<FlagId> {
        let f = function!(self.table, AnvilGameWorldService, flag_id)?;
        let name_c = crate::text::c_string(name);
        let mut id = 0i32;
        // SAFETY: a NUL-terminated name and an out pointer.
        check(unsafe { f(name_c.as_ptr(), &mut id) }, || {
            format!("world flag_id {name}")
        })?;
        Ok(FlagId(id))
    }

    /// A flag's name.
    pub fn flag_name(&self, id: FlagId) -> Result<String> {
        let f = function!(self.table, AnvilGameWorldService, flag_name)?;
        // SAFETY: the service fills at most `capacity` bytes.
        read_text(
            || format!("world flag_name {}", id.0),
            |buffer, capacity, length| unsafe { f(id.0, buffer, capacity, length) },
        )
    }

    /// A country's flags, with the date each was set.
    pub fn country_flags(&self, tag: Tag) -> Result<Vec<Flag>> {
        let f = function!(self.table, AnvilGameWorldService, country_flags)?;
        let text = tag.c_text();
        // SAFETY: a NUL-terminated tag; the service fills at most `capacity` flags.
        flags(
            || format!("world country_flags {tag}"),
            |out, capacity, count| unsafe { f(text.as_ptr(), out, capacity, count) },
        )
    }

    /// A province's flags as `has_province_flag` sees them, its permanent ones included.
    pub fn province_flags(&self, province: i32) -> Result<Vec<Flag>> {
        let f = function!(self.table, AnvilGameWorldService, province_flags)?;
        // SAFETY: the service fills at most `capacity` flags.
        flags(
            || format!("world province_flags {province}"),
            |out, capacity, count| unsafe { f(province, out, capacity, count) },
        )
    }

    /// A ruler's, heir's or consort's flags; `NotFound` as for [`World::monarch`].
    pub fn monarch_flags(&self, tag: Tag, kind: MonarchKind) -> Result<Vec<Flag>> {
        let f = function!(self.table, AnvilGameWorldService, monarch_flags)?;
        let text = tag.c_text();
        // SAFETY: a NUL-terminated tag; the service fills at most `capacity` flags.
        flags(
            || format!("world monarch_flags {tag} {kind:?}"),
            |out, capacity, count| unsafe { f(text.as_ptr(), kind.raw(), out, capacity, count) },
        )
    }

    /// The global flags.
    pub fn global_flags(&self) -> Result<Vec<Flag>> {
        let f = function!(self.table, AnvilGameWorldService, global_flags)?;
        // SAFETY: the service fills at most `capacity` flags.
        flags(
            || "world global_flags".to_owned(),
            |out, capacity, count| unsafe { f(out, capacity, count) },
        )
    }

    /// A country's variable by its name, case included. `NotFound` when it
    /// has none of that name (the game's triggers take that as 0).
    pub fn country_variable(&self, tag: Tag, name: &str) -> Result<Milli> {
        let f = function!(self.table, AnvilGameWorldService, country_variable)?;
        let text = tag.c_text();
        let name_c = crate::text::c_string(name);
        let mut value = 0i32;
        // SAFETY: a NUL-terminated tag and name, and an out pointer.
        let result = unsafe { f(text.as_ptr(), name_c.as_ptr(), &mut value) };
        check(result, || format!("world country_variable {tag} {name}"))?;
        Ok(Milli(value))
    }

    /// A province's variable by its name.
    pub fn province_variable(&self, province: i32, name: &str) -> Result<Milli> {
        let f = function!(self.table, AnvilGameWorldService, province_variable)?;
        let name_c = crate::text::c_string(name);
        let mut value = 0i32;
        // SAFETY: a NUL-terminated name and an out pointer.
        check(unsafe { f(province, name_c.as_ptr(), &mut value) }, || {
            format!("world province_variable {province} {name}")
        })?;
        Ok(Milli(value))
    }

    /// The names of a country's variables, in the game's order.
    pub fn country_variable_names(&self, tag: Tag) -> Result<Vec<String>> {
        let f = function!(self.table, AnvilGameWorldService, country_variable_names)?;
        let text = tag.c_text();
        // SAFETY: a NUL-terminated tag; the service fills at most `capacity` bytes.
        let all = read_text(
            || format!("world country_variable_names {tag}"),
            |buffer, capacity, length| unsafe { f(text.as_ptr(), buffer, capacity, length) },
        )?;
        Ok(split_names(&all))
    }

    /// The names of a province's variables, in the game's order.
    pub fn province_variable_names(&self, province: i32) -> Result<Vec<String>> {
        let f = function!(self.table, AnvilGameWorldService, province_variable_names)?;
        // SAFETY: the service fills at most `capacity` bytes.
        let all = read_text(
            || format!("world province_variable_names {province}"),
            |buffer, capacity, length| unsafe { f(province, buffer, capacity, length) },
        )?;
        Ok(split_names(&all))
    }
}

/// Names that each end with a NUL, one after the other.
fn split_names(all: &str) -> Vec<String> {
    all.split_terminator('\0').map(str::to_owned).collect()
}

fn flags(
    context: impl Fn() -> String,
    call: impl FnMut(*mut AnvilGameFlag, u32, *mut u32) -> crate::sys::AnvilResult,
) -> Result<Vec<Flag>> {
    let empty = AnvilGameFlag {
        id: 0,
        reserved: 0,
        date: AnvilGameDate {
            size: 0,
            year: 0,
            month: 0,
            day: 0,
            session: 0,
        },
    };
    let raw = read_list(empty, context, call)?;
    Ok(raw
        .iter()
        .map(|f| Flag {
            id: FlagId(f.id),
            date: Date::from_raw(&f.date),
        })
        .collect())
}