]> git.xonotic.org Git - xonotic/netradiant.git/blob - contrib/ufoaiplug/ufoai_level.cpp
reformat code! now the code is only ugly on the *inside*
[xonotic/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     const char *m_name;
37     Entity *&m_entity;
38 public:
39     EntityFindByClassname(const char *name, Entity *&entity) : m_name(name), m_entity(entity)
40     {
41         m_entity = 0;
42     }
43
44     bool pre(const scene::Path &path, scene::Instance &instance) const
45     {
46         if (m_entity == 0) {
47             Entity *entity = Node_getEntity(path.top());
48             if (entity != 0 && string_equal(m_name, entity->getKeyValue("classname"))) {
49                 m_entity = entity;
50             }
51         }
52         return true;
53     }
54 };
55
56 /**
57  * @brief
58  */
59 Entity *Scene_FindEntityByClass(const char *name)
60 {
61     Entity *entity = NULL;
62     GlobalSceneGraph().traverse(EntityFindByClassname(name, entity));
63     return entity;
64 }
65
66 /**
67  * @brief finds start positions
68  */
69 class EntityFindFlags : public scene::Graph::Walker {
70     const char *m_classname;
71     const char *m_flag;
72     int *m_count;
73
74 public:
75     EntityFindFlags(const char *classname, const char *flag, int *count) : m_classname(classname), m_flag(flag),
76                                                                            m_count(count)
77     {
78     }
79
80     bool pre(const scene::Path &path, scene::Instance &instance) const
81     {
82         const char *str;
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)) {
87                 (*m_count)++;
88             }
89         }
90         return true;
91     }
92 };
93
94
95 /**
96  * @brief finds start positions
97  */
98 class EntityFindTeams : public scene::Graph::Walker {
99     const char *m_classname;
100     int *m_count;
101     int *m_team;
102
103 public:
104     EntityFindTeams(const char *classname, int *count, int *team) : m_classname(classname), m_count(count), m_team(team)
105     {
106     }
107
108     bool pre(const scene::Path &path, scene::Instance &instance) const
109     {
110         const char *str;
111         Entity *entity = Node_getEntity(path.top());
112         if (entity != 0 && string_equal(m_classname, entity->getKeyValue("classname"))) {
113             if (m_count) {
114                 (*m_count)++;
115             }
116             // now get the highest teamnum
117             if (m_team) {
118                 str = entity->getKeyValue("team");
119                 if (!string_empty(str)) {
120                     if (atoi(str) > *m_team) {
121                         (*m_team) = atoi(str);
122                     }
123                 }
124             }
125         }
126         return true;
127     }
128 };
129
130 /**
131  * @brief
132  */
133 void get_team_count(const char *classname, int *count, int *team)
134 {
135     GlobalSceneGraph().traverse(EntityFindTeams(classname, count, team));
136     globalOutputStream() << "UFO:AI: classname: " << classname << ": #" << (*count) << "\n";
137 }
138
139 /**
140  * @brief Some default values to worldspawn like maxlevel and so on
141  */
142 void assign_default_values_to_worldspawn(bool override, const char **returnMsg)
143 {
144     static char message[1024];
145     Entity *worldspawn;
146     int teams = 0;
147     int count = 0;
148     char str[64];
149
150     worldspawn = Scene_FindEntityByClass("worldspawn");
151     if (!worldspawn) {
152         globalOutputStream() << "UFO:AI: Could not find worldspawn.\n";
153         *returnMsg = "Could not find worldspawn";
154         return;
155     }
156
157     *message = '\0';
158     *str = '\0';
159
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"));
165     }
166
167     if (override || string_empty(worldspawn->getKeyValue("maxteams"))) {
168         get_team_count("info_player_start", &count, &teams);
169         if (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"));
174         }
175         if (count < 16) {
176             snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message),
177                      "You should at least place 16 info_player_start");
178         }
179     }
180
181     // no errors - no warnings
182     if (!strlen(message)) {
183         return;
184     }
185
186     *returnMsg = message;
187 }
188
189 /**
190  * @brief
191  */
192 int check_entity_flags(const char *classname, const char *flag)
193 {
194     int count;
195
196     /* init this with 0 every time we browse the tree */
197     count = 0;
198
199     GlobalSceneGraph().traverse(EntityFindFlags(classname, flag, &count));
200     return count;
201 }
202
203 /**
204  * @brief Will check e.g. the map entities for valid values
205  * @todo: check for maxlevel
206  */
207 void check_map_values(const char **returnMsg)
208 {
209     static char message[1024];
210     int count = 0;
211     int teams = 0;
212     int ent_flags;
213     Entity *worldspawn;
214     char str[64];
215
216     worldspawn = Scene_FindEntityByClass("worldspawn");
217     if (!worldspawn) {
218         globalOutputStream() << "UFO:AI: Could not find worldspawn.\n";
219         *returnMsg = "Could not find worldspawn";
220         return;
221     }
222
223     *message = '\0';
224     *str = '\0';
225
226     // multiplayer start positions
227     get_team_count("info_player_start", &count, &teams);
228     if (!count) {
229         strncat(message, "No multiplayer start positions (info_player_start)\n", sizeof(message) - 1);
230     }
231
232     // singleplayer map?
233     count = 0;
234     get_team_count("info_human_start", &count, NULL);
235     if (!count) {
236         strncat(message, "No singleplayer start positions (info_human_start)\n", sizeof(message) - 1);
237     }
238
239     // singleplayer map?
240     count = 0;
241     get_team_count("info_2x2_start", &count, NULL);
242     if (!count) {
243         strncat(message, "No singleplayer start positions for 2x2 units (info_2x2_start)\n", sizeof(message) - 1);
244     }
245
246     // search for civilians
247     count = 0;
248     get_team_count("info_civilian_start", &count, NULL);
249     if (!count) {
250         strncat(message, "No civilian start positions (info_civilian_start)\n", sizeof(message) - 1);
251     }
252
253     // check maxlevel
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");
259     }
260
261     ent_flags = check_entity_flags("func_door", "spawnflags");
262     if (ent_flags) {
263         snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message),
264                  "Found %i func_door with no spawnflags\n", ent_flags);
265     }
266     ent_flags = check_entity_flags("func_breakable", "spawnflags");
267     if (ent_flags) {
268         snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message),
269                  "Found %i func_breakable with no spawnflags\n", ent_flags);
270     }
271     ent_flags = check_entity_flags("misc_sound", "spawnflags");
272     if (ent_flags) {
273         snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message),
274                  "Found %i misc_sound with no spawnflags\n", ent_flags);
275     }
276     ent_flags = check_entity_flags("misc_model", "spawnflags");
277     if (ent_flags) {
278         snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message),
279                  "Found %i misc_model with no spawnflags\n", ent_flags);
280     }
281     ent_flags = check_entity_flags("misc_particle", "spawnflags");
282     if (ent_flags) {
283         snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message),
284                  "Found %i misc_particle with no spawnflags\n", ent_flags);
285     }
286     ent_flags = check_entity_flags("info_player_start", "team");
287     if (ent_flags) {
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",
290                  ent_flags);
291     }
292     ent_flags = check_entity_flags("light", "color");
293     ent_flags = check_entity_flags("light", "_color");
294     if (ent_flags) {
295         snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message),
296                  "Found %i lights with no color value\n", ent_flags);
297     }
298
299     // no errors found
300     if (!strlen(message)) {
301         snprintf(message, sizeof(message) - 1, "No errors found - you are ready to compile the map now\n");
302     }
303
304     *returnMsg = message;
305 }