Skip to main content

rpfm_lib/files/bmd/common/flags/
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//! Flag definitions for BMD entities.
12//!
13//! This module defines the [`Flags`] structure containing boolean flags that control
14//! entity behavior, placement, and visibility in BMD files.
15//!
16//! # Flag Categories
17//!
18//! - **Placement**: `allow_in_outfield`, `clamp_to_surface`, `clamp_to_water_surface`
19//! - **Seasonal**: `spring`, `summer`, `autumn`, `winter`
20//! - **Visibility**: `visible_in_tactical_view`, `visible_in_tactical_view_only`
21//!
22//! # Supported Versions
23//!
24//! - **Version 1**: Initial format
25//! - **Version 2**: Enhanced format
26//! - **Version 3**: Additional flags
27//! - **Version 4**: Current format
28//!
29//! # Usage
30//!
31//! ```ignore
32//! use rpfm_lib::files::bmd::common::flags::Flags;
33//! use rpfm_lib::files::Decodeable;
34//!
35//! let flags = Flags::decode(&mut reader, &None)?;
36//! if *flags.spring() && *flags.visible_in_tactical_view() {
37//!     println!("Visible in spring tactical view");
38//! }
39//! ```
40
41use serde_derive::{Serialize, Deserialize};
42
43use crate::binary::{ReadBytes, WriteBytes};
44use crate::error::{Result, RLibError};
45use crate::files::{Decodeable, EncodeableExtraData, Encodeable};
46
47use super::*;
48
49mod v1;
50mod v2;
51mod v3;
52mod v4;
53
54//---------------------------------------------------------------------------//
55//                              Enum & Structs
56//---------------------------------------------------------------------------//
57
58/// Boolean flags controlling entity behavior and visibility.
59///
60/// This structure contains various flags that control how entities (buildings,
61/// props, etc.) behave in the game, including placement rules, seasonal visibility,
62/// and tactical view settings.
63///
64/// # Flag Categories
65///
66/// ## Placement Flags
67/// - `allow_in_outfield`: Entity can be placed outside the playable area
68/// - `clamp_to_surface`: Entity position is clamped to terrain surface
69/// - `clamp_to_water_surface`: Entity position is clamped to water surface
70///
71/// ## Seasonal Flags
72/// - `spring`, `summer`, `autumn`, `winter`: Entity is visible in specified seasons
73///
74/// ## Visibility Flags
75/// - `visible_in_tactical_view`: Entity is visible in tactical camera view
76/// - `visible_in_tactical_view_only`: Entity is only visible in tactical view
77///
78/// # Example
79///
80/// ```ignore
81/// use rpfm_lib::files::bmd::common::flags::Flags;
82///
83/// let mut flags = Flags::default();
84/// flags.set_serialise_version(4);
85/// flags.set_spring(true);
86/// flags.set_summer(true);
87/// flags.set_clamp_to_surface(true);
88/// flags.set_visible_in_tactical_view(true);
89/// ```
90#[derive(Default, PartialEq, Clone, Debug, Getters, MutGetters, Setters, Serialize, Deserialize)]
91#[getset(get = "pub", get_mut = "pub", set = "pub")]
92pub struct Flags {
93    /// Format version number (1-4).
94    serialise_version: u16,
95
96    /// Whether the entity can be placed in the outfield (outside playable area).
97    allow_in_outfield: bool,
98
99    /// Whether the entity position is clamped to the terrain surface.
100    clamp_to_surface: bool,
101
102    /// Whether the entity position is clamped to the water surface.
103    clamp_to_water_surface: bool,
104
105    /// Whether the entity is visible during spring season.
106    spring: bool,
107
108    /// Whether the entity is visible during summer season.
109    summer: bool,
110
111    /// Whether the entity is visible during autumn season.
112    autumn: bool,
113
114    /// Whether the entity is visible during winter season.
115    winter: bool,
116
117    /// Whether the entity is visible in tactical camera view.
118    visible_in_tactical_view: bool,
119
120    /// Whether the entity is only visible in tactical view (hidden in normal view).
121    visible_in_tactical_view_only: bool,
122}
123
124//---------------------------------------------------------------------------//
125//                           Implementation of Flags
126//---------------------------------------------------------------------------//
127
128impl Decodeable for Flags {
129
130    fn decode<R: ReadBytes>(data: &mut R, extra_data: &Option<DecodeableExtraData>) -> Result<Self> {
131        let mut flags = Self::default();
132        flags.serialise_version = data.read_u16()?;
133
134        match flags.serialise_version {
135            1 => flags.read_v1(data, extra_data)?,
136            2 => flags.read_v2(data, extra_data)?,
137            3 => flags.read_v3(data, extra_data)?,
138            4 => flags.read_v4(data, extra_data)?,
139            _ => return Err(RLibError::DecodingFastBinUnsupportedVersion(String::from("Flags"), flags.serialise_version)),
140        }
141
142        Ok(flags)
143    }
144}
145
146impl Encodeable for Flags {
147
148    fn encode<W: WriteBytes>(&mut self, buffer: &mut W, extra_data: &Option<EncodeableExtraData>) -> Result<()> {
149        buffer.write_u16(self.serialise_version)?;
150
151        match self.serialise_version {
152            1 => self.write_v1(buffer, extra_data)?,
153            2 => self.write_v2(buffer, extra_data)?,
154            3 => self.write_v3(buffer, extra_data)?,
155            4 => self.write_v4(buffer, extra_data)?,
156            _ => return Err(RLibError::EncodingFastBinUnsupportedVersion(String::from("Flags"), self.serialise_version)),
157        }
158
159        Ok(())
160    }
161}