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 {
39 EntityFindByClassname(const char *name, Entity *&entity) : m_name(name), m_entity(entity)
44 bool pre(const scene::Path &path, scene::Instance &instance) const
47 Entity *entity = Node_getEntity(path.top());
48 if (entity != 0 && string_equal(m_name, entity->getKeyValue("classname"))) {
59 Entity *Scene_FindEntityByClass(const char *name)
61 Entity *entity = NULL;
62 GlobalSceneGraph().traverse(EntityFindByClassname(name, entity));
67 * @brief finds start positions
69 class EntityFindFlags : public scene::Graph::Walker {
70 const char *m_classname;
75 EntityFindFlags(const char *classname, const char *flag, int *count) : m_classname(classname), m_flag(flag),
80 bool pre(const scene::Path &path, scene::Instance &instance) const
83 Entity *entity = Node_getEntity(path.top());
84 if (entity != 0 && string_equal(m_classname, entity->getKeyValue("classname"))) {
85 str = entity->getKeyValue(m_flag);
86 if (string_empty(str)) {
96 * @brief finds start positions
98 class EntityFindTeams : public scene::Graph::Walker {
99 const char *m_classname;
104 EntityFindTeams(const char *classname, int *count, int *team) : m_classname(classname), m_count(count), m_team(team)
108 bool pre(const scene::Path &path, scene::Instance &instance) const
111 Entity *entity = Node_getEntity(path.top());
112 if (entity != 0 && string_equal(m_classname, entity->getKeyValue("classname"))) {
116 // now get the highest teamnum
118 str = entity->getKeyValue("team");
119 if (!string_empty(str)) {
120 if (atoi(str) > *m_team) {
121 (*m_team) = atoi(str);
133 void get_team_count(const char *classname, int *count, int *team)
135 GlobalSceneGraph().traverse(EntityFindTeams(classname, count, team));
136 globalOutputStream() << "UFO:AI: classname: " << classname << ": #" << (*count) << "\n";
140 * @brief Some default values to worldspawn like maxlevel and so on
142 void assign_default_values_to_worldspawn(bool override, const char **returnMsg)
144 static char message[1024];
150 worldspawn = Scene_FindEntityByClass("worldspawn");
152 globalOutputStream() << "UFO:AI: Could not find worldspawn.\n";
153 *returnMsg = "Could not find worldspawn";
160 if (override || string_empty(worldspawn->getKeyValue("maxlevel"))) {
161 // TODO: Get highest brush - a level has 64 units
162 worldspawn->setKeyValue("maxlevel", "5");
163 snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message), "Set maxlevel to: %s",
164 worldspawn->getKeyValue("maxlevel"));
167 if (override || string_empty(worldspawn->getKeyValue("maxteams"))) {
168 get_team_count("info_player_start", &count, &teams);
170 snprintf(str, sizeof(str) - 1, "%i", teams);
171 worldspawn->setKeyValue("maxteams", str);
172 snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message), "Set maxteams to: %s",
173 worldspawn->getKeyValue("maxteams"));
176 snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message),
177 "You should at least place 16 info_player_start");
181 // no errors - no warnings
182 if (!strlen(message)) {
186 *returnMsg = message;
192 int check_entity_flags(const char *classname, const char *flag)
196 /* init this with 0 every time we browse the tree */
199 GlobalSceneGraph().traverse(EntityFindFlags(classname, flag, &count));
204 * @brief Will check e.g. the map entities for valid values
205 * @todo: check for maxlevel
207 void check_map_values(const char **returnMsg)
209 static char message[1024];
216 worldspawn = Scene_FindEntityByClass("worldspawn");
218 globalOutputStream() << "UFO:AI: Could not find worldspawn.\n";
219 *returnMsg = "Could not find worldspawn";
226 // multiplayer start positions
227 get_team_count("info_player_start", &count, &teams);
229 strncat(message, "No multiplayer start positions (info_player_start)\n", sizeof(message) - 1);
234 get_team_count("info_human_start", &count, NULL);
236 strncat(message, "No singleplayer start positions (info_human_start)\n", sizeof(message) - 1);
241 get_team_count("info_2x2_start", &count, NULL);
243 strncat(message, "No singleplayer start positions for 2x2 units (info_2x2_start)\n", sizeof(message) - 1);
246 // search for civilians
248 get_team_count("info_civilian_start", &count, NULL);
250 strncat(message, "No civilian start positions (info_civilian_start)\n", sizeof(message) - 1);
254 if (string_empty(worldspawn->getKeyValue("maxlevel"))) {
255 strncat(message, "Worldspawn: No maxlevel defined\n", sizeof(message) - 1);
256 } else if (atoi(worldspawn->getKeyValue("maxlevel")) > 8) {
257 strncat(message, "Worldspawn: Highest maxlevel is 8\n", sizeof(message) - 1);
258 worldspawn->setKeyValue("maxlevel", "8");
261 ent_flags = check_entity_flags("func_door", "spawnflags");
263 snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message),
264 "Found %i func_door with no spawnflags\n", ent_flags);
266 ent_flags = check_entity_flags("func_breakable", "spawnflags");
268 snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message),
269 "Found %i func_breakable with no spawnflags\n", ent_flags);
271 ent_flags = check_entity_flags("misc_sound", "spawnflags");
273 snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message),
274 "Found %i misc_sound with no spawnflags\n", ent_flags);
276 ent_flags = check_entity_flags("misc_model", "spawnflags");
278 snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message),
279 "Found %i misc_model with no spawnflags\n", ent_flags);
281 ent_flags = check_entity_flags("misc_particle", "spawnflags");
283 snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message),
284 "Found %i misc_particle with no spawnflags\n", ent_flags);
286 ent_flags = check_entity_flags("info_player_start", "team");
288 snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message),
289 "Found %i info_player_start with no team assigned\n!!Teamcount may change after you've fixed this\n",
292 ent_flags = check_entity_flags("light", "color");
293 ent_flags = check_entity_flags("light", "_color");
295 snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message),
296 "Found %i lights with no color value\n", ent_flags);
300 if (!strlen(message)) {
301 snprintf(message, sizeof(message) - 1, "No errors found - you are ready to compile the map now\n");
304 *returnMsg = message;