File: be_aas_main.c

package info (click to toggle)
iortcw 1.51.c%2Bdfsg1-7
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 25,304 kB
  • sloc: ansic: 457,326; cpp: 6,507; makefile: 4,737; sh: 1,292; asm: 1,176; xml: 31
file content (486 lines) | stat: -rw-r--r-- 15,701 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/*
===========================================================================

Return to Castle Wolfenstein single player GPL Source Code
Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. 

This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”).  

RTCW SP Source Code is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

RTCW SP Source Code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with RTCW SP Source Code.  If not, see <http://www.gnu.org/licenses/>.

In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code.  If not, please request a copy in writing from id Software at the address below.

If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.

===========================================================================
*/


/*****************************************************************************
 * name:		be_aas_main.c
 *
 * desc:		AAS
 *
 *
 *****************************************************************************/

#include "../qcommon/q_shared.h"
#include "l_memory.h"
#include "l_libvar.h"
#include "l_utils.h"
#include "l_script.h"
#include "l_precomp.h"
#include "l_struct.h"
#include "l_log.h"
#include "aasfile.h"
#include "botlib.h"
#include "be_aas.h"
#include "be_aas_funcs.h"
#include "be_interface.h"
#include "be_aas_def.h"

aas_t aasworlds[MAX_AAS_WORLDS];

aas_t *aasworld;

//===========================================================================
//
// Parameter:				-
// Returns:					-
// Changes Globals:		-
//===========================================================================
void QDECL AAS_Error( char *fmt, ... ) {
	char str[1024];
	va_list arglist;

	va_start( arglist, fmt );
	Q_vsnprintf(str, sizeof(str), fmt, arglist);
	va_end( arglist );
	botimport.Print(PRT_FATAL, "%s", str);
} //end of the function AAS_Error

// Ridah, multiple AAS worlds
//===========================================================================
//
// Parameter:				-
// Returns:					-
// Changes Globals:		-
//===========================================================================
void AAS_SetCurrentWorld( int index ) {
	if ( index >= MAX_AAS_WORLDS || index < 0 ) {
		AAS_Error( "AAS_SetCurrentWorld: index out of range\n" );
		return;
	}

	// set the current world pointer
	aasworld = &aasworlds[index];
}
// done.

