Skip to main content

rpfm_lib/files/bmd/civilian_deployment_list/
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
11use serde_derive::{Serialize, Deserialize};
12
13use crate::binary::{ReadBytes, WriteBytes};
14use crate::error::{Result, RLibError};
15use crate::files::{Decodeable, EncodeableExtraData, Encodeable};
16
17use super::*;
18
19/// Version of the civilian deployment records. No other is known.
20const DEPLOYMENT_VERSION: u16 = 1;
21
22//---------------------------------------------------------------------------//
23//                              Enum & Structs
24//---------------------------------------------------------------------------//
25
26/// Areas where civilians are deployed in a battle.
27#[derive(Default, PartialEq, Clone, Debug, Getters, MutGetters, Setters, Serialize, Deserialize)]
28#[getset(get = "pub", get_mut = "pub", set = "pub")]
29pub struct CivilianDeploymentList {
30    civilian_deployment_list: Vec<CivilianDeployment>,
31}
32
33/// An area where civilians are deployed.
34#[derive(Default, PartialEq, Clone, Debug, Getters, MutGetters, Setters, Serialize, Deserialize)]
35#[getset(get = "pub", get_mut = "pub", set = "pub")]
36pub struct CivilianDeployment {
37
38    /// Kind of area, like `Urban` or `Agricultural`.
39    kind: String,
40
41    /// Size of the area, like `small`, `medium` or `large`.
42    size: String,
43
44    /// Outline of the area.
45    area: Polygon2d,
46}
47
48//---------------------------------------------------------------------------//
49//                Implementation of CivilianDeploymentList
50//---------------------------------------------------------------------------//
51
52impl Decodeable for CivilianDeploymentList {
53
54    fn decode<R: ReadBytes>(data: &mut R, extra_data: &Option<DecodeableExtraData>) -> Result<Self> {
55        let mut decoded = Self::default();
56
57        for _ in 0..data.read_u32()? {
58            let version = data.read_u16()?;
59            if version != DEPLOYMENT_VERSION {
60                return Err(RLibError::DecodingFastBinUnsupportedVersion(String::from("CivilianDeployment"), version));
61            }
62
63            decoded.civilian_deployment_list.push(CivilianDeployment {
64                kind: data.read_sized_string_u8()?,
65                size: data.read_sized_string_u8()?,
66                area: Polygon2d::decode(data, extra_data)?,
67            });
68        }
69
70        Ok(decoded)
71    }
72}
73
74impl Encodeable for CivilianDeploymentList {
75
76    fn encode<W: WriteBytes>(&mut self, buffer: &mut W, extra_data: &Option<EncodeableExtraData>) -> Result<()> {
77        buffer.write_u32(self.civilian_deployment_list.len() as u32)?;
78
79        for deployment in &mut self.civilian_deployment_list {
80            buffer.write_u16(DEPLOYMENT_VERSION)?;
81            buffer.write_sized_string_u8(&deployment.kind)?;
82            buffer.write_sized_string_u8(&deployment.size)?;
83            deployment.area.encode(buffer, extra_data)?;
84        }
85
86        Ok(())
87    }
88}