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_level.h"
20 #include "ufoai_filters.h"
24 #include "iscenegraph.h"
26 #include "string/string.h"
32 * @brief find entities by class
33 * @note from radiant/map.cpp
35 class EntityFindByClassname : public scene::Graph::Walker
40 EntityFindByClassname(const char* name, Entity*& entity) : m_name(name), m_entity(entity)
44 bool pre(const scene::Path& path, scene::Instance& instance) const
48 Entity* entity = Node_getEntity(path.top());
49 if(entity != 0 && string_equal(m_name, entity->getKeyValue("classname")))
61 Entity* Scene_FindEntityByClass(const char* name)
63 Entity* entity = NULL;
64 GlobalSceneGraph().traverse(EntityFindByClassname(name, entity));
69 * @brief finds start positions
71 class EntityFindFlags : public scene::Graph::Walker
73 const char *m_classname;
78 EntityFindFlags(const char *classname, const char *flag, int *count) : m_classname(classname), m_flag(flag), m_count(count)
81 bool pre(const scene::Path& path, scene::Instance& instance) const
84 Entity* entity = Node_getEntity(path.top());
85 if(entity != 0 && string_equal(m_classname, entity->getKeyValue("classname")))
87 str = entity->getKeyValue(m_flag);
88 if (string_empty(str))
99 * @brief finds start positions
101 class EntityFindTeams : public scene::Graph::Walker
103 const char *m_classname;
108 EntityFindTeams(const char *classname, int *count, int *team) : m_classname(classname), m_count(count), m_team(team)
111 bool pre(const scene::Path& path, scene::Instance& instance) const
114 Entity* entity = Node_getEntity(path.top());
115 if(entity != 0 && string_equal(m_classname, entity->getKeyValue("classname")))
119 // now get the highest teamnum
122 str = entity->getKeyValue("team");
123 if (!string_empty(str))
125 if (atoi(str) > *m_team)
126 (*m_team) = atoi(str);
137 void get_team_count (const char *classname, int *count, int *team)
139 GlobalSceneGraph().traverse(EntityFindTeams(classname, count, team));
140 globalOutputStream() << "UFO:AI: classname: " << classname << ": #" << (*count) << "\n";
144 * @brief Some default values to worldspawn like maxlevel and so on
146 void assign_default_values_to_worldspawn (bool override, char **returnMsg)
148 static char message[1024];
154 worldspawn = Scene_FindEntityByClass("worldspawn");
157 globalOutputStream() << "UFO:AI: Could not find worldspawn.\n";
158 *returnMsg = "Could not find worldspawn";
165 if (override || string_empty(worldspawn->getKeyValue("maxlevel")))
167 // TODO: Get highest brush - a level has 64 units
168 worldspawn->setKeyValue("maxlevel", "5");
169 snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message), "Set maxlevel to: %s", worldspawn->getKeyValue("maxlevel"));
172 if (override || string_empty(worldspawn->getKeyValue("maxteams")))
174 get_team_count("info_player_start", &count, &teams);
177 snprintf(str, sizeof(str) - 1, "%i", teams);
178 worldspawn->setKeyValue("maxteams", str);
179 snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message), "Set maxteams to: %s", worldspawn->getKeyValue("maxteams"));
183 snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message), "You should at least place 16 info_player_start");
187 // no errors - no warnings
188 if (!strlen(message))
191 *returnMsg = message;
197 int check_entity_flags (const char *classname, const char *flag)
201 /* init this with 0 every time we browse the tree */
204 GlobalSceneGraph().traverse(EntityFindFlags(classname, flag, &count));
209 * @brief Will check e.g. the map entities for valid values
210 * @todo: check for maxlevel
212 void check_map_values (char **returnMsg)
214 static char message[1024];
221 worldspawn = Scene_FindEntityByClass("worldspawn");
224 globalOutputStream() << "UFO:AI: Could not find worldspawn.\n";
225 *returnMsg = "Could not find worldspawn";
232 // multiplayer start positions
233 get_team_count("info_player_start", &count, &teams);
235 strncat(message, "No multiplayer start positions (info_player_start)\n", sizeof(message) - 1);
239 get_team_count("info_human_start", &count, NULL);
241 strncat(message, "No singleplayer start positions (info_human_start)\n", sizeof(message) - 1);
245 get_team_count("info_2x2_start", &count, NULL);
247 strncat(message, "No singleplayer start positions for 2x2 units (info_2x2_start)\n", sizeof(message) - 1);
249 // search for civilians
251 get_team_count("info_civilian_start", &count, NULL);
253 strncat(message, "No civilian start positions (info_civilian_start)\n", sizeof(message) - 1);
256 if (string_empty(worldspawn->getKeyValue("maxlevel")))
257 strncat(message, "Worldspawn: No maxlevel defined\n", sizeof(message) - 1);
258 else if (atoi(worldspawn->getKeyValue("maxlevel")) > 8)
260 strncat(message, "Worldspawn: Highest maxlevel is 8\n", sizeof(message) - 1);
261 worldspawn->setKeyValue("maxlevel", "8");
264 ent_flags = check_entity_flags("func_door", "spawnflags");
266 snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message), "Found %i func_door with no spawnflags\n", ent_flags);
267 ent_flags = check_entity_flags("func_breakable", "spawnflags");
269 snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message), "Found %i func_breakable with no spawnflags\n", ent_flags);
270 ent_flags = check_entity_flags("misc_sound", "spawnflags");
272 snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message), "Found %i misc_sound with no spawnflags\n", ent_flags);
273 ent_flags = check_entity_flags("misc_model", "spawnflags");
275 snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message), "Found %i misc_model with no spawnflags\n", ent_flags);
276 ent_flags = check_entity_flags("misc_particle", "spawnflags");
278 snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message), "Found %i misc_particle with no spawnflags\n", ent_flags);
279 ent_flags = check_entity_flags("info_player_start", "team");
281 snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message), "Found %i info_player_start with no team assigned\n!!Teamcount may change after you've fixed this\n", ent_flags);
282 ent_flags = check_entity_flags("light", "color");
283 ent_flags = check_entity_flags("light", "_color");
285 snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message), "Found %i lights with no color value\n", ent_flags);
288 if (!strlen(message)) {
289 snprintf(message, sizeof(message) - 1, "No errors found - you are ready to compile the map now\n");
292 *returnMsg = message;