Debian Codegraph @

contrib/ufoaiplug/ufoai_level.cpp

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