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"
33 * @brief find entities by class
34 * @note from radiant/map.cpp
36 class EntityFindByClassname : public scene::Graph::Walker
41 EntityFindByClassname( const char* name, Entity*& entity ) : m_name( name ), m_entity( entity ){
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" ) ) ) {
58 Entity* Scene_FindEntityByClass( const char* name ){
59 Entity* entity = NULL;
60 GlobalSceneGraph().traverse( EntityFindByClassname( name, entity ) );
65 * @brief finds start positions
67 class EntityFindFlags : public scene::Graph::Walker
69 const char *m_classname;
74 EntityFindFlags( const char *classname, const char *flag, int *count ) : m_classname( classname ), m_flag( flag ), m_count( count ){
76 bool pre( const scene::Path& path, scene::Instance& instance ) const {
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 ) ) {
91 * @brief finds start positions
93 class EntityFindTeams : public scene::Graph::Walker
95 const char *m_classname;
100 EntityFindTeams( const char *classname, int *count, int *team ) : m_classname( classname ), m_count( count ), m_team( team ){
102 bool pre( const scene::Path& path, scene::Instance& instance ) const {
104 Entity* entity = Node_getEntity( path.top() );
105 if ( entity != 0 && string_equal( m_classname, entity->getKeyValue( "classname" ) ) ) {
109 // now get the highest teamnum
111 str = entity->getKeyValue( "team" );
112 if ( !string_empty( str ) ) {
113 if ( atoi( str ) > *m_team ) {
114 ( *m_team ) = atoi( str );
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";
132 * @brief Some default values to worldspawn like maxlevel and so on
134 void assign_default_values_to_worldspawn( bool override, char **returnMsg ){
135 static char message[1024];
141 worldspawn = Scene_FindEntityByClass( "worldspawn" );
143 globalOutputStream() << "UFO:AI: Could not find worldspawn.\n";
144 *returnMsg = "Could not find worldspawn";
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" ) );
157 if ( override || string_empty( worldspawn->getKeyValue( "maxteams" ) ) ) {
158 get_team_count( "info_player_start", &count, &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" ) );
165 snprintf( &message[strlen( message )], sizeof( message ) - 1 - strlen( message ), "You should at least place 16 info_player_start" );
169 // no errors - no warnings
170 if ( !strlen( message ) ) {
174 *returnMsg = message;
180 int check_entity_flags( const char *classname, const char *flag ){
183 /* init this with 0 every time we browse the tree */
186 GlobalSceneGraph().traverse( EntityFindFlags( classname, flag, &count ) );
191 * @brief Will check e.g. the map entities for valid values
192 * @todo: check for maxlevel
194 void check_map_values( char **returnMsg ){
195 static char message[1024];
202 worldspawn = Scene_FindEntityByClass( "worldspawn" );
204 globalOutputStream() << "UFO:AI: Could not find worldspawn.\n";
205 *returnMsg = "Could not find worldspawn";
212 // multiplayer start positions
213 get_team_count( "info_player_start", &count, &teams );
215 strncat( message, "No multiplayer start positions (info_player_start)\n", sizeof( message ) - 1 );
220 get_team_count( "info_human_start", &count, NULL );
222 strncat( message, "No singleplayer start positions (info_human_start)\n", sizeof( message ) - 1 );
227 get_team_count( "info_2x2_start", &count, NULL );
229 strncat( message, "No singleplayer start positions for 2x2 units (info_2x2_start)\n", sizeof( message ) - 1 );
232 // search for civilians
234 get_team_count( "info_civilian_start", &count, NULL );
236 strncat( message, "No civilian start positions (info_civilian_start)\n", sizeof( message ) - 1 );
240 if ( string_empty( worldspawn->getKeyValue( "maxlevel" ) ) ) {
241 strncat( message, "Worldspawn: No maxlevel defined\n", sizeof( message ) - 1 );
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" );
248 ent_flags = check_entity_flags( "func_door", "spawnflags" );
250 snprintf( &message[strlen( message )], sizeof( message ) - 1 - strlen( message ), "Found %i func_door with no spawnflags\n", ent_flags );
252 ent_flags = check_entity_flags( "func_breakable", "spawnflags" );
254 snprintf( &message[strlen( message )], sizeof( message ) - 1 - strlen( message ), "Found %i func_breakable with no spawnflags\n", ent_flags );
256 ent_flags = check_entity_flags( "misc_sound", "spawnflags" );
258 snprintf( &message[strlen( message )], sizeof( message ) - 1 - strlen( message ), "Found %i misc_sound with no spawnflags\n", ent_flags );
260 ent_flags = check_entity_flags( "misc_model", "spawnflags" );
262 snprintf( &message[strlen( message )], sizeof( message ) - 1 - strlen( message ), "Found %i misc_model with no spawnflags\n", ent_flags );
264 ent_flags = check_entity_flags( "misc_particle", "spawnflags" );
266 snprintf( &message[strlen( message )], sizeof( message ) - 1 - strlen( message ), "Found %i misc_particle with no spawnflags\n", ent_flags );
268 ent_flags = check_entity_flags( "info_player_start", "team" );
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 );
272 ent_flags = check_entity_flags( "light", "color" );
273 ent_flags = check_entity_flags( "light", "_color" );
275 snprintf( &message[strlen( message )], sizeof( message ) - 1 - strlen( message ), "Found %i lights with no color value\n", ent_flags );
279 if ( !strlen( message ) ) {
280 snprintf( message, sizeof( message ) - 1, "No errors found - you are ready to compile the map now\n" );
283 *returnMsg = message;