2 This file is part of GtkRadiant.
4 GtkRadiant is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 GtkRadiant is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with GtkRadiant; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #include "ufoai_filters.h"
20 #include "globaldefs.h"
24 #include "iscenegraph.h"
26 // believe me, i'm sorry
27 #include "../../radiant/brush.h"
29 #include "generic/callback.h"
33 bool actorclip_active = false;
34 bool stepon_active = false;
35 bool nodraw_active = false;
36 bool weaponclip_active = false;
39 // TODO: This should be added to ibrush.h
40 // like already done for Node_getEntity in ientity.h
41 // FIXME: Doesn't belong here
42 inline Brush *Node_getBrush(scene::Node &node)
44 return NodeTypeCast<Brush>::cast(node);
47 void hide_node(scene::Node &node, bool hide)
50 ? node.enable(scene::Node::eHidden)
51 : node.disable(scene::Node::eHidden);
54 typedef std::list<Entity *> entitylist_t;
56 class EntityFindByName : public scene::Graph::Walker {
58 entitylist_t &m_entitylist;
59 /* this starts at 1 << level */
63 EntityFindByName(const char *name, entitylist_t &entitylist, int flag, bool hide)
64 : m_name(name), m_entitylist(entitylist), m_flag(flag), m_hide(hide)
68 bool pre(const scene::Path &path, scene::Instance &instance) const
71 Entity *entity = Node_getEntity(path.top());
73 if (string_equal(m_name, entity->getKeyValue("classname"))) {
74 const char *spawnflags = entity->getKeyValue("spawnflags");
75 globalOutputStream() << "spawnflags for " << m_name << ": " << spawnflags << ".\n";
77 if (!string_empty(spawnflags)) {
78 spawnflagsInt = atoi(spawnflags);
79 if (!(spawnflagsInt & m_flag)) {
80 hide_node(path.top(), m_hide); // hide/unhide
81 m_entitylist.push_back(entity);
84 globalOutputStream() << "UFO:AI: Warning: no spawnflags for " << m_name << ".\n";
92 class ForEachFace : public BrushVisitor {
94 mutable int m_contentFlagsVis;
95 mutable int m_surfaceFlagsVis;
99 m_contentFlagsVis = -1;
100 m_surfaceFlagsVis = -1;
103 void visit(Face &face) const
106 if (m_surfaceFlagsVis < 0) {
107 m_surfaceFlagsVis = face.getShader().m_flags.m_surfaceFlags;
108 } else if (m_surfaceFlagsVis >= 0 && m_surfaceFlagsVis != face.getShader().m_flags.m_surfaceFlags) {
109 globalOutputStream() << "Faces with different surfaceflags at brush\n";
111 if (m_contentFlagsVis < 0) {
112 m_contentFlagsVis = face.getShader().m_flags.m_contentFlags;
113 } else if (m_contentFlagsVis >= 0 && m_contentFlagsVis != face.getShader().m_flags.m_contentFlags) {
114 globalOutputStream() << "Faces with different contentflags at brush\n";
117 m_surfaceFlagsVis = face.getShader().m_flags.m_surfaceFlags;
118 m_contentFlagsVis = face.getShader().m_flags.m_contentFlags;
123 typedef std::list<Brush *> brushlist_t;
125 class BrushGetLevel : public scene::Graph::Walker {
126 brushlist_t &m_brushlist;
128 bool m_content; // if true - use m_contentFlags - otherwise m_surfaceFlags
129 mutable bool m_notset;
132 BrushGetLevel(brushlist_t &brushlist, int flag, bool content, bool notset, bool hide)
133 : m_brushlist(brushlist), m_flag(flag), m_content(content), m_notset(notset), m_hide(hide)
137 bool pre(const scene::Path &path, scene::Instance &instance) const
139 Brush *brush = Node_getBrush(path.top());
142 brush->forEachFace(faces);
145 // are any flags set?
146 if (faces.m_contentFlagsVis > 0) {
147 // flag should not be set
148 if (m_notset && (!(faces.m_contentFlagsVis & m_flag))) {
149 hide_node(path.top(), m_hide);
150 m_brushlist.push_back(brush);
152 // check whether flag is set
153 else if (!m_notset && ((faces.m_contentFlagsVis & m_flag))) {
154 hide_node(path.top(), m_hide);
155 m_brushlist.push_back(brush);
161 // are any flags set?
162 if (faces.m_surfaceFlagsVis > 0) {
163 // flag should not be set
164 if (m_notset && (!(faces.m_surfaceFlagsVis & m_flag))) {
165 hide_node(path.top(), m_hide);
166 m_brushlist.push_back(brush);
168 // check whether flag is set
169 else if (!m_notset && ((faces.m_surfaceFlagsVis & m_flag))) {
170 hide_node(path.top(), m_hide);
171 m_brushlist.push_back(brush);
182 * @brief Activates the level filter for the given level
183 * @param[in] level Which level to show?
186 void filter_level(int flag)
190 entitylist_t entities;
195 GlobalSceneGraph().traverse(BrushGetLevel(brushes, (level_active << 8), true, true, false));
196 GlobalSceneGraph().traverse(EntityFindByName("func_door", entities, level_active, false));
197 GlobalSceneGraph().traverse(EntityFindByName("func_breakable", entities, level_active, false));
198 GlobalSceneGraph().traverse(EntityFindByName("misc_model", entities, level_active, false));
199 GlobalSceneGraph().traverse(EntityFindByName("misc_particle", entities, level_active, false));
200 entities.erase(entities.begin(), entities.end());
201 brushes.erase(brushes.begin(), brushes.end());
202 if (level_active == level) {
204 // just disabĺe level filter
208 level_active = level;
209 globalOutputStream() << "UFO:AI: level_active: " << level_active << ", flag: " << flag << ".\n";
212 GlobalSceneGraph().traverse(BrushGetLevel(brushes, flag, true, true, true));
215 GlobalSceneGraph().traverse(EntityFindByName("func_door", entities, level, true));
216 GlobalSceneGraph().traverse(EntityFindByName("func_breakable", entities, level, true));
217 GlobalSceneGraph().traverse(EntityFindByName("misc_model", entities, level, true));
218 GlobalSceneGraph().traverse(EntityFindByName("misc_particle", entities, level, true));
221 if (brushes.empty()) {
222 globalOutputStream() << "UFO:AI: No brushes.\n";
224 globalOutputStream() << "UFO:AI: Found " << Unsigned(brushes.size()) << " brushes.\n";
227 // now let's filter all entities like misc_model, func_breakable and func_door that have the spawnflags set
228 if (entities.empty()) {
229 globalOutputStream() << "UFO:AI: No entities.\n";
231 globalOutputStream() << "UFO:AI: Found " << Unsigned(entities.size()) << " entities.\n";
236 void filter_stepon(void)
239 stepon_active = false;
241 stepon_active = true;
244 GlobalSceneGraph().traverse(BrushGetLevel(brushes, CONTENTS_STEPON, true, false, stepon_active));
246 if (brushes.empty()) {
247 globalOutputStream() << "UFO:AI: No brushes.\n";
249 globalOutputStream() << "UFO:AI: Hiding " << Unsigned(brushes.size()) << " stepon brushes.\n";
253 void filter_nodraw(void)
256 nodraw_active = false;
258 nodraw_active = true;
261 GlobalSceneGraph().traverse(BrushGetLevel(brushes, SURF_NODRAW, false, false, nodraw_active));
264 if (brushes.empty()) {
265 globalOutputStream() << "UFO:AI: No brushes.\n";
267 globalOutputStream() << "UFO:AI: Hiding " << Unsigned(brushes.size()) << " nodraw brushes.\n";
272 void filter_actorclip(void)
274 if (actorclip_active) {
275 actorclip_active = false;
277 actorclip_active = true;
280 GlobalSceneGraph().traverse(BrushGetLevel(brushes, CONTENTS_ACTORCLIP, true, false, actorclip_active));
283 if (brushes.empty()) {
284 globalOutputStream() << "UFO:AI: No brushes.\n";
286 globalOutputStream() << "UFO:AI: Hiding " << Unsigned(brushes.size()) << " actorclip brushes.\n";
291 void filter_weaponclip(void)
293 if (weaponclip_active) {
294 weaponclip_active = false;
296 weaponclip_active = true;
299 GlobalSceneGraph().traverse(BrushGetLevel(brushes, CONTENTS_WEAPONCLIP, true, false, weaponclip_active));
302 if (brushes.empty()) {
303 globalOutputStream() << "UFO:AI: No brushes.\n";
305 globalOutputStream() << "UFO:AI: Hiding " << Unsigned(brushes.size()) << " weaponclip brushes.\n";