//===========================================================================
//
// Parameter:				-
// Returns:					-
// Changes Globals:		-
//===========================================================================
char *AAS_StringFromIndex( char *indexname, char *stringindex[], int numindexes, int index ) {
	if ( !( *aasworld ).indexessetup ) {
		botimport.Print( PRT_ERROR, "%s: index %d not setup\n", indexname, index );
		return "";
	} //end if
	if ( index < 0 || index >= numindexes ) {
		botimport.Print( PRT_ERROR, "%s: index %d out of range\n", indexname, index );
		return "";
	} //end if
	if ( !stringindex[index] ) {
		if ( index ) {
			botimport.Print( PRT_ERROR, "%s: reference to unused index %d\n", indexname, index );
		} //end if
		return "";
	} //end if
	return stringindex[index];
} //end of the function AAS_StringFromIndex
//===========================================================================
//
// Parameter:				-
// Returns:					-
// Changes Globals:		-
//===========================================================================
int AAS_IndexFromString( char *indexname, char *stringindex[], int numindexes, char *string ) {
	int i;
	if ( !( *aasworld ).indexessetup ) {
		botimport.Print( PRT_ERROR, "%s: index not setup \"%s\"\n", indexname, string );
		return 0;
	} //end if
	for ( i = 0; i < numindexes; i++ )
	{
		if ( !stringindex[i] ) {
			continue;
		}
		if ( !Q_stricmp( stringindex[i], string ) ) {
			return i;
		}
	} //end for
	return 0;
} //end of the function AAS_IndexFromString
//===========================================================================
//
// Parameter:				-
// Returns:					-
// Changes Globals:		-
//===========================================================================
char *AAS_ModelFromIndex( int index ) {
//	return AAS_StringFromIndex("ModelFromIndex", &(*aasworld).configstrings[CS_MODELS], MAX_MODELS, index);
	return 0;   // removed so the CS_ defines could be removed from be_aas_def.h
} //end of the function AAS_ModelFromIndex
//===========================================================================
//
// Parameter:				-
// Returns:					-
// Changes Globals:		-
//===========================================================================
int AAS_IndexFromModel( char *modelname ) {
//	return AAS_IndexFromString("IndexFromModel", &(*aasworld).configstrings[CS_MODELS], MAX_MODELS, modelname);
	return 0;   // removed so the CS_ defines could be removed from be_aas_def.h
} //end of the function AAS_IndexFromModel
//===========================================================================
//
// Parameter:				-
// Returns:					-
// Changes Globals:		-
//===========================================================================
void AAS_UpdateStringIndexes( int numconfigstrings, char *configstrings[] ) {
	int i;
	//set string pointers and copy the strings
	for ( i = 0; i < numconfigstrings; i++ )
	{
		if ( configstrings[i] ) {
			//if ((*aasworld).configstrings[i]) FreeMemory((*aasworld).configstrings[i]);
			( *aasworld ).configstrings[i] = (char *) GetMemory( strlen( configstrings[i] ) + 1 );
			strcpy( ( *aasworld ).configstrings[i], configstrings[i] );
		} //end if
	} //end for
	( *aasworld ).indexessetup = qtrue;
} //end of the function AAS_UpdateStringIndexes
//===========================================================================
//
// Parameter:				-
// Returns:					-
// Changes Globals:		-
//===========================================================================
int AAS_Loaded( void ) {
	return ( *aasworld ).loaded;
} //end of the function AAS_Loaded
//===========================================================================
//
// Parameter:				-
// Returns:					-
// Changes Globals:		-
//===========================================================================
int AAS_Initialized( void ) {
	return ( *aasworld ).initialized;
} //end of the function AAS_Initialized
//===========================================================================
//
// Parameter:				-
// Returns:					-
// Changes Globals:		-
//===========================================================================
void AAS_SetInitialized( void ) {
	( *aasworld ).initialized = qtrue;
	botimport.Print( PRT_MESSAGE, "AAS initialized.\n" );
#ifdef DEBUG
	//create all the routing cache
	//AAS_CreateAllRoutingCache();
	//
	//AAS_RoutingInfo();
#endif

	// Ridah, build/load the route-table
	AAS_RT_BuildRouteTable();
	// done.

} //end of the function AAS_SetInitialized
//===========================================================================
//
// Parameter:				-
// Returns:					-
// Changes Globals:		-
//===========================================================================
void AAS_ContinueInit( float time ) {
	//if no AAS file loaded
	if ( !( *aasworld ).loaded ) {
		return;
	}
	//if AAS is already initialized
	if ( ( *aasworld ).initialized ) {
		return;
	}
	//calculate reachability, if not finished return
	if ( AAS_ContinueInitReachability( time ) ) {
		return;
	}
	//initialize clustering for the new map
	AAS_InitClustering();
	//if reachability has been calculated and an AAS file should be written
	//or there is a forced data optimization
	if ( ( *aasworld ).savefile || ( (int)LibVarGetValue( "forcewrite" ) ) ) {
		//optimize the AAS data
		if ( !( (int)LibVarValue( "nooptimize", "1" ) ) ) {
			AAS_Optimize();
		}
		//save the AAS file
		if ( AAS_WriteAASFile( ( *aasworld ).filename ) ) {
			botimport.Print(PRT_MESSAGE, "%s written successfully\n", ( *aasworld ).filename);
		} //end if
		else
		{
			botimport.Print( PRT_ERROR, "couldn't write %s\n", ( *aasworld ).filename );
		} //end else
	} //end if
	  //initialize the routing
	AAS_InitRouting();
	//at this point AAS is initialized
	AAS_SetInitialized();
} //end of the function AAS_ContinueInit
//===========================================================================
// called at the start of every frame
//
// Parameter:				-
// Returns:					-
// Changes Globals:		-
//===========================================================================
int AAS_StartFrame( float time ) {
	// Ridah, do each of the aasworlds
	int i;

	for ( i = 0; i < MAX_AAS_WORLDS; i++ )
	{
		AAS_SetCurrentWorld( i );

		( *aasworld ).time = time;
		//invalidate the entities
		AAS_InvalidateEntities();
		//initialize AAS
		AAS_ContinueInit( time );
		//
		( *aasworld ).frameroutingupdates = 0;
		//
		/* Ridah, disabled for speed
		if (LibVarGetValue("showcacheupdates"))
		{
			AAS_RoutingInfo();
			LibVarSet("showcacheupdates", "0");
		} //end if
		if (LibVarGetValue("showmemoryusage"))
		{
			PrintUsedMemorySize();
			LibVarSet("showmemoryusage", "0");
		} //end if
		if (LibVarGetValue("memorydump"))
		{
			PrintMemoryLabels();
			LibVarSet("memorydump", "0");
		} //end if
		*/
	} //end if
	( *aasworld ).numframes++;
	return BLERR_NOERROR;
} //end of the function AAS_StartFrame
//===========================================================================
//
// Parameter:				-
// Returns:					-
// Changes Globals:		-
//===========================================================================
float AAS_Time( void ) {
	return ( *aasworld ).time;
} //end of the function AAS_Time
//===========================================================================
// basedir	= Quake2 console basedir
// gamedir	= Quake2 console gamedir
//	mapname	= name of the map without extension (.bsp)
//
// Parameter:				-
// Returns:					-
// Changes Globals:		-
//===========================================================================
int AAS_LoadFiles( const char *mapname ) {
	int errnum;
	char aasfile[MAX_QPATH];

	Q_strncpyz( ( *aasworld ).mapname, mapname, sizeof( ( *aasworld ).mapname ) );
	//NOTE: first reset the entity links into the AAS areas and BSP leaves
	// the AAS link heap and BSP link heap are reset after respectively the
	// AAS file and BSP file are loaded
	AAS_ResetEntityLinks();
	//

	// load bsp info
	AAS_LoadBSPFile();

	//load the aas file
	Com_sprintf( aasfile, sizeof( aasfile ), "maps/%s.aas", mapname );
	errnum = AAS_LoadAASFile( aasfile );
	if ( errnum != BLERR_NOERROR ) {
		return errnum;
	}

	botimport.Print( PRT_MESSAGE, "loaded %s\n", aasfile );
	Q_strncpyz( ( *aasworld ).filename, aasfile, sizeof( ( *aasworld ).filename ) );
	return BLERR_NOERROR;
} //end of the function AAS_LoadFiles
//===========================================================================
// called everytime a map changes
//
// Parameter:				-
// Returns:					-
// Changes Globals:		-
//===========================================================================

