rpfm_extensions/startpos/cai/mod.rs
1//---------------------------------------------------------------------------//
2// Copyright (c) 2017-2026 Ismael Gutiérrez González. All rights reserved.
3//
4// This file is part of the Rusted PackFile Manager (RPFM) project,
5// which can be found here: https://github.com/Frodo45127/rpfm.
6//
7// This file is licensed under the MIT license, which can be found here:
8// https://github.com/Frodo45127/rpfm/blob/master/LICENSE.
9//---------------------------------------------------------------------------//
10
11//! Writer of the campaign AI's state (`CAI_INTERFACE`).
12//!
13//! Every AI object (a mirror of a world object, a map region, an analyser...) has a component with an id from a single
14//! counter, in the order the game creates them, and AI records refer to each other by those ids. The writer first
15//! plans every id ([`plan::CaiPlan`]), then writes each list with its references.
16
17use rpfm_lib::error::Result;
18use rpfm_lib::files::esf::RecordNode;
19
20use super::layout::{child_path, StartposLayout};
21
22pub mod factions;
23pub mod interface;
24pub mod lists;
25pub mod managers;
26pub mod map;
27pub mod mobiles;
28pub mod pathfinder;
29pub mod plan;
30pub mod regions;
31pub mod settlements;
32pub mod stances;
33pub mod technologies;
34pub mod world;
35pub mod world_view;
36
37#[cfg(test)] mod cai_test;
38
39/// Path of the AI state.
40pub const CAI_PATH: &str = "CAMPAIGN_STARTPOS/CAMPAIGN_ENV/CAMPAIGN_MODEL/CAI_INTERFACE";
41
42/// Path of the AI's mirror of the world.
43pub const CAI_WORLD_PATH: &str = "CAMPAIGN_STARTPOS/CAMPAIGN_ENV/CAMPAIGN_MODEL/CAI_INTERFACE/CAI_WORLD";
44
45//---------------------------------------------------------------------------//
46// Implementations
47//---------------------------------------------------------------------------//
48
49/// Writes the component (`CAI_BDI_COMPONENT`) of an AI object.
50///
51/// # Arguments
52///
53/// * `layout` - Record layouts.
54/// * `parent` - Path of the record holding the component.
55/// * `id` - Component id.
56/// * `references` - Ids of the objects the component refers to (beliefs about it).
57///
58/// # Errors
59///
60/// Returns an error if the layout lacks the component.
61pub(crate) fn component(layout: &StartposLayout, parent: &str, id: u32, references: Vec<u32>) -> Result<RecordNode> {
62 let mut record = layout.builder(&child_path(parent, "CAI_BDI_COMPONENT"))?;
63 record.group()?
64 .set_u32("^+1", id)?
65 .set_u32s("CAI_BDI_COMPONENT_BLOCK_OWNS+1", references)?;
66 record.build()
67}