- Updated UFA:Plugin (mattn2)
[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 {
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, maxteams and so on
145  */
146 void assign_default_values_to_worldspawn (bool override, bool day, 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         get_team_count("info_player_start", &count, &teams);
166
167         // TODO: Get highest brush - a level has 64 units
168         worldspawn->setKeyValue("maxlevel", "5");
169
170         if (string_empty(worldspawn->getKeyValue("maxteams"))
171          || atoi(worldspawn->getKeyValue("maxteams")) != teams)
172         {
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);
178         }
179
180         if (day)
181         {
182                 if (override)
183                 {
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");
188                 }
189                 else
190                 {
191                         if (string_empty(worldspawn->getKeyValue("light")))
192                         {
193                                 worldspawn->setKeyValue("light", "160");
194                                 snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message), "Set ambient to: %s", worldspawn->getKeyValue("ambient"));
195                         }
196                         if (string_empty(worldspawn->getKeyValue("_color")))
197                         {
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"));
200                         }
201                         if (string_empty(worldspawn->getKeyValue("angles")))
202                         {
203                                 worldspawn->setKeyValue("angles", "30 210");
204                                 snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message), "Set ambient to: %s", worldspawn->getKeyValue("ambient"));
205                         }
206                         if (string_empty(worldspawn->getKeyValue("ambient")))
207                         {
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"));
210                         }
211                 }
212         }
213         else
214         {
215                 if (override)
216                 {
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");
221                 }
222                 else
223                 {
224                         if (string_empty(worldspawn->getKeyValue("light")))
225                         {
226                                 worldspawn->setKeyValue("light", "60");
227                                 snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message), "Set ambient to: %s", worldspawn->getKeyValue("ambient"));
228                         }
229                         if (string_empty(worldspawn->getKeyValue("_color")))
230                         {
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"));
233                         }
234                         if (string_empty(worldspawn->getKeyValue("angles")))
235                         {
236                                 worldspawn->setKeyValue("angles", "15 60");
237                                 snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message), "Set ambient to: %s", worldspawn->getKeyValue("ambient"));
238                         }
239                         if (string_empty(worldspawn->getKeyValue("ambient")))
240                         {
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"));
243                         }
244                 }
245         }
246
247         if (override)
248         {
249                 snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message),
250                         "Set light to: %s\n"
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")
258                 );
259         }
260
261         // no errors - no warnings
262         if (!strlen(message))
263                 return;
264
265         *returnMsg = message;
266 }
267
268 /**
269  * @brief
270  */
271 int check_entity_flags (const char *classname, const char *flag)
272 {
273         int count;
274
275         /* init this with 0 every time we browse the tree */
276         count = 0;
277
278         GlobalSceneGraph().traverse(EntityFindFlags(classname, flag, &count));
279         return count;
280 }
281
282 /**
283  * @brief Will check e.g. the map entities for valid values
284  * @todo: check for maxlevel
285  */
286 void check_map_values (char **returnMsg)
287 {
288         static char message[1024];
289         int count = 0;
290         int teams = 0;
291         int ent_flags;
292         Entity* worldspawn;
293         char str[64];
294
295         worldspawn = Scene_FindEntityByClass("worldspawn");
296         if (!worldspawn)
297         {
298                 globalOutputStream() << "UFO:AI: Could not find worldspawn.\n";
299                 *returnMsg = "Could not find worldspawn";
300                 return;
301         }
302
303         *message = '\0';
304         *str = '\0';
305
306         // multiplayer start positions
307         get_team_count("info_player_start", &count, &teams);
308         if (!count)
309                 strncat(message, "No multiplayer start positions (info_player_start)\n", sizeof(message) - 1);
310         else if (string_empty(worldspawn->getKeyValue("maxteams")))
311         {
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);
315         }
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);
318
319         // singleplayer map?
320         count = 0;
321         get_team_count("info_human_start", &count, NULL);
322         if (!count)
323                 strncat(message, "No singleplayer start positions (info_human_start)\n", sizeof(message) - 1);
324
325         // search for civilians
326         count = 0;
327         get_team_count("info_civilian_start", &count, NULL);
328         if (!count)
329                 strncat(message, "No civilian start positions (info_civilian_start)\n", sizeof(message) - 1);
330
331         // check maxlevel
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)
335         {
336                 strncat(message, "Worldspawn: Highest maxlevel is 8\n", sizeof(message) - 1);
337                 worldspawn->setKeyValue("maxlevel", "8");
338         }
339
340         ent_flags = check_entity_flags("func_door", "spawnflags");
341         if (ent_flags)
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");
344         if (ent_flags)
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");
347         if (ent_flags)
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");
350         if (ent_flags)
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");
353         if (ent_flags)
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");
357         if (ent_flags)
358                 snprintf(&message[strlen(message)], sizeof(message) - 1 - strlen(message), "Found %i lights with no color value\n", ent_flags);
359
360         // no errors found
361         if (!strlen(message)) {
362                 snprintf(message, sizeof(message) - 1, "No errors found - you are ready to compile the map now\n");
363         }
364
365         *returnMsg = message;
366 }