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, maxteams and so on
146 void assign_default_values_to_worldspawn (bool override, bool day, 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 get_team_count("info_player_start", &count, &teams);
167 // TODO: Get highest brush - a level has 64 units
168 worldspawn->setKeyValue("maxlevel", "5");
170 if (string_empty(worldspawn->getKeyValue("maxteams"))
171 || atoi(worldspawn->getKeyValue("maxteams")) != teams)
173 snprintf(str, sizeof(str) - 1, "%i", teams);
174 worldspawn->setKeyValue("maxteams", str);
175 strncat(message, "Worldspawn: Set maxteams to ", sizeof(message) - 1);
176 strncat(message, str, sizeof(message) - 1);
177 strncat(message, "\n", sizeof(message) - 1);
184 worldspawn->setKeyValue("light", "160");
185 worldspawn->setKeyValue("_color", "1 0.8 0.8");
186 worldspawn->setKeyValue("angles", "30 210");
187 worldspawn->setKeyValue("ambient", "0.4 0.4 0.4");
191 if (string_empty(worldspawn->getKeyValue("light")))
193 worldspawn->setKeyValue("light", "160");
194 snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message), "Set ambient to: %s", worldspawn->getKeyValue("ambient"));
196 if (string_empty(worldspawn->getKeyValue("_color")))
198 worldspawn->setKeyValue("_color", "1 0.8 0.8");
199 snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message), "Set ambient to: %s", worldspawn->getKeyValue("ambient"));
201 if (string_empty(worldspawn->getKeyValue("angles")))
203 worldspawn->setKeyValue("angles", "30 210");
204 snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message), "Set ambient to: %s", worldspawn->getKeyValue("ambient"));
206 if (string_empty(worldspawn->getKeyValue("ambient")))
208 worldspawn->setKeyValue("ambient", "0.4 0.4 0.4");
209 snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message), "Set ambient to: %s", worldspawn->getKeyValue("ambient"));
217 worldspawn->setKeyValue("light", "60");
218 worldspawn->setKeyValue("_color", "0.8 0.8 1");
219 worldspawn->setKeyValue("angles", "15 60");
220 worldspawn->setKeyValue("ambient", "0.25 0.25 0.275");
224 if (string_empty(worldspawn->getKeyValue("light")))
226 worldspawn->setKeyValue("light", "60");
227 snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message), "Set ambient to: %s", worldspawn->getKeyValue("ambient"));
229 if (string_empty(worldspawn->getKeyValue("_color")))
231 worldspawn->setKeyValue("_color", "0.8 0.8 1");
232 snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message), "Set ambient to: %s", worldspawn->getKeyValue("ambient"));
234 if (string_empty(worldspawn->getKeyValue("angles")))
236 worldspawn->setKeyValue("angles", "15 60");
237 snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message), "Set ambient to: %s", worldspawn->getKeyValue("ambient"));
239 if (string_empty(worldspawn->getKeyValue("ambient")))
241 worldspawn->setKeyValue("ambient", "0.25 0.25 0.275");
242 snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message), "Set ambient to: %s", worldspawn->getKeyValue("ambient"));
249 snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message),
251 "Set _color to: %s\n"
252 "Set angles to: %s\n"
253 "Set ambient to: %s\n",
254 worldspawn->getKeyValue("light"),
255 worldspawn->getKeyValue("_color"),
256 worldspawn->getKeyValue("angles"),
257 worldspawn->getKeyValue("ambient")
261 // no errors - no warnings
262 if (!strlen(message))
265 *returnMsg = message;
271 int check_entity_flags (const char *classname, const char *flag)
275 /* init this with 0 every time we browse the tree */
278 GlobalSceneGraph().traverse(EntityFindFlags(classname, flag, &count));
283 * @brief Will check e.g. the map entities for valid values
284 * @todo: check for maxlevel
286 void check_map_values (char **returnMsg)
288 static char message[1024];
295 worldspawn = Scene_FindEntityByClass("worldspawn");
298 globalOutputStream() << "UFO:AI: Could not find worldspawn.\n";
299 *returnMsg = "Could not find worldspawn";
306 // multiplayer start positions
307 get_team_count("info_player_start", &count, &teams);
309 strncat(message, "No multiplayer start positions (info_player_start)\n", sizeof(message) - 1);
310 else if (string_empty(worldspawn->getKeyValue("maxteams")))
312 snprintf(message, sizeof(message) - 1, "Worldspawn: No maxteams defined (#info_player_start) (set to: %i)\n", teams);
313 snprintf(str, sizeof(str) - 1, "%i", teams);
314 worldspawn->setKeyValue("maxteams", str);
316 else if (teams != atoi(worldspawn->getKeyValue("maxteams")))
317 snprintf(message, sizeof(message) - 1, "Worldspawn: Settings for maxteams (%s) doesn't match team count (%i)\n", worldspawn->getKeyValue("maxteams"), teams);
321 get_team_count("info_human_start", &count, NULL);
323 strncat(message, "No singleplayer start positions (info_human_start)\n", sizeof(message) - 1);
325 // search for civilians
327 get_team_count("info_civilian_start", &count, NULL);
329 strncat(message, "No civilian start positions (info_civilian_start)\n", sizeof(message) - 1);
332 if (string_empty(worldspawn->getKeyValue("maxlevel")))
333 strncat(message, "Worldspawn: No maxlevel defined\n", sizeof(message) - 1);
334 else if (atoi(worldspawn->getKeyValue("maxlevel")) > 8)
336 strncat(message, "Worldspawn: Highest maxlevel is 8\n", sizeof(message) - 1);
337 worldspawn->setKeyValue("maxlevel", "8");
340 ent_flags = check_entity_flags("func_door", "spawnflags");
342 snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message), "Found %i func_door with no spawnflags\n", ent_flags);
343 ent_flags = check_entity_flags("func_breakable", "spawnflags");
345 snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message), "Found %i func_breakable with no spawnflags\n", ent_flags);
346 ent_flags = check_entity_flags("misc_model", "spawnflags");
348 snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message), "Found %i misc_model with no spawnflags\n", ent_flags);
349 ent_flags = check_entity_flags("misc_particle", "spawnflags");
351 snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message), "Found %i misc_particle with no spawnflags\n", ent_flags);
352 ent_flags = check_entity_flags("info_player_start", "team");
354 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);
355 ent_flags = check_entity_flags("light", "color");
356 ent_flags = check_entity_flags("light", "_color");
358 snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message), "Found %i lights with no color value\n", ent_flags);
361 if (!strlen(message)) {
362 snprintf(message, sizeof(message) - 1, "No errors found - you are ready to compile the map now\n");
365 *returnMsg = message;