Debian Codegraph @

code/game/g_utils.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 // g_utils.c -- misc utility functions for game module
24
25 #include "g_local.h"
26
27 typedef struct {
28 char oldShader[MAX_QPATH];
29 char newShader[MAX_QPATH];
30 float timeOffset;
31 } shaderRemap_t;
32
33 #define MAX_SHADER_REMAPS 128
34
35 int remapCount = 0;
36 shaderRemap_t remappedShaders[MAX_SHADER_REMAPS];
37
38 void AddRemap(const char *oldShader, const char *newShader, float timeOffset) {
39 int i;
40
41 for (i = 0; i < remapCount; i++) {
42 if (Q_stricmp(oldShader, remappedShaders[i].oldShader) == 0) {
43 // found it, just update this one
44 strcpy(remappedShaders[i].newShader,newShader);
45 remappedShaders[i].timeOffset = timeOffset;
46 return;
47 }
48 }
49 if (remapCount < MAX_SHADER_REMAPS) {
50 strcpy(remappedShaders[remapCount].newShader,newShader);
51 strcpy(remappedShaders[remapCount].oldShader,oldShader);
52 remappedShaders[remapCount].timeOffset = timeOffset;
53 remapCount++;
54 }
55 }
56
57 const char *BuildShaderStateConfig(void) {
58 static char buff[MAX_STRING_CHARS*4];
59 char out[(MAX_QPATH * 2) + 5];
60 int i;
61
62 memset(buff, 0, sizeof(buff));
63 for (i = 0; i < remapCount; i++) {
64 Com_sprintf(out, (MAX_QPATH * 2) + 5, "%s=%s:%5.2f@", remappedShaders[i].oldShader, remappedShaders[i].newShader, remappedShaders[i].timeOffset);
65 Q_strcat( buff, sizeof( buff ), out);
66 }
67 return buff;
68 }
69
70 /*
71 =========================================================================
72
73 model / sound configstring indexes
74
75 =========================================================================
76 */
77
78 /*
79 ================
80 G_FindConfigstringIndex
81
82 ================
83 */
84 int G_FindConfigstringIndex( char *name, int start, int max, qboolean create ) {
85 int i;
86 char s[MAX_STRING_CHARS];
87
88 if ( !name || !name[0] ) {
89 return 0;
90 }
91
92 for ( i=1 ; i<max ; i++ ) {
93 trap_GetConfigstring( start + i, s, sizeof( s ) );
94 if ( !s[0] ) {
95 break;
96 }
97 if ( !strcmp( s, name ) ) {
98 return i;
99 }
100 }
101
102 if ( !create ) {
103 return 0;
104 }
105
106 if ( i == max ) {
107 G_Error( "G_FindConfigstringIndex: overflow" );
108 }
109
110 trap_SetConfigstring( start + i, name );
111
112 return i;
113 }
114
115
116 int G_ModelIndex( char *name ) {
117 return G_FindConfigstringIndex (name, CS_MODELS, MAX_MODELS, qtrue);
118 }
119
120 int G_SoundIndex( char *name ) {
121 return G_FindConfigstringIndex (name, CS_SOUNDS, MAX_SOUNDS, qtrue);
122 }
123
124 //=====================================================================
125
126
127 /*
128 ================
129 G_TeamCommand
130
131 Broadcasts a command to only a specific team
132 ================
133 */
134 void G_TeamCommand( team_t team, char *cmd ) {
135 int i;
136
137 for ( i = 0 ; i < level.maxclients ; i++ ) {
138 if ( level.clients[i].pers.connected == CON_CONNECTED ) {
139 if ( level.clients[i].sess.sessionTeam == team ) {
140 trap_SendServerCommand( i, va("%s", cmd ));
141 }
142 }
143 }
144 }
145
146
147 /*
148 =============
149 G_Find
150
151 Searches all active entities for the next one that holds
152 the matching string at fieldofs (use the FOFS() macro) in the structure.
153
154 Searches beginning at the entity after from, or the beginning if NULL
155 NULL will be returned if the end of the list is reached.
156
157 =============
158 */
159 gentity_t *G_Find (gentity_t *from, int fieldofs, const char *match)
160 {
161 char *s;
162
163 if (!from)
164 from = g_entities;
165 else
166 from++;
167
168 for ( ; from < &g_entities[level.num_entities] ; from++)
169 {
170 if (!from->inuse)
171 continue;
172 s = *(char **) ((byte *)from + fieldofs);
173 if (!s)
174 continue;
175 if (!Q_stricmp (s, match))
176 return from;
177 }
178
179 return NULL;
180 }
181
182
183 /*
184 =============
185 G_PickTarget
186
187 Selects a random entity from among the targets
188 =============
189 */
190 #define MAXCHOICES 32
191
192 gentity_t *G_PickTarget (char *targetname)
193 {
194 gentity_t *ent = NULL;
195 int num_choices = 0;
196 gentity_t *choice[MAXCHOICES];
197
198 if (!targetname)
199 {
200 G_Printf("G_PickTarget called with NULL targetname\n");
201 return NULL;
202 }
203
204 while(1)
205 {
206 ent = G_Find (ent, FOFS(targetname), targetname);
207 if (!ent)
208 break;
209 choice[num_choices++] = ent;
210 if (num_choices == MAXCHOICES)
211 break;
212 }
213
214 if (!num_choices)
215 {
216 G_Printf("G_PickTarget: target %s not found\n", targetname);
217 return NULL;
218 }
219
220 return choice[rand() % num_choices];
221 }
222
223
224 /*
225 ==============================
226 G_UseTargets
227
228 "activator" should be set to the entity that initiated the firing.
229
230 Search for (string)targetname in all entities that
231 match (string)self.target and call their .use function
232
233 ==============================
234 */
235 void G_UseTargets( gentity_t *ent, gentity_t *activator ) {
236 gentity_t *t;
237
238 if ( !ent ) {
239 return;
240 }
241
242 if (ent->targetShaderName && ent->targetShaderNewName) {
243 float f = level.time * 0.001;
244 AddRemap(ent->targetShaderName, ent->targetShaderNewName, f);
245 trap_SetConfigstring(CS_SHADERSTATE, BuildShaderStateConfig());
246 }
247
248 if ( !ent->target ) {
249 return;
250 }
251
252 t = NULL;
253 while ( (t = G_Find (t, FOFS(targetname), ent->target)) != NULL ) {
254 if ( t == ent ) {
255 G_Printf ("WARNING: Entity used itself.\n");
256 } else {
257 if ( t->use ) {
258 t->use (t, ent, activator);
259 }
260 }
261 if ( !ent->inuse ) {
262 G_Printf("entity was removed while using targets\n");
263 return;
264 }
265 }
266 }
267
268
269 /*
270 =============
271 TempVector
272
273 This is just a convenience function
274 for making temporary vectors for function calls
275 =============
276 */
277 float *tv( float x, float y, float z ) {
278 static int index;
279 static vec3_t vecs[8];
280 float *v;
281
282 // use an array so that multiple tempvectors won't collide
283 // for a while
284 v = vecs[index];
285 index = (index + 1)&7;
286
287 v[0] = x;
288 v[1] = y;
289 v[2] = z;
290
291 return v;
292 }
293
294
295 /*
296 =============
297 VectorToString
298
299 This is just a convenience function
300 for printing vectors
301 =============
302 */
303 char *vtos( const vec3_t v ) {
304 static int index;
305 static char str[8][32];
306 char *s;
307
308 // use an array so that multiple vtos won't collide
309 s = str[index];
310 index = (index + 1)&7;
311
312 Com_sprintf (s, 32, "(%i %i %i)", (int)v[0], (int)v[1], (int)v[2]);
313
314 return s;
315 }
316
317
318 /*
319 ===============
320 G_SetMovedir
321
322 The editor only specifies a single value for angles (yaw),
323 but we have special constants to generate an up or down direction.
324 Angles will be cleared, because it is being used to represent a direction
325 instead of an orientation.
326 ===============
327 */
328 void G_SetMovedir( vec3_t angles, vec3_t movedir ) {
329 static vec3_t VEC_UP = {0, -1, 0};
330 static vec3_t MOVEDIR_UP = {0, 0, 1};
331 static vec3_t VEC_DOWN = {0, -2, 0};
332 static vec3_t MOVEDIR_DOWN = {0, 0, -1};
333
334 if ( VectorCompare (angles, VEC_UP) ) {
335 VectorCopy (MOVEDIR_UP, movedir);
336 } else if ( VectorCompare (angles, VEC_DOWN) ) {
337 VectorCopy (MOVEDIR_DOWN, movedir);
338 } else {
339 AngleVectors (angles, movedir, NULL, NULL);
340 }
341 VectorClear( angles );
342 }
343
344
345 float vectoyaw( const vec3_t vec ) {
346 float yaw;
347
348 if (vec[YAW] == 0 && vec[PITCH] == 0) {
349 yaw = 0;
350 } else {
351 if (vec[PITCH]) {
352 yaw = ( atan2( vec[YAW], vec[PITCH]) * 180 / M_PI );
353 } else if (vec[YAW] > 0) {
354 yaw = 90;
355 } else {
356 yaw = 270;
357 }
358 if (yaw < 0) {
359 yaw += 360;
360 }
361 }
362
363 return yaw;
364 }
365
366
367 void G_InitGentity( gentity_t *e ) {
368 e->inuse = qtrue;
369 e->classname = "noclass";
370 e->s.number = e - g_entities;
371 e->r.ownerNum = ENTITYNUM_NONE;
372 }
373
374 /*
375 =================
376 G_Spawn
377
378 Either finds a free entity, or allocates a new one.
379
380 The slots from 0 to MAX_CLIENTS-1 are always reserved for clients, and will
381 never be used by anything else.
382
383 Try to avoid reusing an entity that was recently freed, because it
384 can cause the client to think the entity morphed into something else
385 instead of being removed and recreated, which can cause interpolated
386 angles and bad trails.
387 =================
388 */
389 gentity_t *G_Spawn( void ) {
390 int i, force;
391 gentity_t *e;
392
393 e = NULL; // shut up warning
394 for ( force = 0 ; force < 2 ; force++ ) {
395 // if we go through all entities and can't find one to free,
396 // override the normal minimum times before use
397 e = &g_entities[MAX_CLIENTS];
398 for ( i = MAX_CLIENTS ; i<level.num_entities ; i++, e++) {
399 if ( e->inuse ) {
400 continue;
401 }
402
403 // the first couple seconds of server time can involve a lot of
404 // freeing and allocating, so relax the replacement policy
405 if ( !force && e->freetime > level.startTime + 2000 && level.time - e->freetime < 1000 ) {
406 continue;
407 }
408
409 // reuse this slot
410 G_InitGentity( e );
411 return e;
412 }
413 if ( level.num_entities < ENTITYNUM_MAX_NORMAL ) {
414 break;
415 }
416 }
417 if ( level.num_entities == ENTITYNUM_MAX_NORMAL ) {
418 for (i = 0; i < MAX_GENTITIES; i++) {
419 G_Printf("%4i: %s\n", i, g_entities[i].classname);
420 }
421 G_Error( "G_Spawn: no free entities" );
422 }
423
424 // open up a new slot
425 level.num_entities++;
426
427 // let the server system know that there are more entities
428 trap_LocateGameData( level.gentities, level.num_entities, sizeof( gentity_t ),
429 &level.clients[0].ps, sizeof( level.clients[0] ) );
430
431 G_InitGentity( e );
432 return e;
433 }
434
435 /*
436 =================
437 G_EntitiesFree
438 =================
439 */
440 qboolean G_EntitiesFree( void ) {
441 int i;
442 gentity_t *e;
443
444 if ( level.num_entities < ENTITYNUM_MAX_NORMAL ) {
445 // can open a new slot if needed
446 return qtrue;
447 }
448
449 e = &g_entities[MAX_CLIENTS];
450 for ( i = MAX_CLIENTS; i < level.num_entities; i++, e++) {
451 if ( e->inuse ) {
452 continue;
453 }
454 // slot available
455 return qtrue;
456 }
457 return qfalse;
458 }
459
460
461 /*
462 =================
463 G_FreeEntity
464
465 Marks the entity as free
466 =================
467 */
468 void G_FreeEntity( gentity_t *ed ) {
469 trap_UnlinkEntity (ed); // unlink from world
470
471 if ( ed->neverFree ) {
472 return;
473 }
474
475 memset (ed, 0, sizeof(*ed));
476 ed->classname = "freed";
477 ed->freetime = level.time;
478 ed->inuse = qfalse;
479 }
480
481 /*
482 =================
483 G_TempEntity
484
485 Spawns an event entity that will be auto-removed
486 The origin will be snapped to save net bandwidth, so care
487 must be taken if the origin is right on a surface (snap towards start vector first)
488 =================
489 */
490 gentity_t *G_TempEntity( vec3_t origin, int event ) {
491 gentity_t *e;
492 vec3_t snapped;
493
494 e = G_Spawn();
495 e->s.eType = ET_EVENTS + event;
496
497 e->classname = "tempEntity";
498 e->eventTime = level.time;
499 e->freeAfterEvent = qtrue;
500
501 VectorCopy( origin, snapped );
502 SnapVector( snapped ); // save network bandwidth
503 G_SetOrigin( e, snapped );
504
505 // find cluster for PVS
506 trap_LinkEntity( e );
507
508 return e;
509 }
510
511
512
513 /*
514 ==============================================================================
515
516 Kill box
517
518 ==============================================================================
519 */
520
521 /*
522 =================
523 G_KillBox
524
525 Kills all entities that would touch the proposed new positioning
526 of ent. Ent should be unlinked before calling this!
527 =================
528 */
529 void G_KillBox (gentity_t *ent) {
530 int i, num;
531 int touch[MAX_GENTITIES];
532 gentity_t *hit;
533 vec3_t mins, maxs;
534
535 VectorAdd( ent->client->ps.origin, ent->r.mins, mins );
536 VectorAdd( ent->client->ps.origin, ent->r.maxs, maxs );
537 num = trap_EntitiesInBox( mins, maxs, touch, MAX_GENTITIES );
538
539 for (i=0 ; i<num ; i++) {
540 hit = &g_entities[touch[i]];
541 if ( !hit->client ) {
542 continue;
543 }
544
545 // nail it
546 G_Damage ( hit, ent, ent, NULL, NULL,
547 100000, DAMAGE_NO_PROTECTION, MOD_TELEFRAG);
548 }
549
550 }
551
552 //==============================================================================
553
554 /*
555 ===============
556 G_AddPredictableEvent
557
558 Use for non-pmove events that would also be predicted on the
559 client side: jumppads and item pickups
560 Adds an event+parm and twiddles the event counter
561 ===============
562 */
563 void G_AddPredictableEvent( gentity_t *ent, int event, int eventParm ) {
564 if ( !ent->client ) {
565 return;
566 }
567 BG_AddPredictableEventToPlayerstate( event, eventParm, &ent->client->ps );
568 }
569
570
571 /*
572 ===============
573 G_AddEvent
574
575 Adds an event+parm and twiddles the event counter
576 ===============
577 */
578 void G_AddEvent( gentity_t *ent, int event, int eventParm ) {
579 int bits;
580
581 if ( !event ) {
582 G_Printf( "G_AddEvent: zero event added for entity %i\n", ent->s.number );
583 return;
584 }
585
586 // clients need to add the event in playerState_t instead of entityState_t
587 if ( ent->client ) {
588 bits = ent->client->ps.externalEvent & EV_EVENT_BITS;
589 bits = ( bits + EV_EVENT_BIT1 ) & EV_EVENT_BITS;
590 ent->client->ps.externalEvent = event | bits;
591 ent->client->ps.externalEventParm = eventParm;
592 ent->client->ps.externalEventTime = level.time;
593 } else {
594 bits = ent->s.event & EV_EVENT_BITS;
595 bits = ( bits + EV_EVENT_BIT1 ) & EV_EVENT_BITS;
596 ent->s.event = event | bits;
597 ent->s.eventParm = eventParm;
598 }
599 ent->eventTime = level.time;
600 }
601
602
603 /*
604 =============
605 G_Sound
606 =============
607 */
608 void G_Sound( gentity_t *ent, int channel, int soundIndex ) {
609 gentity_t *te;
610
611 te = G_TempEntity( ent->r.currentOrigin, EV_GENERAL_SOUND );
612 te->s.eventParm = soundIndex;
613 }
614
615
616 //==============================================================================
617
618
619 /*
620 ================
621 G_SetOrigin
622
623 Sets the pos trajectory for a fixed position
624 ================
625 */
626 void G_SetOrigin( gentity_t *ent, vec3_t origin ) {
627 VectorCopy( origin, ent->s.pos.trBase );
628 ent->s.pos.trType = TR_STATIONARY;
629 ent->s.pos.trTime = 0;
630 ent->s.pos.trDuration = 0;
631 VectorClear( ent->s.pos.trDelta );
632
633 VectorCopy( origin, ent->r.currentOrigin );
634 }
635
636 /*
637 ================
638 DebugLine
639
640 debug polygons only work when running a local game
641 with r_debugSurface set to 2
642 ================
643 */
644 int DebugLine(vec3_t start, vec3_t end, int color) {
645 vec3_t points[4], dir, cross, up = {0, 0, 1};
646 float dot;
647
648 VectorCopy(start, points[0]);
649 VectorCopy(start, points[1]);
650 //points[1][2] -= 2;
651 VectorCopy(end, points[2]);
652 //points[2][2] -= 2;
653 VectorCopy(end, points[3]);
654
655
656 VectorSubtract(end, start, dir);
657 VectorNormalize(dir);
658 dot = DotProduct(dir, up);
659 if (dot > 0.99 || dot < -0.99) VectorSet(cross, 1, 0, 0);
660 else CrossProduct(dir, up, cross);
661
662 VectorNormalize(cross);
663
664 VectorMA(points[0], 2, cross, points[0]);
665 VectorMA(points[1], -2, cross, points[1]);
666 VectorMA(points[2], -2, cross, points[2]);
667 VectorMA(points[3], 2, cross, points[3]);
668
669 return trap_DebugPolygonCreate(color, 4, points);
670 }