rpfm_extensions/startpos/world/writer/slaves.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 world's slaves (`WORLD/SLAVES`): the slave points of each region's settlement
12//! (`start_pos_settlements.startpos_slave_points`), in region id order.
13
14use rpfm_lib::error::Result;
15use rpfm_lib::files::esf::RecordNode;
16
17use super::{child, WorldInputs, WorldPlan, WORLD_PATH};
18use crate::startpos::layout::child_path;
19
20#[cfg(test)] #[path = "slaves_test.rs"] mod slaves_test;
21
22/// Writes the slaves of the world.
23///
24/// # Arguments
25///
26/// * `inputs` - Tables, startpos rows and layouts.
27/// * `plan` - Order and runtime ids of the regions.
28///
29/// # Errors
30///
31/// Returns an error if the layout lacks the records.
32pub fn write_slaves(inputs: &WorldInputs, plan: &WorldPlan) -> Result<RecordNode> {
33 let path = child_path(WORLD_PATH, "SLAVES");
34 let mut block = child(inputs.layout, &path, "SLAVES_BLOCK")?;
35 for region in plan.regions() {
36 let points = inputs.index.settlement_of(region.key()).map(|settlement| *settlement.slave_points()).unwrap_or_default();
37 block.push_group()?
38 .set_u32("^+1", plan.region_id(region.key()).unwrap_or_default())?
39 .set_u32("^+2", u32::try_from(points).unwrap_or_default())?;
40 }
41
42 let mut record = inputs.layout.builder(&path)?;
43 record.group()?.set_record("SLAVES_BLOCK", Some(block.build()?))?;
44 record.build()
45}