2 This file is part of GtkRadiant.
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.
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.
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
19 #include "ufoai_level.h"
20 #include "ufoai_filters.h"
24 #include "iscenegraph.h"
26 #include "string/string.h"
32 * @brief find entities by class
33 * @note from radiant/map.cpp
35 class EntityFindByClassname : public scene::Graph::Walker
40 EntityFindByClassname( const char* name, Entity*& entity ) : m_name( name ), m_entity( entity ){
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" ) ) ) {
57 Entity* Scene_FindEntityByClass( const char* name ){
58 Entity* entity = NULL;
59 GlobalSceneGraph().traverse( EntityFindByClassname( name, entity ) );
64 * @brief finds start positions
66 class EntityFindFlags : public scene::Graph::Walker
68 const char *m_classname;
73 EntityFindFlags( const char *classname, const char *flag, int *count ) : m_classname( classname ), m_flag( flag ), m_count( count ){
75 bool pre( const scene::Path& path, scene::Instance& instance ) const {
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 ) ) {
90 * @brief finds start positions
92 class EntityFindTeams : public scene::Graph::Walker
94 const char *m_classname;
99 EntityFindTeams( const char *classname, int *count, int *team ) : m_classname( classname ), m_count( count ), m_team( team ){
101 bool pre( const scene::Path& path, scene::Instance& instance ) const {
103 Entity* entity = Node_getEntity( path.top() );
104 if ( entity != 0 && string_equal( m_classname, entity->getKeyValue( "classname" ) ) ) {
108 // now get the highest teamnum
110 str = entity->getKeyValue( "team" );
111 if ( !string_empty( str ) ) {
112 if ( atoi( str ) > *m_team ) {
113 ( *m_team ) = atoi( str );
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";
131 * @brief Some default values to worldspawn like maxlevel and so on
133 void assign_default_values_to_worldspawn( bool override, char **returnMsg ){
134 static char message[1024];
140 worldspawn = Scene_FindEntityByClass( "worldspawn" );
142 globalOutputStream() << "UFO:AI: Could not find worldspawn.\n";
143 *returnMsg = "Could not find worldspawn";
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" ) );
156 if ( override || string_empty( worldspawn->getKeyValue( "maxteams" ) ) ) {
157 get_team_count( "info_player_start", &count, &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" ) );
164 snprintf( &message[strlen( message )], sizeof( message ) - 1 - strlen( message ), "You should at least place 16 info_player_start" );
168 // no errors - no warnings
169 if ( !strlen( message ) ) {
173 *returnMsg = message;
179 int check_entity_flags( const char *classname, const char *flag ){
182 /* init this with 0 every time we browse the tree */
185 GlobalSceneGraph().traverse( EntityFindFlags( classname, flag, &count ) );
190 * @brief Will check e.g. the map entities for valid values
191 * @todo: check for maxlevel
193 void check_map_values( char **returnMsg ){
194 static char message[1024];
201 worldspawn = Scene_FindEntityByClass( "worldspawn" );
203 globalOutputStream() << "UFO:AI: Could not find worldspawn.\n";
204 *returnMsg = "Could not find worldspawn";
211 // multiplayer start positions
212 get_team_count( "info_player_start", &count, &teams );
214 strncat( message, "No multiplayer start positions (info_player_start)\n", sizeof( message ) - 1 );
219 get_team_count( "info_human_start", &count, NULL );
221 strncat( message, "No singleplayer start positions (info_human_start)\n", sizeof( message ) - 1 );
226 get_team_count( "info_2x2_start", &count, NULL );
228 strncat( message, "No singleplayer start positions for 2x2 units (info_2x2_start)\n", sizeof( message ) - 1 );
231 // search for civilians
233 get_team_count( "info_civilian_start", &count, NULL );
235 strncat( message, "No civilian start positions (info_civilian_start)\n", sizeof( message ) - 1 );
239 if ( string_empty( worldspawn->getKeyValue( "maxlevel" ) ) ) {
240 strncat( message, "Worldspawn: No maxlevel defined\n", sizeof( message ) - 1 );
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" );
247 ent_flags = check_entity_flags( "func_door", "spawnflags" );
249 snprintf( &message[strlen( message )], sizeof( message ) - 1 - strlen( message ), "Found %i func_door with no spawnflags\n", ent_flags );
251 ent_flags = check_entity_flags( "func_breakable", "spawnflags" );
253 snprintf( &message[strlen( message )], sizeof( message ) - 1 - strlen( message ), "Found %i func_breakable with no spawnflags\n", ent_flags );
255 ent_flags = check_entity_flags( "misc_sound", "spawnflags" );
257 snprintf( &message[strlen( message )], sizeof( message ) - 1 - strlen( message ), "Found %i misc_sound with no spawnflags\n", ent_flags );
259 ent_flags = check_entity_flags( "misc_model", "spawnflags" );
261 snprintf( &message[strlen( message )], sizeof( message ) - 1 - strlen( message ), "Found %i misc_model with no spawnflags\n", ent_flags );
263 ent_flags = check_entity_flags( "misc_particle", "spawnflags" );
265 snprintf( &message[strlen( message )], sizeof( message ) - 1 - strlen( message ), "Found %i misc_particle with no spawnflags\n", ent_flags );
267 ent_flags = check_entity_flags( "info_player_start", "team" );
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 );
271 ent_flags = check_entity_flags( "light", "color" );
272 ent_flags = check_entity_flags( "light", "_color" );
274 snprintf( &message[strlen( message )], sizeof( message ) - 1 - strlen( message ), "Found %i lights with no color value\n", ent_flags );
278 if ( !strlen( message ) ) {
279 snprintf( message, sizeof( message ) - 1, "No errors found - you are ready to compile the map now\n" );
282 *returnMsg = message;