Debian Codegraph @

code/botlib/be_aas_bspq3.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_bspq3.c
25 *
26 * desc: BSP, Environment Sampling
27 *
28 * $Archive: /MissionPack/code/botlib/be_aas_bspq3.c $
29 *
30 *****************************************************************************/
31
32 #include "../qcommon/q_shared.h"
33 #include "l_memory.h"
34 #include "l_script.h"
35 #include "l_precomp.h"
36 #include "l_struct.h"
37 #include "aasfile.h"
38 #include "botlib.h"
39 #include "be_aas.h"
40 #include "be_aas_funcs.h"
41 #include "be_aas_def.h"
42
43 extern botlib_import_t botimport;
44
45 //#define TRACE_DEBUG
46
47 #define ON_EPSILON 0.005
48 //#define DEG2RAD( a ) (( a * M_PI ) / 180.0F)
49
50 #define MAX_BSPENTITIES 2048
51
52 typedef struct rgb_s
53 {
54 int red;
55 int green;
56 int blue;
57 } rgb_t;
58
59 //bsp entity epair
60 typedef struct bsp_epair_s
61 {
62 char *key;
63 char *value;
64 struct bsp_epair_s *next;
65 } bsp_epair_t;
66
67 //bsp data entity
68 typedef struct bsp_entity_s
69 {
70 bsp_epair_t *epairs;
71 } bsp_entity_t;
72
73 //id Software BSP data
74 typedef struct bsp_s
75 {
76 //true when bsp file is loaded
77 int loaded;
78 //entity data
79 int entdatasize;
80 char *dentdata;
81 //bsp entities
82 int numentities;
83 bsp_entity_t entities[MAX_BSPENTITIES];
84 } bsp_t;
85
86 //global bsp
87 bsp_t bspworld;
88
89
90 #ifdef BSP_DEBUG
91 typedef struct cname_s
92 {
93 int value;
94 char *name;
95 } cname_t;
96
97 cname_t contentnames[] =
98 {
99 {CONTENTS_SOLID,"CONTENTS_SOLID"},
100 {CONTENTS_WINDOW,"CONTENTS_WINDOW"},
101 {CONTENTS_AUX,"CONTENTS_AUX"},
102 {CONTENTS_LAVA,"CONTENTS_LAVA"},
103 {CONTENTS_SLIME,"CONTENTS_SLIME"},
104 {CONTENTS_WATER,"CONTENTS_WATER"},
105 {CONTENTS_MIST,"CONTENTS_MIST"},
106 {LAST_VISIBLE_CONTENTS,"LAST_VISIBLE_CONTENTS"},
107
108 {CONTENTS_AREAPORTAL,"CONTENTS_AREAPORTAL"},
109 {CONTENTS_PLAYERCLIP,"CONTENTS_PLAYERCLIP"},
110 {CONTENTS_MONSTERCLIP,"CONTENTS_MONSTERCLIP"},
111 {CONTENTS_CURRENT_0,"CONTENTS_CURRENT_0"},
112 {CONTENTS_CURRENT_90,"CONTENTS_CURRENT_90"},
113 {CONTENTS_CURRENT_180,"CONTENTS_CURRENT_180"},
114 {CONTENTS_CURRENT_270,"CONTENTS_CURRENT_270"},
115 {CONTENTS_CURRENT_UP,"CONTENTS_CURRENT_UP"},
116 {CONTENTS_CURRENT_DOWN,"CONTENTS_CURRENT_DOWN"},
117 {CONTENTS_ORIGIN,"CONTENTS_ORIGIN"},
118 {CONTENTS_MONSTER,"CONTENTS_MONSTER"},
119 {CONTENTS_DEADMONSTER,"CONTENTS_DEADMONSTER"},
120 {CONTENTS_DETAIL,"CONTENTS_DETAIL"},
121 {CONTENTS_TRANSLUCENT,"CONTENTS_TRANSLUCENT"},
122 {CONTENTS_LADDER,"CONTENTS_LADDER"},
123 {0, 0}
124 };
125
126 void PrintContents(int contents)
127 {
128 int i;
129
130 for (i = 0; contentnames[i].value; i++)
131 {
132 if (contents & contentnames[i].value)
133 {
134 botimport.Print(PRT_MESSAGE, "%s\n", contentnames[i].name);
135 } //end if
136 } //end for
137 } //end of the function PrintContents
138
139 #endif // BSP_DEBUG
140 //===========================================================================
141 // traces axial boxes of any size through the world
142 //
143 // Parameter: -
144 // Returns: -
145 // Changes Globals: -
146 //===========================================================================
147 bsp_trace_t AAS_Trace(vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end, int passent, int contentmask)
148 {
149 bsp_trace_t bsptrace;
150 botimport.Trace(&bsptrace, start, mins, maxs, end, passent, contentmask);
151 return bsptrace;
152 } //end of the function AAS_Trace
153 //===========================================================================
154 // returns the contents at the given point
155 //
156 // Parameter: -
157 // Returns: -
158 // Changes Globals: -
159 //===========================================================================
160 int AAS_PointContents(vec3_t point)
161 {
162 return botimport.PointContents(point);
163 } //end of the function AAS_PointContents
164 //===========================================================================
165 //
166 // Parameter: -
167 // Returns: -
168 // Changes Globals: -
169 //===========================================================================
170 qboolean AAS_EntityCollision(int entnum,
171 vec3_t start, vec3_t boxmins, vec3_t boxmaxs, vec3_t end,
172 int contentmask, bsp_trace_t *trace)
173 {
174 bsp_trace_t enttrace;
175
176 botimport.EntityTrace(&enttrace, start, boxmins, boxmaxs, end, entnum, contentmask);
177 if (enttrace.fraction < trace->fraction)
178 {
179 Com_Memcpy(trace, &enttrace, sizeof(bsp_trace_t));
180 return qtrue;
181 } //end if
182 return qfalse;
183 } //end of the function AAS_EntityCollision
184 //===========================================================================
185 // returns true if in Potentially Hearable Set
186 //
187 // Parameter: -
188 // Returns: -
189 // Changes Globals: -
190 //===========================================================================
191 qboolean AAS_inPVS(vec3_t p1, vec3_t p2)
192 {
193 return botimport.inPVS(p1, p2);
194 } //end of the function AAS_InPVS
195 //===========================================================================
196 // returns true if in Potentially Visible Set
197 //
198 // Parameter: -
199 // Returns: -
200 // Changes Globals: -
201 //===========================================================================
202 qboolean AAS_inPHS(vec3_t p1, vec3_t p2)
203 {
204 return qtrue;
205 } //end of the function AAS_inPHS
206 //===========================================================================
207 //
208 // Parameter: -
209 // Returns: -
210 // Changes Globals: -
211 //===========================================================================
212 void AAS_BSPModelMinsMaxsOrigin(int modelnum, vec3_t angles, vec3_t mins, vec3_t maxs, vec3_t origin)
213 {
214 botimport.BSPModelMinsMaxsOrigin(modelnum, angles, mins, maxs, origin);
215 } //end of the function AAS_BSPModelMinsMaxs
216 //===========================================================================
217 // unlinks the entity from all leaves
218 //
219 // Parameter: -
220 // Returns: -
221 // Changes Globals: -
222 //===========================================================================
223 void AAS_UnlinkFromBSPLeaves(bsp_link_t *leaves)
224 {
225 } //end of the function AAS_UnlinkFromBSPLeaves
226 //===========================================================================
227 //
228 // Parameter: -
229 // Returns: -
230 // Changes Globals: -
231 //===========================================================================
232 bsp_link_t *AAS_BSPLinkEntity(vec3_t absmins, vec3_t absmaxs, int entnum, int modelnum)
233 {
234 return NULL;
235 } //end of the function AAS_BSPLinkEntity
236 //===========================================================================
237 //
238 // Parameter: -
239 // Returns: -
240 // Changes Globals: -
241 //===========================================================================
242 int AAS_BoxEntities(vec3_t absmins, vec3_t absmaxs, int *list, int maxcount)
243 {
244 return 0;
245 } //end of the function AAS_BoxEntities
246 //===========================================================================
247 //
248 // Parameter: -
249 // Returns: -
250 // Changes Globals: -
251 //===========================================================================
252 int AAS_NextBSPEntity(int ent)
253 {
254 ent++;
255 if (ent >= 1 && ent < bspworld.numentities) return ent;
256 return 0;
257 } //end of the function AAS_NextBSPEntity
258 //===========================================================================
259 //
260 // Parameter: -
261 // Returns: -
262 // Changes Globals: -
263 //===========================================================================
264 int AAS_BSPEntityInRange(int ent)
265 {
266 if (ent <= 0 || ent >= bspworld.numentities)
267 {
268 botimport.Print(PRT_MESSAGE, "bsp entity out of range\n");
269 return qfalse;
270 } //end if
271 return qtrue;
272 } //end of the function AAS_BSPEntityInRange
273 //===========================================================================
274 //
275 // Parameter: -
276 // Returns: -
277 // Changes Globals: -
278 //===========================================================================
279 int AAS_ValueForBSPEpairKey(int ent, char *key, char *value, int size)
280 {
281 bsp_epair_t *epair;
282
283 value[0] = '\0';
284 if (!AAS_BSPEntityInRange(ent)) return qfalse;
285 for (epair = bspworld.entities[ent].epairs; epair; epair = epair->next)
286 {
287 if (!strcmp(epair->key, key))
288 {
289 Q_strncpyz(value, epair->value, size);
290 return qtrue;
291 } //end if
292 } //end for
293 return qfalse;
294 } //end of the function AAS_FindBSPEpair
295 //===========================================================================
296 //
297 // Parameter: -
298 // Returns: -
299 // Changes Globals: -
300 //===========================================================================
301 int AAS_VectorForBSPEpairKey(int ent, char *key, vec3_t v)
302 {
303 char buf[MAX_EPAIRKEY];
304 double v1, v2, v3;
305
306 VectorClear(v);
307 if (!AAS_ValueForBSPEpairKey(ent, key, buf, MAX_EPAIRKEY)) return qfalse;
308 //scanf into doubles, then assign, so it is vec_t size independent
309 v1 = v2 = v3 = 0;
310 sscanf(buf, "%lf %lf %lf", &v1, &v2, &v3);
311 v[0] = v1;
312 v[1] = v2;
313 v[2] = v3;
314 return qtrue;
315 } //end of the function AAS_VectorForBSPEpairKey
316 //===========================================================================
317 //
318 // Parameter: -
319 // Returns: -
320 // Changes Globals: -
321 //===========================================================================
322 int AAS_FloatForBSPEpairKey(int ent, char *key, float *value)
323 {
324 char buf[MAX_EPAIRKEY];
325
326 *value = 0;
327 if (!AAS_ValueForBSPEpairKey(ent, key, buf, MAX_EPAIRKEY)) return qfalse;
328 *value = atof(buf);
329 return qtrue;
330 } //end of the function AAS_FloatForBSPEpairKey
331 //===========================================================================
332 //
333 // Parameter: -
334 // Returns: -
335 // Changes Globals: -
336 //===========================================================================
337 int AAS_IntForBSPEpairKey(int ent, char *key, int *value)
338 {
339 char buf[MAX_EPAIRKEY];
340
341 *value = 0;
342 if (!AAS_ValueForBSPEpairKey(ent, key, buf, MAX_EPAIRKEY)) return qfalse;
343 *value = atoi(buf);
344 return qtrue;
345 } //end of the function AAS_IntForBSPEpairKey
346 //===========================================================================
347 //
348 // Parameter: -
349 // Returns: -
350 // Changes Globals: -
351 //===========================================================================
352 void AAS_FreeBSPEntities(void)
353 {
354 int i;
355 bsp_entity_t *ent;
356 bsp_epair_t *epair, *nextepair;
357
358 for (i = 1; i < bspworld.numentities; i++)
359 {
360 ent = &bspworld.entities[i];
361 for (epair = ent->epairs; epair; epair = nextepair)
362 {
363 nextepair = epair->next;
364 //
365 if (epair->key) FreeMemory(epair->key);
366 if (epair->value) FreeMemory(epair->value);
367 FreeMemory(epair);
368 } //end for
369 } //end for
370 bspworld.numentities = 0;
371 } //end of the function AAS_FreeBSPEntities
372 //===========================================================================
373 //
374 // Parameter: -
375 // Returns: -
376 // Changes Globals: -
377 //===========================================================================
378 void AAS_ParseBSPEntities(void)
379 {
380 script_t *script;
381 token_t token;
382 bsp_entity_t *ent;
383 bsp_epair_t *epair;
384
385 script = LoadScriptMemory(bspworld.dentdata, bspworld.entdatasize, "entdata");
386 SetScriptFlags(script, SCFL_NOSTRINGWHITESPACES|SCFL_NOSTRINGESCAPECHARS);//SCFL_PRIMITIVE);
387
388 bspworld.numentities = 1;
389
390 while(PS_ReadToken(script, &token))
391 {
392 if (strcmp(token.string, "{"))
393 {
394 ScriptError(script, "invalid %s", token.string);
395 AAS_FreeBSPEntities();
396 FreeScript(script);
397 return;
398 } //end if
399 if (bspworld.numentities >= MAX_BSPENTITIES)
400 {
401 botimport.Print(PRT_MESSAGE, "too many entities in BSP file\n");
402 break;
403 } //end if
404 ent = &bspworld.entities[bspworld.numentities];
405 bspworld.numentities++;
406 ent->epairs = NULL;
407 while(PS_ReadToken(script, &token))
408 {
409 if (!strcmp(token.string, "}")) break;
410 epair = (bsp_epair_t *) GetClearedHunkMemory(sizeof(bsp_epair_t));
411 epair->next = ent->epairs;
412 ent->epairs = epair;
413 if (token.type != TT_STRING)
414 {
415 ScriptError(script, "invalid %s", token.string);
416 AAS_FreeBSPEntities();
417 FreeScript(script);
418 return;
419 } //end if
420 StripDoubleQuotes(token.string);
421 epair->key = (char *) GetHunkMemory(strlen(token.string) + 1);
422 strcpy(epair->key, token.string);
423 if (!PS_ExpectTokenType(script, TT_STRING, 0, &token))
424 {
425 AAS_FreeBSPEntities();
426 FreeScript(script);
427 return;
428 } //end if
429 StripDoubleQuotes(token.string);
430 epair->value = (char *) GetHunkMemory(strlen(token.string) + 1);
431 strcpy(epair->value, token.string);
432 } //end while
433 if (strcmp(token.string, "}"))
434 {
435 ScriptError(script, "missing }");
436 AAS_FreeBSPEntities();
437 FreeScript(script);
438 return;
439 } //end if
440 } //end while
441 FreeScript(script);
442 } //end of the function AAS_ParseBSPEntities
443 //===========================================================================
444 //
445 // Parameter: -
446 // Returns: -
447 // Changes Globals: -
448 //===========================================================================
449 int AAS_BSPTraceLight(vec3_t start, vec3_t end, vec3_t endpos, int *red, int *green, int *blue)
450 {
451 return 0;
452 } //end of the function AAS_BSPTraceLight
453 //===========================================================================
454 //
455 // Parameter: -
456 // Returns: -
457 // Changes Globals: -
458 //===========================================================================
459 void AAS_DumpBSPData(void)
460 {
461 AAS_FreeBSPEntities();
462
463 if (bspworld.dentdata) FreeMemory(bspworld.dentdata);
464 bspworld.dentdata = NULL;
465 bspworld.entdatasize = 0;
466 //
467 bspworld.loaded = qfalse;
468 Com_Memset( &bspworld, 0, sizeof(bspworld) );
469 } //end of the function AAS_DumpBSPData
470 //===========================================================================
471 // load a .bsp file
472 //
473 // Parameter: -
474 // Returns: -
475 // Changes Globals: -
476 //===========================================================================
477 int AAS_LoadBSPFile(void)
478 {
479 AAS_DumpBSPData();
480 bspworld.entdatasize = strlen(botimport.BSPEntityData()) + 1;
481 bspworld.dentdata = (char *) GetClearedHunkMemory(bspworld.entdatasize);
482 Com_Memcpy(bspworld.dentdata, botimport.BSPEntityData(), bspworld.entdatasize);
483 AAS_ParseBSPEntities();
484 bspworld.loaded = qtrue;
485 return BLERR_NOERROR;
486 } //end of the function AAS_LoadBSPFile