// Copyright (C) 1999-2000 Id Software, Inc. // // g_utils.c -- misc utility functions for game module #include "g_local.h" typedef struct { char oldShader[MAX_QPATH]; char newShader[MAX_QPATH]; float timeOffset; } shaderRemap_t; #define MAX_SHADER_REMAPS 128 int remapCount = 0; shaderRemap_t remappedShaders[MAX_SHADER_REMAPS]; void AddRemap(const char *oldShader, const char *newShader, float timeOffset) { int i; for (i = 0; i < remapCount; i++) { if (Q_stricmp(oldShader, remappedShaders[i].oldShader) == 0) { // found it, just update this one strcpy(remappedShaders[i].newShader,newShader); remappedShaders[i].timeOffset = timeOffset; return; } } if (remapCount < MAX_SHADER_REMAPS) { strcpy(remappedShaders[remapCount].newShader,newShader); strcpy(remappedShaders[remapCount].oldShader,oldShader); remappedShaders[remapCount].timeOffset = timeOffset; remapCount++; } } const char *BuildShaderStateConfig(void) { static char buff[MAX_STRING_CHARS*4]; char out[(MAX_QPATH * 2) + 5]; int i; memset(buff, 0, MAX_STRING_CHARS); for (i = 0; i < remapCount; i++) { Com_sprintf(out, (MAX_QPATH * 2) + 5, "%s=%s:%5.2f@", remappedShaders[i].oldShader, remappedShaders[i].newShader, remappedShaders[i].timeOffset); Q_strcat( buff, sizeof( buff ), out); } return buff; } /* ========================================================================= model / sound configstring indexes ========================================================================= */ /* ================ G_FindConfigstringIndex ================ */ int G_FindConfigstringIndex( char *name, int start, int max, qboolean create ) { int i; char s[MAX_STRING_CHARS]; if ( !name || !name[0] ) { return 0; } for ( i=1 ; iinuse) continue; s = *(char **) ((byte *)from + fieldofs); if (!s) continue; if (!Q_stricmp (s, match)) return from; } return NULL; } /* ============ G_RadiusList - given an origin and a radius, return all entities that are in use that are within the list ============ */ int G_RadiusList ( vec3_t origin, float radius, gentity_t *ignore, qboolean takeDamage, gentity_t *ent_list[MAX_GENTITIES]) { float dist; gentity_t *ent; int entityList[MAX_GENTITIES]; int numListedEntities; vec3_t mins, maxs; vec3_t v; int i, e; int ent_count = 0; if ( radius < 1 ) { radius = 1; } for ( i = 0 ; i < 3 ; i++ ) { mins[i] = origin[i] - radius; maxs[i] = origin[i] + radius; } numListedEntities = trap_EntitiesInBox( mins, maxs, entityList, MAX_GENTITIES ); for ( e = 0 ; e < numListedEntities ; e++ ) { ent = &g_entities[entityList[ e ]]; if ((ent == ignore) || !(ent->inuse) || ent->takedamage != takeDamage) continue; // find the distance from the edge of the bounding box for ( i = 0 ; i < 3 ; i++ ) { if ( origin[i] < ent->r.absmin[i] ) { v[i] = ent->r.absmin[i] - origin[i]; } else if ( origin[i] > ent->r.absmax[i] ) { v[i] = origin[i] - ent->r.absmax[i]; } else { v[i] = 0; } } dist = VectorLength( v ); if ( dist >= radius ) { continue; } // ok, we are within the radius, add us to the incoming list ent_list[ent_count] = ent; ent_count++; } // we are done, return how many we found return(ent_count); } /* ============= G_PickTarget Selects a random entity from among the targets ============= */ #define MAXCHOICES 32 gentity_t *G_PickTarget (char *targetname) { gentity_t *ent = NULL; int num_choices = 0; gentity_t *choice[MAXCHOICES]; if (!targetname) { G_Printf("G_PickTarget called with NULL targetname\n"); return NULL; } while(1) { ent = G_Find (ent, FOFS(targetname), targetname); if (!ent) break; choice[num_choices++] = ent; if (num_choices == MAXCHOICES) break; } if (!num_choices) { G_Printf("G_PickTarget: target %s not found\n", targetname); return NULL; } return choice[rand() % num_choices]; } /* ============================== G_UseTargets "activator" should be set to the entity that initiated the firing. Search for (string)targetname in all entities that match (string)self.target and call their .use function ============================== */ void G_UseTargets( gentity_t *ent, gentity_t *activator ) { gentity_t *t; if ( !ent ) { return; } if (ent->targetShaderName && ent->targetShaderNewName) { float f = level.time * 0.001; AddRemap(ent->targetShaderName, ent->targetShaderNewName, f); trap_SetConfigstring(CS_SHADERSTATE, BuildShaderStateConfig()); } if ( !ent->target ) { return; } t = NULL; while ( (t = G_Find (t, FOFS(targetname), ent->target)) != NULL ) { if ( t == ent ) { G_Printf ("WARNING: Entity used itself.\n"); } else { if ( t->use ) { t->use (t, ent, activator); } } if ( !ent->inuse ) { G_Printf("entity was removed while using targets\n"); return; } } } /* ============= TempVector This is just a convenience function for making temporary vectors for function calls ============= */ float *tv( float x, float y, float z ) { static int index; static vec3_t vecs[8]; float *v; // use an array so that multiple tempvectors won't collide // for a while v = vecs[index]; index = (index + 1)&7; v[0] = x; v[1] = y; v[2] = z; return v; } /* ============= VectorToString This is just a convenience function for printing vectors ============= */ char *vtos( const vec3_t v ) { static int index; static char str[8][32]; char *s; // use an array so that multiple vtos won't collide s = str[index]; index = (index + 1)&7; Com_sprintf (s, 32, "(%i %i %i)", (int)v[0], (int)v[1], (int)v[2]); return s; } /* =============== G_SetMovedir The editor only specifies a single value for angles (yaw), but we have special constants to generate an up or down direction. Angles will be cleared, because it is being used to represent a direction instead of an orientation. =============== */ void G_SetMovedir( vec3_t angles, vec3_t movedir ) { static vec3_t VEC_UP = {0, -1, 0}; static vec3_t MOVEDIR_UP = {0, 0, 1}; static vec3_t VEC_DOWN = {0, -2, 0}; static vec3_t MOVEDIR_DOWN = {0, 0, -1}; if ( VectorCompare (angles, VEC_UP) ) { VectorCopy (MOVEDIR_UP, movedir); } else if ( VectorCompare (angles, VEC_DOWN) ) { VectorCopy (MOVEDIR_DOWN, movedir); } else { AngleVectors (angles, movedir, NULL, NULL); } VectorClear( angles ); } void G_InitGentity( gentity_t *e ) { e->inuse = qtrue; e->classname = "noclass"; e->s.number = e - g_entities; e->r.ownerNum = ENTITYNUM_NONE; e->s.modelGhoul2 = 0; //assume not } /* ================= G_Spawn Either finds a free entity, or allocates a new one. The slots from 0 to MAX_CLIENTS-1 are always reserved for clients, and will never be used by anything else. Try to avoid reusing an entity that was recently freed, because it can cause the client to think the entity morphed into something else instead of being removed and recreated, which can cause interpolated angles and bad trails. ================= */ gentity_t *G_Spawn( void ) { int i, force; gentity_t *e; e = NULL; // shut up warning i = 0; // shut up warning for ( force = 0 ; force < 2 ; force++ ) { // if we go through all entities and can't find one to free, // override the normal minimum times before use e = &g_entities[MAX_CLIENTS]; for ( i = MAX_CLIENTS ; iinuse ) { continue; } // the first couple seconds of server time can involve a lot of // freeing and allocating, so relax the replacement policy if ( !force && e->freetime > level.startTime + 2000 && level.time - e->freetime < 1000 ) { continue; } // reuse this slot G_InitGentity( e ); return e; } if ( i != MAX_GENTITIES ) { break; } } if ( i == ENTITYNUM_MAX_NORMAL ) { for (i = 0; i < MAX_GENTITIES; i++) { G_Printf("%4i: %s\n", i, g_entities[i].classname); } G_Error( "G_Spawn: no free entities" ); } // open up a new slot level.num_entities++; // let the server system know that there are more entities trap_LocateGameData( level.gentities, level.num_entities, sizeof( gentity_t ), &level.clients[0].ps, sizeof( level.clients[0] ) ); G_InitGentity( e ); return e; } /* ================= G_EntitiesFree ================= */ qboolean G_EntitiesFree( void ) { int i; gentity_t *e; e = &g_entities[MAX_CLIENTS]; for ( i = MAX_CLIENTS; i < level.num_entities; i++, e++) { if ( e->inuse ) { continue; } // slot available return qtrue; } return qfalse; } #define MAX_G2_KILL_QUEUE 64 int gG2KillIndex[MAX_G2_KILL_QUEUE]; int gG2KillNum = 0; void G_SendG2KillQueue(void) { char g2KillString[1024]; int i = 0; if (!gG2KillNum) { return; } Com_sprintf(g2KillString, 1024, "kg2"); while (i < gG2KillNum) { Q_strcat(g2KillString, 1024, va(" %i", gG2KillIndex[i])); i++; } trap_SendServerCommand(-1, g2KillString); //Clear the count because we just sent off the whole queue gG2KillNum = 0; } void G_KillG2Queue(int entNum) { if (gG2KillNum >= MAX_G2_KILL_QUEUE) { //This would be considered a Bad Thing. #ifdef _DEBUG Com_Printf("WARNING: Exceeded the MAX_G2_KILL_QUEUE count for this frame!\n"); #endif //Since we're out of queue slots, just send it now as a seperate command (eats more bandwidth, but we have no choice) trap_SendServerCommand(-1, va("kg2 %i", entNum)); return; } gG2KillIndex[gG2KillNum] = entNum; gG2KillNum++; } /* ================= G_FreeEntity Marks the entity as free ================= */ void G_FreeEntity( gentity_t *ed ) { //gentity_t *te; if (ed->isSaberEntity) { #ifdef _DEBUG Com_Printf("Tried to remove JM saber!\n"); #endif return; } trap_UnlinkEntity (ed); // unlink from world if ( ed->neverFree ) { return; } //rww - this may seem a bit hackish, but unfortunately we have no access //to anything ghoul2-related on the server and thus must send a message //to let the client know he needs to clean up all the g2 stuff for this //now-removed entity if (ed->s.modelGhoul2) { //force all clients to accept an event to destroy this instance, right now /* te = G_TempEntity( vec3_origin, EV_DESTROY_GHOUL2_INSTANCE ); te->r.svFlags |= SVF_BROADCAST; te->s.eventParm = ed->s.number; */ //Or not. Events can be dropped, so that would be a bad thing. G_KillG2Queue(ed->s.number); } if (ed->s.eFlags & EF_SOUNDTRACKER) { int i = 0; gentity_t *ent; while (i < MAX_CLIENTS) { ent = &g_entities[i]; if (ent && ent->inuse && ent->client) { int ch = TRACK_CHANNEL_NONE-50; while (ch < NUM_TRACK_CHANNELS-50) { if (ent->client->ps.fd.killSoundEntIndex[ch] == ed->s.number) { ent->client->ps.fd.killSoundEntIndex[ch] = 0; } ch++; } } i++; } } memset (ed, 0, sizeof(*ed)); ed->classname = "freed"; ed->freetime = level.time; ed->inuse = qfalse; } /* ================= G_TempEntity Spawns an event entity that will be auto-removed The origin will be snapped to save net bandwidth, so care must be taken if the origin is right on a surface (snap towards start vector first) ================= */ gentity_t *G_TempEntity( vec3_t origin, int event ) { gentity_t *e; vec3_t snapped; e = G_Spawn(); e->s.eType = ET_EVENTS + event; e->classname = "tempEntity"; e->eventTime = level.time; e->freeAfterEvent = qtrue; VectorCopy( origin, snapped ); SnapVector( snapped ); // save network bandwidth G_SetOrigin( e, snapped ); // find cluster for PVS trap_LinkEntity( e ); return e; } gentity_t *GetTrackerEnt(int channel) { int i = MAX_CLIENTS; gentity_t *ent; while (i < MAX_GENTITIES) { ent = &g_entities[i]; if (ent && (ent->s.eFlags & EF_SOUNDTRACKER) && ent->s.saberEntityNum == channel) { return ent; } i++; } return NULL; } /* ================= G_SoundTempEntity Special event entity that keeps sound trackers in mind ================= */ gentity_t *G_SoundTempEntity( vec3_t origin, int event, int channel ) { gentity_t *e; vec3_t snapped; e = G_Spawn(); e->s.eType = ET_EVENTS + event; e->inuse = qtrue; e->classname = "tempEntity"; e->eventTime = level.time; e->freeAfterEvent = qtrue; VectorCopy( origin, snapped ); SnapVector( snapped ); // save network bandwidth G_SetOrigin( e, snapped ); // find cluster for PVS trap_LinkEntity( e ); return e; } /* ============================================================================== Kill box ============================================================================== */ /* ================= G_KillBox Kills all entities that would touch the proposed new positioning of ent. Ent should be unlinked before calling this! ================= */ void G_KillBox (gentity_t *ent) { int i, num; int touch[MAX_GENTITIES]; gentity_t *hit; vec3_t mins, maxs; VectorAdd( ent->client->ps.origin, ent->r.mins, mins ); VectorAdd( ent->client->ps.origin, ent->r.maxs, maxs ); num = trap_EntitiesInBox( mins, maxs, touch, MAX_GENTITIES ); for (i=0 ; iclient ) { continue; } // nail it G_Damage ( hit, ent, ent, NULL, NULL, 100000, DAMAGE_NO_PROTECTION, MOD_TELEFRAG); } } //============================================================================== /* =============== G_AddPredictableEvent Use for non-pmove events that would also be predicted on the client side: jumppads and item pickups Adds an event+parm and twiddles the event counter =============== */ void G_AddPredictableEvent( gentity_t *ent, int event, int eventParm ) { if ( !ent->client ) { return; } BG_AddPredictableEventToPlayerstate( event, eventParm, &ent->client->ps ); } /* =============== G_AddEvent Adds an event+parm and twiddles the event counter =============== */ void G_AddEvent( gentity_t *ent, int event, int eventParm ) { int bits; if ( !event ) { G_Printf( "G_AddEvent: zero event added for entity %i\n", ent->s.number ); return; } // clients need to add the event in playerState_t instead of entityState_t if ( ent->client ) { bits = ent->client->ps.externalEvent & EV_EVENT_BITS; bits = ( bits + EV_EVENT_BIT1 ) & EV_EVENT_BITS; ent->client->ps.externalEvent = event | bits; ent->client->ps.externalEventParm = eventParm; ent->client->ps.externalEventTime = level.time; } else { bits = ent->s.event & EV_EVENT_BITS; bits = ( bits + EV_EVENT_BIT1 ) & EV_EVENT_BITS; ent->s.event = event | bits; ent->s.eventParm = eventParm; } ent->eventTime = level.time; } /* ============= G_PlayEffect ============= */ gentity_t *G_PlayEffect(int fxID, vec3_t org, vec3_t ang) { gentity_t *te; te = G_TempEntity( org, EV_PLAY_EFFECT ); VectorCopy(ang, te->s.angles); VectorCopy(org, te->s.origin); te->s.eventParm = fxID; return te; } /* ============= G_ScreenShake ============= */ gentity_t *G_ScreenShake(vec3_t org, gentity_t *target, float intensity, int duration, qboolean global) { gentity_t *te; te = G_TempEntity( org, EV_SCREENSHAKE ); VectorCopy(org, te->s.origin); te->s.angles[0] = intensity; te->s.time = duration; if (target) { te->s.modelindex = target->s.number+1; } else { te->s.modelindex = 0; } if (global) { te->r.svFlags |= SVF_BROADCAST; } return te; } /* ============= G_MuteSound ============= */ void G_MuteSound( int entnum, int channel ) { gentity_t *te, *e; te = G_TempEntity( vec3_origin, EV_MUTE_SOUND ); te->r.svFlags = SVF_BROADCAST; te->s.trickedentindex2 = entnum; te->s.trickedentindex = channel; e = &g_entities[entnum]; if (e && (e->s.eFlags & EF_SOUNDTRACKER)) { G_FreeEntity(e); e->s.eFlags = 0; } } /* ============= G_Sound ============= */ void G_Sound( gentity_t *ent, int channel, int soundIndex ) { gentity_t *te; te = G_SoundTempEntity( ent->r.currentOrigin, EV_GENERAL_SOUND, channel ); te->s.eventParm = soundIndex; if (ent && ent->client && channel > TRACK_CHANNEL_NONE) { //let the client remember the index of the player entity so he can kill the most recent sound on request te->s.saberEntityNum = channel; if (g_entities[ent->client->ps.fd.killSoundEntIndex[channel-50]].inuse && ent->client->ps.fd.killSoundEntIndex[channel-50] > MAX_CLIENTS) { G_MuteSound(ent->client->ps.fd.killSoundEntIndex[channel-50], CHAN_VOICE); if (ent->client->ps.fd.killSoundEntIndex[channel-50] > MAX_CLIENTS && g_entities[ent->client->ps.fd.killSoundEntIndex[channel-50]].inuse) { G_FreeEntity(&g_entities[ent->client->ps.fd.killSoundEntIndex[channel-50]]); } ent->client->ps.fd.killSoundEntIndex[channel-50] = 0; } ent->client->ps.fd.killSoundEntIndex[channel-50] = te->s.number; te->s.trickedentindex = ent->s.number; te->s.eFlags = EF_SOUNDTRACKER; //te->freeAfterEvent = qfalse; } } /* ============= G_SoundAtLoc ============= */ void G_SoundAtLoc( vec3_t loc, int channel, int soundIndex ) { gentity_t *te; te = G_TempEntity( loc, EV_GENERAL_SOUND ); te->s.eventParm = soundIndex; } /* ============= G_EntitySound ============= */ void G_EntitySound( gentity_t *ent, int channel, int soundIndex ) { gentity_t *te; te = G_TempEntity( ent->r.currentOrigin, EV_ENTITY_SOUND ); te->s.eventParm = soundIndex; te->s.weapon = ent->s.number; te->s.trickedentindex = channel; } //============================================================================== /* ============== ValidUseTarget Returns whether or not the targeted entity is useable ============== */ qboolean ValidUseTarget( gentity_t *ent ) { if ( !ent->use ) { return qfalse; } if ( !(ent->r.svFlags & SVF_PLAYER_USABLE) ) {//Check for flag that denotes BUTTON_USE useability return qfalse; } return qtrue; } /* ============== TryUse Try and use an entity in the world, directly ahead of us ============== */ #define USE_DISTANCE 64.0f extern void Touch_Button(gentity_t *ent, gentity_t *other, trace_t *trace ); void TryUse( gentity_t *ent ) { gentity_t *target; trace_t trace; vec3_t src, dest, vf; vec3_t viewspot; VectorCopy(ent->client->ps.origin, viewspot); viewspot[2] += ent->client->ps.viewheight; VectorCopy( viewspot, src ); AngleVectors( ent->client->ps.viewangles, vf, NULL, NULL ); VectorMA( src, USE_DISTANCE, vf, dest ); //Trace ahead to find a valid target trap_Trace( &trace, src, vec3_origin, vec3_origin, dest, ent->s.number, MASK_OPAQUE|CONTENTS_SOLID|CONTENTS_BODY|CONTENTS_ITEM|CONTENTS_CORPSE ); if ( trace.fraction == 1.0f || trace.entityNum < 1 ) { return; } target = &g_entities[trace.entityNum]; //Check for a use command if ( ValidUseTarget( target ) ) { /* NPC_SetAnim( ent, SETANIM_TORSO, BOTH_FORCEPUSH, SETANIM_FLAG_OVERRIDE|SETANIM_FLAG_HOLD ); if ( !VectorLengthSquared( ent->client->ps.velocity ) ) { NPC_SetAnim( ent, SETANIM_LEGS, BOTH_FORCEPUSH, SETANIM_FLAG_NORMAL|SETANIM_FLAG_HOLD ); } */ if ( target->touch == Touch_Button ) {//pretend we touched it target->touch(target, ent, NULL); } else { target->use(target, ent, ent); } return; } } qboolean G_PointInBounds( vec3_t point, vec3_t mins, vec3_t maxs ) { int i; for(i = 0; i < 3; i++ ) { if ( point[i] < mins[i] ) { return qfalse; } if ( point[i] > maxs[i] ) { return qfalse; } } return qtrue; } qboolean G_BoxInBounds( vec3_t point, vec3_t mins, vec3_t maxs, vec3_t boundsMins, vec3_t boundsMaxs ) { vec3_t boxMins; vec3_t boxMaxs; VectorAdd( point, mins, boxMins ); VectorAdd( point, maxs, boxMaxs ); if(boxMaxs[0]>boundsMaxs[0]) return qfalse; if(boxMaxs[1]>boundsMaxs[1]) return qfalse; if(boxMaxs[2]>boundsMaxs[2]) return qfalse; if(boxMins[0]r.currentAngles ); VectorCopy( angles, ent->s.angles ); VectorCopy( angles, ent->s.apos.trBase ); } qboolean G_ClearTrace( vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end, int ignore, int clipmask ) { static trace_t tr; trap_Trace( &tr, start, mins, maxs, end, ignore, clipmask ); if ( tr.allsolid || tr.startsolid || tr.fraction < 1.0 ) { return qfalse; } return qtrue; } /* ================ G_SetOrigin Sets the pos trajectory for a fixed position ================ */ void G_SetOrigin( gentity_t *ent, vec3_t origin ) { VectorCopy( origin, ent->s.pos.trBase ); ent->s.pos.trType = TR_STATIONARY; ent->s.pos.trTime = 0; ent->s.pos.trDuration = 0; VectorClear( ent->s.pos.trDelta ); VectorCopy( origin, ent->r.currentOrigin ); } /* ================ DebugLine debug polygons only work when running a local game with r_debugSurface set to 2 ================ */ int DebugLine(vec3_t start, vec3_t end, int color) { vec3_t points[4], dir, cross, up = {0, 0, 1}; float dot; VectorCopy(start, points[0]); VectorCopy(start, points[1]); //points[1][2] -= 2; VectorCopy(end, points[2]); //points[2][2] -= 2; VectorCopy(end, points[3]); VectorSubtract(end, start, dir); VectorNormalize(dir); dot = DotProduct(dir, up); if (dot > 0.99 || dot < -0.99) VectorSet(cross, 1, 0, 0); else CrossProduct(dir, up, cross); VectorNormalize(cross); VectorMA(points[0], 2, cross, points[0]); VectorMA(points[1], -2, cross, points[1]); VectorMA(points[2], -2, cross, points[2]); VectorMA(points[3], 2, cross, points[3]); return trap_DebugPolygonCreate(color, 4, points); } void G_ROFF_NotetrackCallback( gentity_t *cent, const char *notetrack) { char type[256]; int i = 0; int addlArg = 0; if (!cent || !notetrack) { return; } while (notetrack[i] && notetrack[i] != ' ') { type[i] = notetrack[i]; i++; } type[i] = '\0'; if (!i || !type[0]) { return; } if (notetrack[i] == ' ') { addlArg = 1; } if (strcmp(type, "loop") == 0) { if (addlArg) //including an additional argument means reset to original position before loop { VectorCopy(cent->s.origin2, cent->s.pos.trBase); VectorCopy(cent->s.origin2, cent->r.currentOrigin); VectorCopy(cent->s.angles2, cent->s.apos.trBase); VectorCopy(cent->s.angles2, cent->r.currentAngles); } trap_ROFF_Play(cent->s.number, cent->roffid, qfalse); } }