]> icculus.org git repositories - divverent/netradiant.git/blob - contrib/ufoaiplug/ufoai_level.cpp
fix hang in tjunction.c
[divverent/netradiant.git] / contrib / ufoaiplug / ufoai_level.cpp
1 /*
2 This file is part of GtkRadiant.
3
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.
8
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.
13
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
17 */
18
19 #include "ufoai_level.h"
20 #include "ufoai_filters.h"
21
22 #include "ibrush.h"
23 #include "ientity.h"
24 #include "iscenegraph.h"
25
26 #include "string/string.h"
27 #include <list>
28
29 class Level;
30
31 /**
32  * @brief find entities by class
33  * @note from radiant/map.cpp
34  */
35 class EntityFindByClassname : public scene::Graph::Walker
36 {
37         const char* m_name;
38         Entity*& m_entity;
39 public:
40         EntityFindByClassname(const char* name, Entity*& entity) : m_name(name), m_entity(entity)
41         {
42                 m_entity = 0;
43         }
44         bool pre(const scene::Path& path, scene::Instance& instance) const
45         {
46                 if(m_entity == 0)
47                 {
48                         Entity* entity = Node_getEntity(path.top());
49                         if(entity != 0 && string_equal(m_name, entity->getKeyValue("classname")))
50                         {
51                                 m_entity = entity;
52                         }
53                 }
54                 return true;
55         }
56 };
57
58 /**
59  * @brief
60  */
61 Entity* Scene_FindEntityByClass(const char* name)
62 {
63         Entity* entity = NULL;
64         GlobalSceneGraph().traverse(EntityFindByClassname(name, entity));
65         return entity;
66 }
67
68 /**
69  * @brief finds start positions
70  */
71 class EntityFindFlags : public scene::Graph::Walker
72 {
73         const char *m_classname;
74         const char *m_flag;
75         int *m_count;
76
77         public:
78                 EntityFindFlags(const char *classname, const char *flag, int *count) : m_classname(classname), m_flag(flag), m_count(count)
79                 {
80                 }
81                 bool pre(const scene::Path& path, scene::Instance& instance) const
82                 {
83                         const char *str;
84                         Entity* entity = Node_getEntity(path.top());
85                         if(entity != 0 && string_equal(m_classname, entity->getKeyValue("classname")))
86                         {
87                                 str = entity->getKeyValue(m_flag);
88                                 if (string_empty(str))
89                                 {
90                                         (*m_count)++;
91                                 }
92                         }
93                         return true;
94                 }
95 };
96
97
98 /**
99  * @brief finds start positions
100  */
101 class EntityFindTeams : public scene::Graph::Walker
102 {
103         const char *m_classname;
104         int *m_count;
105         int *m_team;
106
107 public:
108         EntityFindTeams(const char *classname, int *count, int *team) : m_classname(classname), m_count(count), m_team(team)
109         {
110         }
111         bool pre(const scene::Path& path, scene::Instance& instance) const
112         {
113                 const char *str;
114                 Entity* entity = Node_getEntity(path.top());
115                 if(entity != 0 && string_equal(m_classname, entity->getKeyValue("classname")))
116                 {
117                         if (m_count)
118                                 (*m_count)++;
119                         // now get the highest teamnum
120                         if (m_team)
121                         {
122                                 str = entity->getKeyValue("team");
123                                 if (!string_empty(str))
124                                 {
125                                         if (atoi(str) > *m_team)
126                                                 (*m_team) = atoi(str);
127                                 }
128                         }
129                 }
130                 return true;
131         }
132 };
133
134 /**
135  * @brief
136  */
137 void get_team_count (const char *classname, int *count, int *team)
138 {
139         GlobalSceneGraph().traverse(EntityFindTeams(classname, count, team));
140         globalOutputStream() << "UFO:AI: classname: " << classname << ": #" << (*count) << "\n";
141 }
142
143 /**
144  * @brief Some default values to worldspawn like maxlevel and so on
145  */
146 void assign_default_values_to_worldspawn (bool override, char **returnMsg)
147 {
148         static char message[1024];
149         Entity* worldspawn;
150         int teams = 0;
151         int count = 0;
152         char str[64];
153
154         worldspawn = Scene_FindEntityByClass("worldspawn");
155         if (!worldspawn)
156         {
157                 globalOutputStream() << "UFO:AI: Could not find worldspawn.\n";
158                 *returnMsg = "Could not find worldspawn";
159                 return;
160         }
161
162         *message = '\0';
163         *str = '\0';
164
165         if (override || string_empty(worldspawn->getKeyValue("maxlevel")))
166         {
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"));
170         }
171
172         if (override || string_empty(worldspawn->getKeyValue("maxteams")))
173         {
174                 get_team_count("info_player_start", &count, &teams);
175                 if (teams)
176                 {
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"));
180                 }
181                 if (count < 16)
182                 {
183                         snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message), "You should at least place 16 info_player_start");
184                 }
185         }
186
187         // no errors - no warnings
188         if (!strlen(message))
189                 return;
190
191         *returnMsg = message;
192 }
193
194 /**
195  * @brief
196  */
197 int check_entity_flags (const char *classname, const char *flag)
198 {
199         int count;
200
201         /* init this with 0 every time we browse the tree */
202         count = 0;
203
204         GlobalSceneGraph().traverse(EntityFindFlags(classname, flag, &count));
205         return count;
206 }
207
208 /**
209  * @brief Will check e.g. the map entities for valid values
210  * @todo: check for maxlevel
211  */
212 void check_map_values (char **returnMsg)
213 {
214         static char message[1024];
215         int count = 0;
216         int teams = 0;
217         int ent_flags;
218         Entity* worldspawn;
219         char str[64];
220
221         worldspawn = Scene_FindEntityByClass("worldspawn");
222         if (!worldspawn)
223         {
224                 globalOutputStream() << "UFO:AI: Could not find worldspawn.\n";
225                 *returnMsg = "Could not find worldspawn";
226                 return;
227         }
228
229         *message = '\0';
230         *str = '\0';
231
232         // multiplayer start positions
233         get_team_count("info_player_start", &count, &teams);
234         if (!count)
235                 strncat(message, "No multiplayer start positions (info_player_start)\n", sizeof(message) - 1);
236
237         // singleplayer map?
238         count = 0;
239         get_team_count("info_human_start", &count, NULL);
240         if (!count)
241                 strncat(message, "No singleplayer start positions (info_human_start)\n", sizeof(message) - 1);
242
243         // singleplayer map?
244         count = 0;
245         get_team_count("info_2x2_start", &count, NULL);
246         if (!count)
247                 strncat(message, "No singleplayer start positions for 2x2 units (info_2x2_start)\n", sizeof(message) - 1);
248
249         // search for civilians
250         count = 0;
251         get_team_count("info_civilian_start", &count, NULL);
252         if (!count)
253                 strncat(message, "No civilian start positions (info_civilian_start)\n", sizeof(message) - 1);
254
255         // check maxlevel
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)
259         {
260                 strncat(message, "Worldspawn: Highest maxlevel is 8\n", sizeof(message) - 1);
261                 worldspawn->setKeyValue("maxlevel", "8");
262         }
263
264         ent_flags = check_entity_flags("func_door", "spawnflags");
265         if (ent_flags)
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");
268         if (ent_flags)
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");
271         if (ent_flags)
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");
274         if (ent_flags)
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");
277         if (ent_flags)
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");
280         if (ent_flags)
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");
284         if (ent_flags)
285                 snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message), "Found %i lights with no color value\n", ent_flags);
286
287         // no errors found
288         if (!strlen(message)) {
289                 snprintf(message, sizeof(message) - 1, "No errors found - you are ready to compile the map now\n");
290         }
291
292         *returnMsg = message;
293 }