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