Debian Codegraph @

code/botlib/be_aas_main.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4
5 This file is part of Quake III Arena source code.
6
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22
23 /*****************************************************************************
24 * name: be_aas_main.c
25 *
26 * desc: AAS
27 *
28 * $Archive: /MissionPack/code/botlib/be_aas_main.c $
29 *
30 *****************************************************************************/
31
32 #include "../qcommon/q_shared.h"
33 #include "l_memory.h"
34 #include "l_libvar.h"
35 #include "l_utils.h"
36 #include "l_script.h"
37 #include "l_precomp.h"
38 #include "l_struct.h"
39 #include "l_log.h"
40 #include "aasfile.h"
41 #include "botlib.h"
42 #include "be_aas.h"
43 #include "be_aas_funcs.h"
44 #include "be_interface.h"
45 #include "be_aas_def.h"
46
47 aas_t aasworld;
48
49 libvar_t *saveroutingcache;
50
51 //===========================================================================
52 //
53 // Parameter: -
54 // Returns: -
55 // Changes Globals: -
56 //===========================================================================
57 void QDECL AAS_Error(char *fmt, ...)
58 {
59 char str[1024];
60 va_list arglist;
61
62 va_start(arglist, fmt);
63 Q_vsnprintf(str, sizeof(str), fmt, arglist);
64 va_end(arglist);
65 botimport.Print(PRT_FATAL, "%s", str);
66 } //end of the function AAS_Error
67 //===========================================================================
68 //
69 // Parameter: -
70 // Returns: -
71 // Changes Globals: -
72 //===========================================================================
73 int AAS_Loaded(void)
74 {
75 return aasworld.loaded;
76 } //end of the function AAS_Loaded
77 //===========================================================================
78 //
79 // Parameter: -
80 // Returns: -
81 // Changes Globals: -
82 //===========================================================================
83 int AAS_Initialized(void)
84 {
85 return aasworld.initialized;
86 } //end of the function AAS_Initialized
87 //===========================================================================
88 //
89 // Parameter: -
90 // Returns: -
91 // Changes Globals: -
92 //===========================================================================
93 void AAS_SetInitialized(void)
94 {
95 aasworld.initialized = qtrue;
96 botimport.Print(PRT_MESSAGE, "AAS initialized.\n");
97 #ifdef DEBUG
98 //create all the routing cache
99 //AAS_CreateAllRoutingCache();
100 //
101 //AAS_RoutingInfo();
102 #endif
103 } //end of the function AAS_SetInitialized
104 //===========================================================================
105 //
106 // Parameter: -
107 // Returns: -
108 // Changes Globals: -
109 //===========================================================================
110 void AAS_ContinueInit(float time)
111 {
112 //if no AAS file loaded
113 if (!aasworld.loaded) return;
114 //if AAS is already initialized
115 if (aasworld.initialized) return;
116 //calculate reachability, if not finished return
117 if (AAS_ContinueInitReachability(time)) return;
118 //initialize clustering for the new map
119 AAS_InitClustering();
120 //if reachability has been calculated and an AAS file should be written
121 //or there is a forced data optimization
122 if (aasworld.savefile || ((int)LibVarGetValue("forcewrite")))
123 {
124 //optimize the AAS data
125 if ((int)LibVarValue("aasoptimize", "0")) AAS_Optimize();
126 //save the AAS file
127 if (AAS_WriteAASFile(aasworld.filename))
128 {
129 botimport.Print(PRT_MESSAGE, "%s written successfully\n", aasworld.filename);
130 } //end if
131 else
132 {
133 botimport.Print(PRT_ERROR, "couldn't write %s\n", aasworld.filename);
134 } //end else
135 } //end if
136 //initialize the routing
137 AAS_InitRouting();
138 //at this point AAS is initialized
139 AAS_SetInitialized();
140 } //end of the function AAS_ContinueInit
141 //===========================================================================
142 // called at the start of every frame
143 //
144 // Parameter: -
145 // Returns: -
146 // Changes Globals: -
147 //===========================================================================
148 int AAS_StartFrame(float time)
149 {
150 aasworld.time = time;
151 //unlink all entities that were not updated last frame
152 AAS_UnlinkInvalidEntities();
153 //invalidate the entities
154 AAS_InvalidateEntities();
155 //initialize AAS
156 AAS_ContinueInit(time);
157 //
158 aasworld.frameroutingupdates = 0;
159 //
160 if (botDeveloper)
161 {
162 if (LibVarGetValue("showcacheupdates"))
163 {
164 AAS_RoutingInfo();
165 LibVarSet("showcacheupdates", "0");
166 } //end if
167 if (LibVarGetValue("showmemoryusage"))
168 {
169 PrintUsedMemorySize();
170 LibVarSet("showmemoryusage", "0");
171 } //end if
172 if (LibVarGetValue("memorydump"))
173 {
174 PrintMemoryLabels();
175 LibVarSet("memorydump", "0");
176 } //end if
177 } //end if
178 //
179 if (saveroutingcache->value)
180 {
181 AAS_WriteRouteCache();
182 LibVarSet("saveroutingcache", "0");
183 } //end if
184 //
185 aasworld.numframes++;
186 return BLERR_NOERROR;
187 } //end of the function AAS_StartFrame
188 //===========================================================================
189 //
190 // Parameter: -
191 // Returns: -
192 // Changes Globals: -
193 //===========================================================================
194 float AAS_Time(void)
195 {
196 return aasworld.time;
197 } //end of the function AAS_Time
198 //===========================================================================
199 //
200 // Parameter: -
201 // Returns: -
202 // Changes Globals: -
203 //===========================================================================
204 void AAS_ProjectPointOntoVector( vec3_t point, vec3_t vStart, vec3_t vEnd, vec3_t vProj )
205 {
206 vec3_t pVec, vec;
207
208 VectorSubtract( point, vStart, pVec );
209 VectorSubtract( vEnd, vStart, vec );
210 VectorNormalize( vec );
211 // project onto the directional vector for this segment
212 VectorMA( vStart, DotProduct( pVec, vec ), vec, vProj );
213 } //end of the function AAS_ProjectPointOntoVector
214 //===========================================================================
215 //
216 // Parameter: -
217 // Returns: -
218 // Changes Globals: -
219 //===========================================================================
220 int AAS_LoadFiles(const char *mapname)
221 {
222 int errnum;
223 char aasfile[MAX_QPATH];
224
225 Q_strncpyz(aasworld.mapname, mapname, sizeof(aasworld.mapname));
226 //NOTE: first reset the entity links into the AAS areas and BSP leaves
227 // the AAS link heap and BSP link heap are reset after respectively the
228 // AAS file and BSP file are loaded
229 AAS_ResetEntityLinks();
230 // load bsp info
231 AAS_LoadBSPFile();
232
233 //load the aas file
234 Com_sprintf(aasfile, sizeof(aasfile), "maps/%s.aas", mapname);
235 errnum = AAS_LoadAASFile(aasfile);
236 if (errnum != BLERR_NOERROR)
237 return errnum;
238
239 botimport.Print(PRT_MESSAGE, "loaded %s\n", aasfile);
240 Q_strncpyz(aasworld.filename, aasfile, sizeof(aasworld.filename));
241 return BLERR_NOERROR;
242 } //end of the function AAS_LoadFiles
243 //===========================================================================
244 // called every time a map changes
245 //
246 // Parameter: -
247 // Returns: -
248 // Changes Globals: -
249 //===========================================================================
250 int AAS_LoadMap(const char *mapname)
251 {
252 int errnum;
253
254 //if no mapname is provided then the string indexes are updated
255 if (!mapname)
256 {
257 return 0;
258 } //end if
259 //
260 aasworld.initialized = qfalse;
261 //NOTE: free the routing caches before loading a new map because
262 // to free the caches the old number of areas, number of clusters
263 // and number of areas in a clusters must be available
264 AAS_FreeRoutingCaches();
265 //load the map
266 errnum = AAS_LoadFiles(mapname);
267 if (errnum != BLERR_NOERROR)
268 {
269 aasworld.loaded = qfalse;
270 return errnum;
271 } //end if
272 //
273 AAS_InitSettings();
274 //initialize the AAS link heap for the new map
275 AAS_InitAASLinkHeap();
276 //initialize the AAS linked entities for the new map
277 AAS_InitAASLinkedEntities();
278 //initialize reachability for the new map
279 AAS_InitReachability();
280 //initialize the alternative routing
281 AAS_InitAlternativeRouting();
282 //everything went ok
283 return 0;
284 } //end of the function AAS_LoadMap
285 //===========================================================================
286 // called when the library is first loaded
287 //
288 // Parameter: -
289 // Returns: -
290 // Changes Globals: -
291 //===========================================================================
292 int AAS_Setup(void)
293 {
294 aasworld.maxclients = (int) LibVarValue("maxclients", "128");
295 aasworld.maxentities = (int) LibVarValue("maxentities", "1024");
296 // as soon as it's set to 1 the routing cache will be saved
297 saveroutingcache = LibVar("saveroutingcache", "0");
298 //allocate memory for the entities
299 if (aasworld.entities) FreeMemory(aasworld.entities);
300 aasworld.entities = (aas_entity_t *) GetClearedHunkMemory(aasworld.maxentities * sizeof(aas_entity_t));
301 //invalidate all the entities
302 AAS_InvalidateEntities();
303 //force some recalculations
304 //LibVarSet("forceclustering", "1"); //force clustering calculation
305 //LibVarSet("forcereachability", "1"); //force reachability calculation
306 aasworld.numframes = 0;
307 return BLERR_NOERROR;
308 } //end of the function AAS_Setup
309 //===========================================================================
310 //
311 // Parameter: -
312 // Returns: -
313 // Changes Globals: -
314 //===========================================================================
315 void AAS_Shutdown(void)
316 {
317 AAS_ShutdownAlternativeRouting();
318 //
319 AAS_DumpBSPData();
320 //free routing caches
321 AAS_FreeRoutingCaches();
322 //free aas link heap
323 AAS_FreeAASLinkHeap();
324 //free aas linked entities
325 AAS_FreeAASLinkedEntities();
326 //free the aas data
327 AAS_DumpAASData();
328 //free the entities
329 if (aasworld.entities) FreeMemory(aasworld.entities);
330 //clear the aasworld structure
331 Com_Memset(&aasworld, 0, sizeof(aas_t));
332 //aas has not been initialized
333 aasworld.initialized = qfalse;
334 //NOTE: as soon as a new .bsp file is loaded the .bsp file memory is
335 // freed and reallocated, so there's no need to free that memory here
336 //print shutdown
337 botimport.Print(PRT_MESSAGE, "AAS shutdown.\n");
338 } //end of the function AAS_Shutdown