1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use std::collections::HashSet;
use super::VmfFile;
use crate::prelude::{Entity, Solid, VisGroup};
impl VmfFile {
/// Returns an iterator over entities (including hidden ones) belonging to the specified VisGroup ID.
///
/// # Arguments
///
/// * `group_id` - The ID of the target VisGroup.
/// * `include_children` - If true, includes entities from all child VisGroups recursively.
///
/// # Returns
///
/// An `Option` containing an iterator yielding references to the matching `Entity` objects.
/// Returns `None` if no VisGroup with the given `group_id` is found.
pub fn get_entities_in_visgroup(
&self,
group_id: i32,
include_children: bool,
) -> Option<impl Iterator<Item = &Entity> + '_> {
// 1. Find the starting VisGroup by ID. Returns None if not found.
let start_group = self.visgroups.find_by_id(group_id)?;
// 2. Collect all relevant VisGroup IDs.
let ids_to_check: HashSet<i32> = if include_children {
let mut ids = HashSet::new();
collect_child_visgroup_ids(start_group, &mut ids);
ids
} else {
// Only the ID of the found group.
HashSet::from([start_group.id])
};
// 3. Create the filtered iterator over entities and hidden entities.
let iterator = self
.entities
.iter()
.chain(self.hiddens.iter())
.filter(move |entity| {
// Check if the entity belongs to one of the target VisGroup IDs.
entity
.editor
.visgroup_id.is_some_and(|ent_group_id| ids_to_check.contains(&ent_group_id))
});
// 4. Return the iterator wrapped in Some.
Some(iterator)
}
/// Returns a mutable iterator over entities (including hidden ones) belonging to the specified VisGroup ID.
///
/// # Arguments
///
/// * `group_id` - The ID of the target VisGroup.
/// * `include_children` - If true, includes entities from all child VisGroups recursively.
///
/// # Returns
///
/// An `Option` containing an iterator yielding mutable references to the matching `Entity` objects.
/// Returns `None` if no VisGroup with the given `group_id` is found.
pub fn get_entities_in_visgroup_mut(
&mut self,
group_id: i32,
include_children: bool,
) -> Option<impl Iterator<Item = &mut Entity> + '_> {
// Note: returns mutable references
// 1. Find the starting VisGroup by ID (immutable lookup is sufficient to get IDs).
let start_group = self.visgroups.find_by_id(group_id)?;
// 2. Collect all relevant VisGroup IDs (immutable collection is fine).
let ids_to_check: HashSet<i32> = if include_children {
let mut ids = HashSet::new();
collect_child_visgroup_ids(start_group, &mut ids);
ids
} else {
HashSet::from([start_group.id])
};
// 3. Create the filtered *mutable* iterator.
let iterator = self
.entities
.iter_mut() // Get mutable iterator
.chain(self.hiddens.iter_mut()) // Chain mutable iterator
.filter(move |entity| {
entity
.editor
.visgroup_id.is_some_and(|ent_group_id| ids_to_check.contains(&ent_group_id))
});
// 4. Return the mutable iterator wrapped in Some.
Some(iterator)
}
/// Returns an iterator over world solids (visible and hidden) belonging to the specified VisGroup ID.
///
/// # Arguments
///
/// * `group_id` - The ID of the target VisGroup.
/// * `include_children` - If true, includes solids from all child VisGroups recursively.
///
/// # Returns
///
/// An `Option` containing an iterator yielding references to the matching `Solid` objects.
/// Returns `None` if no VisGroup with the given `group_id` is found.
pub fn get_solids_in_visgroup(
&self,
group_id: i32,
include_children: bool,
) -> Option<impl Iterator<Item = &Solid> + '_> {
// 1. Find the starting VisGroup.
let start_group = self.visgroups.find_by_id(group_id)?;
// 2. Collect relevant IDs.
let ids_to_check: HashSet<i32> = if include_children {
let mut ids = HashSet::new();
collect_child_visgroup_ids(start_group, &mut ids);
ids
} else {
HashSet::from([start_group.id])
};
// 3. Create filtered iterator over world solids.
let iterator = self
.world
.solids
.iter()
.chain(self.world.hidden.iter())
.filter(move |solid| {
solid.editor.visgroup_id.is_some_and(|solid_group_id| {
ids_to_check.contains(&solid_group_id)
})
});
// 4. Return iterator.
Some(iterator)
}
/// Returns a mutable iterator over world solids (visible and hidden) belonging to the specified VisGroup ID.
///
/// # Arguments
///
/// * `group_id` - The ID of the target VisGroup.
/// * `include_children` - If true, includes solids from all child VisGroups recursively.
///
/// # Returns
///
/// An `Option` containing an iterator yielding mutable references to the matching `Solid` objects.
/// Returns `None` if no VisGroup with the given `group_id` is found.
pub fn get_solids_in_visgroup_mut(
&mut self,
group_id: i32,
include_children: bool,
) -> Option<impl Iterator<Item = &mut Solid> + '_> {
// 1. Find the starting VisGroup.
let start_group = self.visgroups.find_by_id(group_id)?;
// 2. Collect relevant IDs.
let ids_to_check: HashSet<i32> = if include_children {
let mut ids = HashSet::new();
collect_child_visgroup_ids(start_group, &mut ids);
ids
} else {
HashSet::from([start_group.id])
};
// 3. Create filtered *mutable* iterator over world solids.
let iterator = self
.world
.solids
.iter_mut() // Mutable iterator
.chain(self.world.hidden.iter_mut()) // Chain mutable iterator
.filter(move |solid| {
solid.editor.visgroup_id.is_some_and(|solid_group_id| {
ids_to_check.contains(&solid_group_id)
})
});
// 4. Return mutable iterator.
Some(iterator)
}
}
/// Recursively collects the IDs of a VisGroup and all its children into a HashSet.
/// The passed `group` must be the one found by ID/Name previously.
/// Uses the `collected_ids` set to avoid infinite loops in case of (unlikely) cycles.
fn collect_child_visgroup_ids(group: &VisGroup, collected_ids: &mut HashSet<i32>) {
// Insert the current group's ID. If it was already present, stop to prevent cycles.
if !collected_ids.insert(group.id) {
return;
}
// Recursively collect IDs from children
if let Some(ref children) = group.children {
for child in children {
collect_child_visgroup_ids(child, collected_ids);
}
}
}