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"
23 #include "iscenegraph.h"
25 // believe me, i'm sorry
26 #include "../../radiant/brush.h"
28 #include "generic/callback.h"
32 bool actorclip_active = false;
33 bool stepon_active = false;
36 // TODO: This should be added to ibrush.h
37 // like already done for Node_getEntity in ientity.h
38 // FIXME: Doesn't belong here
39 inline Brush* Node_getBrush(scene::Node& node)
41 return NodeTypeCast<Brush>::cast(node);
44 void hide_node(scene::Node& node, bool hide)
47 ? node.enable(scene::Node::eHidden)
48 : node.disable(scene::Node::eHidden);
51 typedef std::list<Entity*> entitylist_t;
53 class EntityFindByName : public scene::Graph::Walker
56 entitylist_t& m_entitylist;
57 /* this starts at 1 << level */
61 EntityFindByName(const char* name, entitylist_t& entitylist, int flag, bool hide)
62 : m_name(name), m_entitylist(entitylist), m_flag(flag), m_hide(hide)
65 bool pre(const scene::Path& path, scene::Instance& instance) const
68 Entity* entity = Node_getEntity(path.top());
71 if (string_equal(m_name, entity->getKeyValue("classname")))
73 const char *spawnflags = entity->getKeyValue("spawnflags");
74 globalOutputStream() << "spawnflags for " << m_name << ": " << spawnflags << ".\n";
76 if (!string_empty(spawnflags)) {
77 spawnflagsInt = atoi(spawnflags);
78 if (!(spawnflagsInt & m_flag))
80 hide_node(path.top(), m_hide); // hide/unhide
81 m_entitylist.push_back(entity);
86 globalOutputStream() << "UFO:AI: Warning: no spawnflags for " << m_name << ".\n";
94 class ForEachFace : public BrushVisitor
98 mutable int m_contentFlagsVis;
99 mutable int m_surfaceFlagsVis;
101 ForEachFace(Brush& brush)
104 m_contentFlagsVis = -1;
105 m_surfaceFlagsVis = -1;
108 void visit(Face& face) const
111 if (m_surfaceFlagsVis < 0)
112 m_surfaceFlagsVis = face.getShader().m_flags.m_surfaceFlags;
113 else if (m_surfaceFlagsVis >= 0 && m_surfaceFlagsVis != face.getShader().m_flags.m_surfaceFlags)
114 globalOutputStream() << "Faces with different surfaceflags at brush\n";
115 if (m_contentFlagsVis < 0)
116 m_contentFlagsVis = face.getShader().m_flags.m_contentFlags;
117 else if (m_contentFlagsVis >= 0 && m_contentFlagsVis != face.getShader().m_flags.m_contentFlags)
118 globalOutputStream() << "Faces with different contentflags at brush\n";
120 m_surfaceFlagsVis = face.getShader().m_flags.m_surfaceFlags;
121 m_contentFlagsVis = face.getShader().m_flags.m_contentFlags;
126 typedef std::list<Brush*> brushlist_t;
128 class BrushGetLevel : public scene::Graph::Walker
130 brushlist_t& m_brushlist;
132 bool m_content; // if true - use m_contentFlags - otherwise m_surfaceFlags
133 mutable bool m_notset;
136 BrushGetLevel(brushlist_t& brushlist, int flag, bool content, bool notset, bool hide)
137 : m_brushlist(brushlist), m_flag(flag), m_content(content), m_notset(notset), m_hide(hide)
140 bool pre(const scene::Path& path, scene::Instance& instance) const
142 Brush* brush = Node_getBrush(path.top());
145 ForEachFace faces(*brush);
146 brush->forEachFace(faces);
150 // are any flags set?
151 if (faces.m_contentFlagsVis > 0)
153 // flag should not be set
154 if (m_notset && (!(faces.m_contentFlagsVis & m_flag)))
156 hide_node(path.top(), m_hide);
157 m_brushlist.push_back(brush);
159 // check whether flag is set
160 else if (!m_notset && ((faces.m_contentFlagsVis & m_flag)))
162 hide_node(path.top(), m_hide);
163 m_brushlist.push_back(brush);
170 // are any flags set?
171 if (faces.m_surfaceFlagsVis > 0)
173 // flag should not be set
174 if (m_notset && (!(faces.m_surfaceFlagsVis & m_flag)))
176 hide_node(path.top(), m_hide);
177 m_brushlist.push_back(brush);
179 // check whether flag is set
180 else if (!m_notset && ((faces.m_surfaceFlagsVis & m_flag)))
182 hide_node(path.top(), m_hide);
183 m_brushlist.push_back(brush);
194 * @brief Activates the level filter for the given level
195 * @param[in] level Which level to show?
198 void filter_level(int flag)
202 entitylist_t entities;
208 GlobalSceneGraph().traverse(BrushGetLevel(brushes, (level_active << 8), true, true, false));
209 GlobalSceneGraph().traverse(EntityFindByName("func_breakable", entities, level_active, false));
210 GlobalSceneGraph().traverse(EntityFindByName("misc_model", entities, level_active, false));
211 entities.erase(entities.begin(), entities.end());
212 brushes.erase(brushes.begin(), brushes.end());
213 if (level_active == level)
216 // just disabĺe level filter
220 level_active = level;
221 globalOutputStream() << "UFO:AI: level_active: " << level_active << ", flag: " << flag << ".\n";
224 GlobalSceneGraph().traverse(BrushGetLevel(brushes, flag, true, true, true));
227 GlobalSceneGraph().traverse(EntityFindByName("func_breakable", entities, level, true));
228 GlobalSceneGraph().traverse(EntityFindByName("misc_model", entities, level, true));
233 globalOutputStream() << "UFO:AI: No brushes.\n";
237 globalOutputStream() << "UFO:AI: Found " << Unsigned(brushes.size()) << " brushes.\n";
240 // now let's filter all entities like misc_model and func_breakable that have the spawnflags set
241 if (entities.empty())
243 globalOutputStream() << "UFO:AI: No entities.\n";
247 globalOutputStream() << "UFO:AI: Found " << Unsigned(entities.size()) << " entities.\n";
252 void filter_stepon (void)
255 stepon_active = false;
257 stepon_active = true;
260 GlobalSceneGraph().traverse(BrushGetLevel(brushes, CONTENTS_STEPON, true, false, stepon_active));
264 globalOutputStream() << "UFO:AI: No brushes.\n";
268 globalOutputStream() << "UFO:AI: Hiding " << Unsigned(brushes.size()) << " stepon 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));
285 globalOutputStream() << "UFO:AI: No brushes.\n";
289 globalOutputStream() << "UFO:AI: Hiding " << Unsigned(brushes.size()) << " actorclip brushes.\n";