// Ridah, modified this for multiple AAS files

int AAS_LoadMap( const char *mapname ) {
#define MAPNAME_LEN 256
	int errnum;
	int i;
	char this_mapname[MAPNAME_LEN], intstr[4];
	qboolean loaded = qfalse;
	int missingErrNum = 0;

	for ( i = 0; i < MAX_AAS_WORLDS; i++ )
	{
		AAS_SetCurrentWorld( i );

		Q_strncpyz( this_mapname, mapname, sizeof( this_mapname ) );
		Q_strcat( this_mapname, sizeof( this_mapname ) - strlen( this_mapname ) - 1, "_b" );
		Com_sprintf( intstr, sizeof( intstr ), "%i", i);
		Q_strcat( this_mapname, sizeof( this_mapname ) - strlen( this_mapname ) - 1, intstr );

		//if no mapname is provided then the string indexes are updated
		if ( !mapname ) {
			return 0;
		} //end if
		  //
		( *aasworld ).initialized = qfalse;
		//NOTE: free the routing caches before loading a new map because
		// to free the caches the old number of areas, number of clusters
		// and number of areas in a clusters must be available
		AAS_FreeRoutingCaches();
		//load the map
		errnum = AAS_LoadFiles( this_mapname );
		if ( errnum != BLERR_NOERROR ) {
			( *aasworld ).loaded = qfalse;
			// RF, we are allowed to skip one of the files, but not both
			//return errnum;
			missingErrNum = errnum;
			continue;
		} //end if
		  //
		loaded = qtrue;
		//
		AAS_InitSettings();
		//initialize the AAS link heap for the new map
		AAS_InitAASLinkHeap();
		//initialize the AAS linked entities for the new map
		AAS_InitAASLinkedEntities();
		//initialize reachability for the new map
		AAS_InitReachability();
		//initialize the alternative routing
		AAS_InitAlternativeRouting();
	}

	if ( !loaded ) {
		return missingErrNum;
	}

	//everything went ok
	return 0;
} //end of the function AAS_LoadMap

// done.

//===========================================================================
// called when the library is first loaded
//
// Parameter:				-
// Returns:					-
// Changes Globals:		-
//===========================================================================
int AAS_Setup( void ) {
	// Ridah, just use the default world for entities
	AAS_SetCurrentWorld( 0 );

	( *aasworlds ).maxclients = (int) LibVarValue( "maxclients", "128" );
	( *aasworlds ).maxentities = (int) LibVarValue( "maxentities", "2048" );
	//allocate memory for the entities
	if ( ( *aasworld ).entities ) {
		FreeMemory( ( *aasworld ).entities );
	}
	( *aasworld ).entities = (aas_entity_t *) GetClearedHunkMemory( ( *aasworld ).maxentities * sizeof( aas_entity_t ) );
	//invalidate all the entities
	AAS_InvalidateEntities();

	//force some recalculations
	//LibVarSet("forceclustering", "1");			//force clustering calculation
	//LibVarSet("forcereachability", "1");		//force reachability calculation
	( *aasworld ).numframes = 0;
	return BLERR_NOERROR;
} //end of the function AAS_Setup
//===========================================================================
//
// Parameter:				-
// Returns:					-
// Changes Globals:		-
//===========================================================================
void AAS_Shutdown( void ) {
	// Ridah, do each of the worlds
	int i;

	for ( i = 0; i < MAX_AAS_WORLDS; i++ )
	{
		AAS_SetCurrentWorld( i );

		// Ridah, kill the route-table data
		AAS_RT_ShutdownRouteTable();

		AAS_ShutdownAlternativeRouting();
		AAS_DumpBSPData();
		//free routing caches
		AAS_FreeRoutingCaches();
		//free aas link heap
		AAS_FreeAASLinkHeap();
		//free aas linked entities
		AAS_FreeAASLinkedEntities();
		//free the aas data
		AAS_DumpAASData();

		if ( i == 0 ) {
			//free the entities
			if ( ( *aasworld ).entities ) {
				FreeMemory( ( *aasworld ).entities );
			}
		}

		//clear the (*aasworld) structure
		memset( &( *aasworld ), 0, sizeof( aas_t ) );
		//aas has not been initialized
		( *aasworld ).initialized = qfalse;
	}

	//NOTE: as soon as a new .bsp file is loaded the .bsp file memory is
	// freed and reallocated, so there's no need to free that memory here
	//print shutdown
	botimport.Print( PRT_MESSAGE, "AAS shutdown.\n" );
} //end of the function AAS_Shutdown