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