2023-10-17 15:47:50 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) ZeniMax Media Inc.
|
|
|
|
* Licensed under the GNU General Public License 2.0.
|
|
|
|
*/
|
|
|
|
/* =======================================================================
|
|
|
|
*
|
|
|
|
* Misc. utility functions for the game logic.
|
|
|
|
*
|
|
|
|
* =======================================================================
|
|
|
|
*/
|
2023-10-16 21:39:22 +00:00
|
|
|
|
2023-10-17 15:47:50 +00:00
|
|
|
#include "header/local.h"
|
2023-10-16 21:39:22 +00:00
|
|
|
|
2023-10-17 15:47:50 +00:00
|
|
|
#define MAXCHOICES 8
|
2023-10-16 21:39:22 +00:00
|
|
|
|
2023-10-17 15:47:50 +00:00
|
|
|
void
|
|
|
|
G_ProjectSource(vec3_t point, vec3_t distance, vec3_t forward,
|
|
|
|
vec3_t right, vec3_t result)
|
2023-10-16 21:39:22 +00:00
|
|
|
{
|
|
|
|
result[0] = point[0] + forward[0] * distance[0] + right[0] * distance[1];
|
|
|
|
result[1] = point[1] + forward[1] * distance[0] + right[1] * distance[1];
|
|
|
|
result[2] = point[2] + forward[2] * distance[0] + right[2] * distance[1] + distance[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2023-10-17 15:47:50 +00:00
|
|
|
* Searches all active entities for the next one that holds
|
|
|
|
* the matching string at fieldofs (use the FOFS() macro) in the structure.
|
|
|
|
*
|
|
|
|
* Searches beginning at the edict after from, or the beginning if NULL
|
|
|
|
* NULL will be returned if the end of the list is reached.
|
|
|
|
*/
|
|
|
|
edict_t *
|
|
|
|
G_Find(edict_t *from, int fieldofs, char *match)
|
2023-10-16 21:39:22 +00:00
|
|
|
{
|
2023-10-17 15:47:50 +00:00
|
|
|
char *s;
|
2023-10-16 21:39:22 +00:00
|
|
|
|
|
|
|
if (!from)
|
2023-10-17 15:47:50 +00:00
|
|
|
{
|
2023-10-16 21:39:22 +00:00
|
|
|
from = g_edicts;
|
2023-10-17 15:47:50 +00:00
|
|
|
}
|
2023-10-16 21:39:22 +00:00
|
|
|
else
|
2023-10-17 15:47:50 +00:00
|
|
|
{
|
2023-10-16 21:39:22 +00:00
|
|
|
from++;
|
2023-10-17 15:47:50 +00:00
|
|
|
}
|
2023-10-16 21:39:22 +00:00
|
|
|
|
2023-10-17 15:47:50 +00:00
|
|
|
for ( ; from < &g_edicts[globals.num_edicts]; from++)
|
2023-10-16 21:39:22 +00:00
|
|
|
{
|
|
|
|
if (!from->inuse)
|
2023-10-17 15:47:50 +00:00
|
|
|
{
|
2023-10-16 21:39:22 +00:00
|
|
|
continue;
|
2023-10-17 15:47:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s = *(char **)((byte *)from + fieldofs);
|
|
|
|
|
2023-10-16 21:39:22 +00:00
|
|
|
if (!s)
|
2023-10-17 15:47:50 +00:00
|
|
|
{
|
2023-10-16 21:39:22 +00:00
|
|
|
continue;
|
2023-10-17 15:47:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!Q_stricmp(s, match))
|
|
|
|
{
|
2023-10-16 21:39:22 +00:00
|
|
|
return from;
|
2023-10-17 15:47:50 +00:00
|
|
|
}
|
2023-10-16 21:39:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2023-10-17 15:47:50 +00:00
|
|
|
* Returns entities that have origins within a spherical area
|
|
|
|
*/
|
|
|
|
edict_t *
|
|
|
|
findradius(edict_t *from, vec3_t org, float rad)
|
2023-10-16 21:39:22 +00:00
|
|
|
{
|
2023-10-17 15:47:50 +00:00
|
|
|
vec3_t eorg;
|
|
|
|
int j;
|
2023-10-16 21:39:22 +00:00
|
|
|
|
|
|
|
if (!from)
|
2023-10-17 15:47:50 +00:00
|
|
|
{
|
2023-10-16 21:39:22 +00:00
|
|
|
from = g_edicts;
|
2023-10-17 15:47:50 +00:00
|
|
|
}
|
2023-10-16 21:39:22 +00:00
|
|
|
else
|
2023-10-17 15:47:50 +00:00
|
|
|
{
|
2023-10-16 21:39:22 +00:00
|
|
|
from++;
|
2023-10-17 15:47:50 +00:00
|
|
|
}
|
|
|
|
|
2023-10-16 21:39:22 +00:00
|
|
|
for ( ; from < &g_edicts[globals.num_edicts]; from++)
|
|
|
|
{
|
|
|
|
if (!from->inuse)
|
2023-10-17 15:47:50 +00:00
|
|
|
{
|
2023-10-16 21:39:22 +00:00
|
|
|
continue;
|
2023-10-17 15:47:50 +00:00
|
|
|
}
|
|
|
|
|
2023-10-16 21:39:22 +00:00
|
|
|
if (from->solid == SOLID_NOT)
|
2023-10-17 15:47:50 +00:00
|
|
|
{
|
2023-10-16 21:39:22 +00:00
|
|
|
continue;
|
2023-10-17 15:47:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (j = 0; j < 3; j++)
|
|
|
|
{
|
|
|
|
eorg[j] = org[j] - (from->s.origin[j] +
|
|
|
|
(from->mins[j] + from->maxs[j]) * 0.5);
|
|
|
|
}
|
|
|
|
|
2023-10-16 21:39:22 +00:00
|
|
|
if (VectorLength(eorg) > rad)
|
2023-10-17 15:47:50 +00:00
|
|
|
{
|
2023-10-16 21:39:22 +00:00
|
|
|
continue;
|
2023-10-17 15:47:50 +00:00
|
|
|
}
|
|
|
|
|
2023-10-16 21:39:22 +00:00
|
|
|
return from;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2023-10-17 15:47:50 +00:00
|
|
|
* Searches all active entities for the next one that holds
|
|
|
|
* the matching string at fieldofs (use the FOFS() macro) in the structure.
|
|
|
|
*
|
|
|
|
* Searches beginning at the edict after from, or the beginning if NULL
|
|
|
|
* NULL will be returned if the end of the list is reached.
|
|
|
|
*/
|
|
|
|
edict_t *
|
|
|
|
G_PickTarget(char *targetname)
|
2023-10-16 21:39:22 +00:00
|
|
|
{
|
2023-10-17 15:47:50 +00:00
|
|
|
edict_t *ent = NULL;
|
|
|
|
int num_choices = 0;
|
|
|
|
edict_t *choice[MAXCHOICES];
|
2023-10-16 21:39:22 +00:00
|
|
|
|
|
|
|
if (!targetname)
|
|
|
|
{
|
|
|
|
gi.dprintf("G_PickTarget called with NULL targetname\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-10-17 15:47:50 +00:00
|
|
|
while (1)
|
2023-10-16 21:39:22 +00:00
|
|
|
{
|
2023-10-17 15:47:50 +00:00
|
|
|
ent = G_Find(ent, FOFS(targetname), targetname);
|
|
|
|
|
2023-10-16 21:39:22 +00:00
|
|
|
if (!ent)
|
2023-10-17 15:47:50 +00:00
|
|
|
{
|
2023-10-16 21:39:22 +00:00
|
|
|
break;
|
2023-10-17 15:47:50 +00:00
|
|
|
}
|
|
|
|
|
2023-10-16 21:39:22 +00:00
|
|
|
choice[num_choices++] = ent;
|
2023-10-17 15:47:50 +00:00
|
|
|
|
2023-10-16 21:39:22 +00:00
|
|
|
if (num_choices == MAXCHOICES)
|
2023-10-17 15:47:50 +00:00
|
|
|
{
|
2023-10-16 21:39:22 +00:00
|
|
|
break;
|
2023-10-17 15:47:50 +00:00
|
|
|
}
|
2023-10-16 21:39:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!num_choices)
|
|
|
|
{
|
|
|
|
gi.dprintf("G_PickTarget: target %s not found\n", targetname);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return choice[rand() % num_choices];
|
|
|
|
}
|
|
|
|
|
2023-10-17 15:47:50 +00:00
|
|
|
void
|
|
|
|
Think_Delay(edict_t *ent)
|
2023-10-16 21:39:22 +00:00
|
|
|
{
|
2023-10-17 15:47:50 +00:00
|
|
|
if (!ent)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
G_UseTargets(ent, ent->activator);
|
|
|
|
G_FreeEdict(ent);
|
2023-10-16 21:39:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2023-10-17 15:47:50 +00:00
|
|
|
* the global "activator" should be set to the entity that initiated the firing.
|
|
|
|
*
|
|
|
|
* If self.delay is set, a DelayedUse entity will be created that will actually
|
|
|
|
* do the SUB_UseTargets after that many seconds have passed.
|
|
|
|
*
|
|
|
|
* Centerprints any self.message to the activator.
|
|
|
|
*
|
|
|
|
* Search for (string)targetname in all entities that
|
|
|
|
* match (string)self.target and call their .use function
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
G_UseTargets(edict_t *ent, edict_t *activator /* may be NULL */)
|
2023-10-16 21:39:22 +00:00
|
|
|
{
|
2023-10-17 15:47:50 +00:00
|
|
|
edict_t *t;
|
|
|
|
|
|
|
|
if (!ent)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2023-10-16 21:39:22 +00:00
|
|
|
|
2023-10-17 15:47:50 +00:00
|
|
|
/* check for a delay */
|
2023-10-16 21:39:22 +00:00
|
|
|
if (ent->delay)
|
|
|
|
{
|
2023-10-17 15:47:50 +00:00
|
|
|
/* create a temp object to fire at a later time */
|
2023-10-16 21:39:22 +00:00
|
|
|
t = G_Spawn();
|
|
|
|
t->classname = "DelayedUse";
|
|
|
|
t->nextthink = level.time + ent->delay;
|
|
|
|
t->think = Think_Delay;
|
|
|
|
t->activator = activator;
|
2023-10-17 15:47:50 +00:00
|
|
|
|
2023-10-16 21:39:22 +00:00
|
|
|
if (!activator)
|
2023-10-17 15:47:50 +00:00
|
|
|
{
|
|
|
|
gi.dprintf("Think_Delay with no activator\n");
|
|
|
|
}
|
|
|
|
|
2023-10-16 21:39:22 +00:00
|
|
|
t->message = ent->message;
|
|
|
|
t->target = ent->target;
|
|
|
|
t->killtarget = ent->killtarget;
|
|
|
|
return;
|
|
|
|
}
|
2023-10-17 15:47:50 +00:00
|
|
|
|
|
|
|
/* print the message */
|
|
|
|
if (activator && (ent->message) && !(activator->svflags & SVF_MONSTER))
|
2023-10-16 21:39:22 +00:00
|
|
|
{
|
2023-10-17 15:47:50 +00:00
|
|
|
gi.centerprintf(activator, "%s", ent->message);
|
|
|
|
|
2023-10-16 21:39:22 +00:00
|
|
|
if (ent->noise_index)
|
2023-10-17 15:47:50 +00:00
|
|
|
{
|
|
|
|
gi.sound(activator, CHAN_AUTO, ent->noise_index, 1, ATTN_NORM, 0);
|
|
|
|
}
|
2023-10-16 21:39:22 +00:00
|
|
|
else
|
2023-10-17 15:47:50 +00:00
|
|
|
{
|
|
|
|
gi.sound(activator, CHAN_AUTO, gi.soundindex("misc/talk1.wav"),
|
|
|
|
1, ATTN_NORM, 0);
|
|
|
|
}
|
2023-10-16 21:39:22 +00:00
|
|
|
}
|
|
|
|
|
2023-10-17 15:47:50 +00:00
|
|
|
/* kill killtargets */
|
2023-10-16 21:39:22 +00:00
|
|
|
if (ent->killtarget)
|
|
|
|
{
|
|
|
|
t = NULL;
|
2023-10-17 15:47:50 +00:00
|
|
|
|
|
|
|
while ((t = G_Find(t, FOFS(targetname), ent->killtarget)))
|
2023-10-16 21:39:22 +00:00
|
|
|
{
|
2023-10-17 15:47:50 +00:00
|
|
|
G_FreeEdict(t);
|
|
|
|
|
2023-10-16 21:39:22 +00:00
|
|
|
if (!ent->inuse)
|
|
|
|
{
|
|
|
|
gi.dprintf("entity was removed while using killtargets\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-17 15:47:50 +00:00
|
|
|
/* fire targets */
|
2023-10-16 21:39:22 +00:00
|
|
|
if (ent->target)
|
|
|
|
{
|
|
|
|
t = NULL;
|
2023-10-17 15:47:50 +00:00
|
|
|
|
|
|
|
while ((t = G_Find(t, FOFS(targetname), ent->target)))
|
2023-10-16 21:39:22 +00:00
|
|
|
{
|
2023-10-17 15:47:50 +00:00
|
|
|
/* doors fire area portals in a specific way */
|
2023-10-16 21:39:22 +00:00
|
|
|
if (!Q_stricmp(t->classname, "func_areaportal") &&
|
2023-10-17 15:47:50 +00:00
|
|
|
(!Q_stricmp(ent->classname, "func_door") ||
|
|
|
|
!Q_stricmp(ent->classname, "func_door_rotating")))
|
|
|
|
{
|
2023-10-16 21:39:22 +00:00
|
|
|
continue;
|
2023-10-17 15:47:50 +00:00
|
|
|
}
|
2023-10-16 21:39:22 +00:00
|
|
|
|
|
|
|
if (t == ent)
|
|
|
|
{
|
2023-10-17 15:47:50 +00:00
|
|
|
gi.dprintf("WARNING: Entity used itself.\n");
|
2023-10-16 21:39:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (t->use)
|
2023-10-17 15:47:50 +00:00
|
|
|
{
|
|
|
|
t->use(t, ent, activator);
|
|
|
|
}
|
2023-10-16 21:39:22 +00:00
|
|
|
}
|
2023-10-17 15:47:50 +00:00
|
|
|
|
2023-10-16 21:39:22 +00:00
|
|
|
if (!ent->inuse)
|
|
|
|
{
|
|
|
|
gi.dprintf("entity was removed while using targets\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2023-10-17 15:47:50 +00:00
|
|
|
* This is just a convenience function
|
|
|
|
* for making temporary vectors for function calls
|
|
|
|
*/
|
|
|
|
float *
|
|
|
|
tv(float x, float y, float z)
|
2023-10-16 21:39:22 +00:00
|
|
|
{
|
2023-10-17 15:47:50 +00:00
|
|
|
static int index;
|
|
|
|
static vec3_t vecs[8];
|
|
|
|
float *v;
|
2023-10-16 21:39:22 +00:00
|
|
|
|
2023-10-17 15:47:50 +00:00
|
|
|
/* use an array so that multiple tempvectors
|
|
|
|
won't collide for a while */
|
2023-10-16 21:39:22 +00:00
|
|
|
v = vecs[index];
|
2023-10-17 15:47:50 +00:00
|
|
|
index = (index + 1) & 7;
|
2023-10-16 21:39:22 +00:00
|
|
|
|
|
|
|
v[0] = x;
|
|
|
|
v[1] = y;
|
|
|
|
v[2] = z;
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2023-10-17 15:47:50 +00:00
|
|
|
* This is just a convenience function
|
|
|
|
* for printing vectors
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
vtos(vec3_t v)
|
2023-10-16 21:39:22 +00:00
|
|
|
{
|
2023-10-17 15:47:50 +00:00
|
|
|
static int index;
|
|
|
|
static char str[8][32];
|
|
|
|
char *s;
|
2023-10-16 21:39:22 +00:00
|
|
|
|
2023-10-17 15:47:50 +00:00
|
|
|
/* use an array so that multiple vtos won't collide */
|
2023-10-16 21:39:22 +00:00
|
|
|
s = str[index];
|
2023-10-17 15:47:50 +00:00
|
|
|
index = (index + 1) & 7;
|
2023-10-16 21:39:22 +00:00
|
|
|
|
2023-10-17 15:47:50 +00:00
|
|
|
Com_sprintf(s, 32, "(%i %i %i)", (int)v[0], (int)v[1], (int)v[2]);
|
2023-10-16 21:39:22 +00:00
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2023-10-17 15:47:50 +00:00
|
|
|
void
|
|
|
|
get_normal_vector(const cplane_t *p, vec3_t normal)
|
|
|
|
{
|
|
|
|
if (p)
|
|
|
|
{
|
|
|
|
VectorCopy(p->normal, normal);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
VectorCopy(vec3_origin, normal);
|
|
|
|
}
|
|
|
|
}
|
2023-10-16 21:39:22 +00:00
|
|
|
|
2023-10-17 15:47:50 +00:00
|
|
|
vec3_t VEC_UP = {0, -1, 0};
|
|
|
|
vec3_t MOVEDIR_UP = {0, 0, 1};
|
|
|
|
vec3_t VEC_DOWN = {0, -2, 0};
|
|
|
|
vec3_t MOVEDIR_DOWN = {0, 0, -1};
|
2023-10-16 21:39:22 +00:00
|
|
|
|
2023-10-17 15:47:50 +00:00
|
|
|
void
|
|
|
|
G_SetMovedir(vec3_t angles, vec3_t movedir)
|
2023-10-16 21:39:22 +00:00
|
|
|
{
|
2023-10-17 15:47:50 +00:00
|
|
|
if (VectorCompare(angles, VEC_UP))
|
2023-10-16 21:39:22 +00:00
|
|
|
{
|
2023-10-17 15:47:50 +00:00
|
|
|
VectorCopy(MOVEDIR_UP, movedir);
|
2023-10-16 21:39:22 +00:00
|
|
|
}
|
2023-10-17 15:47:50 +00:00
|
|
|
else if (VectorCompare(angles, VEC_DOWN))
|
2023-10-16 21:39:22 +00:00
|
|
|
{
|
2023-10-17 15:47:50 +00:00
|
|
|
VectorCopy(MOVEDIR_DOWN, movedir);
|
2023-10-16 21:39:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-10-17 15:47:50 +00:00
|
|
|
AngleVectors(angles, movedir, NULL, NULL);
|
2023-10-16 21:39:22 +00:00
|
|
|
}
|
|
|
|
|
2023-10-17 15:47:50 +00:00
|
|
|
VectorClear(angles);
|
2023-10-16 21:39:22 +00:00
|
|
|
}
|
|
|
|
|
2023-10-17 15:47:50 +00:00
|
|
|
float
|
|
|
|
vectoyaw(vec3_t vec)
|
2023-10-16 21:39:22 +00:00
|
|
|
{
|
2023-10-17 15:47:50 +00:00
|
|
|
float yaw;
|
|
|
|
|
|
|
|
if (vec[PITCH] == 0)
|
2023-10-16 21:39:22 +00:00
|
|
|
{
|
|
|
|
yaw = 0;
|
2023-10-17 15:47:50 +00:00
|
|
|
|
2023-10-16 21:39:22 +00:00
|
|
|
if (vec[YAW] > 0)
|
2023-10-17 15:47:50 +00:00
|
|
|
{
|
2023-10-16 21:39:22 +00:00
|
|
|
yaw = 90;
|
2023-10-17 15:47:50 +00:00
|
|
|
}
|
2023-10-16 21:39:22 +00:00
|
|
|
else if (vec[YAW] < 0)
|
2023-10-17 15:47:50 +00:00
|
|
|
{
|
2023-10-16 21:39:22 +00:00
|
|
|
yaw = -90;
|
2023-10-17 15:47:50 +00:00
|
|
|
}
|
2023-10-16 21:39:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-10-17 15:47:50 +00:00
|
|
|
yaw = (int)(atan2(vec[YAW], vec[PITCH]) * 180 / M_PI);
|
|
|
|
|
2023-10-16 21:39:22 +00:00
|
|
|
if (yaw < 0)
|
2023-10-17 15:47:50 +00:00
|
|
|
{
|
2023-10-16 21:39:22 +00:00
|
|
|
yaw += 360;
|
2023-10-17 15:47:50 +00:00
|
|
|
}
|
2023-10-16 21:39:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return yaw;
|
|
|
|
}
|
|
|
|
|
2023-10-17 15:47:50 +00:00
|
|
|
void
|
|
|
|
vectoangles(vec3_t value1, vec3_t angles)
|
2023-10-16 21:39:22 +00:00
|
|
|
{
|
2023-10-17 15:47:50 +00:00
|
|
|
float forward;
|
|
|
|
float yaw, pitch;
|
|
|
|
|
|
|
|
if ((value1[1] == 0) && (value1[0] == 0))
|
2023-10-16 21:39:22 +00:00
|
|
|
{
|
|
|
|
yaw = 0;
|
2023-10-17 15:47:50 +00:00
|
|
|
|
2023-10-16 21:39:22 +00:00
|
|
|
if (value1[2] > 0)
|
2023-10-17 15:47:50 +00:00
|
|
|
{
|
2023-10-16 21:39:22 +00:00
|
|
|
pitch = 90;
|
2023-10-17 15:47:50 +00:00
|
|
|
}
|
2023-10-16 21:39:22 +00:00
|
|
|
else
|
2023-10-17 15:47:50 +00:00
|
|
|
{
|
2023-10-16 21:39:22 +00:00
|
|
|
pitch = 270;
|
2023-10-17 15:47:50 +00:00
|
|
|
}
|
2023-10-16 21:39:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (value1[0])
|
2023-10-17 15:47:50 +00:00
|
|
|
{
|
|
|
|
yaw = (int)(atan2(value1[1], value1[0]) * 180 / M_PI);
|
|
|
|
}
|
2023-10-16 21:39:22 +00:00
|
|
|
else if (value1[1] > 0)
|
2023-10-17 15:47:50 +00:00
|
|
|
{
|
2023-10-16 21:39:22 +00:00
|
|
|
yaw = 90;
|
2023-10-17 15:47:50 +00:00
|
|
|
}
|
2023-10-16 21:39:22 +00:00
|
|
|
else
|
2023-10-17 15:47:50 +00:00
|
|
|
{
|
2023-10-16 21:39:22 +00:00
|
|
|
yaw = -90;
|
2023-10-17 15:47:50 +00:00
|
|
|
}
|
|
|
|
|
2023-10-16 21:39:22 +00:00
|
|
|
if (yaw < 0)
|
2023-10-17 15:47:50 +00:00
|
|
|
{
|
2023-10-16 21:39:22 +00:00
|
|
|
yaw += 360;
|
2023-10-17 15:47:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
forward = sqrt(value1[0] * value1[0] + value1[1] * value1[1]);
|
|
|
|
pitch = (int)(atan2(value1[2], forward) * 180 / M_PI);
|
2023-10-16 21:39:22 +00:00
|
|
|
|
|
|
|
if (pitch < 0)
|
2023-10-17 15:47:50 +00:00
|
|
|
{
|
2023-10-16 21:39:22 +00:00
|
|
|
pitch += 360;
|
2023-10-17 15:47:50 +00:00
|
|
|
}
|
2023-10-16 21:39:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
angles[PITCH] = -pitch;
|
|
|
|
angles[YAW] = yaw;
|
|
|
|
angles[ROLL] = 0;
|
|
|
|
}
|
|
|
|
|
2023-10-17 15:47:50 +00:00
|
|
|
char *
|
|
|
|
G_CopyString(char *in)
|
2023-10-16 21:39:22 +00:00
|
|
|
{
|
2023-10-17 15:47:50 +00:00
|
|
|
char *out;
|
|
|
|
|
|
|
|
out = gi.TagMalloc(strlen(in) + 1, TAG_LEVEL);
|
|
|
|
strcpy(out, in);
|
2023-10-16 21:39:22 +00:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2023-10-17 15:47:50 +00:00
|
|
|
void
|
|
|
|
G_InitEdict(edict_t *e)
|
2023-10-16 21:39:22 +00:00
|
|
|
{
|
|
|
|
e->inuse = true;
|
|
|
|
e->classname = "noclass";
|
|
|
|
e->gravity = 1.0;
|
|
|
|
e->s.number = e - g_edicts;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2023-10-17 15:47:50 +00:00
|
|
|
* Either finds a free edict, or allocates a
|
|
|
|
* new one. 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.
|
|
|
|
*/
|
|
|
|
#define POLICY_DEFAULT 0
|
|
|
|
#define POLICY_DESPERATE 1
|
|
|
|
|
|
|
|
static edict_t *
|
|
|
|
G_FindFreeEdict(int policy)
|
2023-10-16 21:39:22 +00:00
|
|
|
{
|
2023-10-17 15:47:50 +00:00
|
|
|
edict_t *e;
|
2023-10-16 21:39:22 +00:00
|
|
|
|
2023-10-17 15:47:50 +00:00
|
|
|
for (e = g_edicts + game.maxclients + 1 ; e < &g_edicts[globals.num_edicts] ; e++)
|
2023-10-16 21:39:22 +00:00
|
|
|
{
|
2023-10-17 15:47:50 +00:00
|
|
|
/* the first couple seconds of server time can involve a lot of
|
|
|
|
freeing and allocating, so relax the replacement policy
|
|
|
|
*/
|
|
|
|
if (!e->inuse && (policy == POLICY_DESPERATE || e->freetime < 2.0f || (level.time - e->freetime) > 0.5f))
|
2023-10-16 21:39:22 +00:00
|
|
|
{
|
|
|
|
G_InitEdict (e);
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
}
|
2023-10-17 15:47:50 +00:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
edict_t *
|
|
|
|
G_SpawnOptional(void)
|
|
|
|
{
|
|
|
|
edict_t *e = G_FindFreeEdict (POLICY_DEFAULT);
|
|
|
|
|
|
|
|
if (e)
|
|
|
|
{
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (globals.num_edicts >= game.maxentities)
|
|
|
|
{
|
|
|
|
return G_FindFreeEdict (POLICY_DESPERATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
e = &g_edicts[globals.num_edicts++];
|
2023-10-16 21:39:22 +00:00
|
|
|
G_InitEdict (e);
|
2023-10-17 15:47:50 +00:00
|
|
|
|
2023-10-16 21:39:22 +00:00
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2023-10-17 15:47:50 +00:00
|
|
|
edict_t *
|
|
|
|
G_Spawn(void)
|
|
|
|
{
|
|
|
|
edict_t *e = G_SpawnOptional();
|
2023-10-16 21:39:22 +00:00
|
|
|
|
2023-10-17 15:47:50 +00:00
|
|
|
if (!e)
|
|
|
|
gi.error ("ED_Alloc: no free edicts");
|
|
|
|
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Marks the edict as free
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
G_FreeEdict(edict_t *ed)
|
2023-10-16 21:39:22 +00:00
|
|
|
{
|
2023-10-17 15:47:50 +00:00
|
|
|
gi.unlinkentity(ed); /* unlink from world */
|
2023-10-16 21:39:22 +00:00
|
|
|
|
|
|
|
if ((ed - g_edicts) <= (maxclients->value + BODY_QUEUE_SIZE))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-10-17 15:47:50 +00:00
|
|
|
memset(ed, 0, sizeof(*ed));
|
2023-10-16 21:39:22 +00:00
|
|
|
ed->classname = "freed";
|
|
|
|
ed->freetime = level.time;
|
|
|
|
ed->inuse = false;
|
|
|
|
}
|
|
|
|
|
2023-10-17 15:47:50 +00:00
|
|
|
void
|
|
|
|
G_TouchTriggers(edict_t *ent)
|
2023-10-16 21:39:22 +00:00
|
|
|
{
|
2023-10-17 15:47:50 +00:00
|
|
|
int i, num;
|
|
|
|
edict_t *touch[MAX_EDICTS], *hit;
|
2023-10-16 21:39:22 +00:00
|
|
|
|
2023-10-17 15:47:50 +00:00
|
|
|
if (!ent)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* dead things don't activate triggers! */
|
2023-10-16 21:39:22 +00:00
|
|
|
if ((ent->client || (ent->svflags & SVF_MONSTER)) && (ent->health <= 0))
|
2023-10-17 15:47:50 +00:00
|
|
|
{
|
2023-10-16 21:39:22 +00:00
|
|
|
return;
|
2023-10-17 15:47:50 +00:00
|
|
|
}
|
2023-10-16 21:39:22 +00:00
|
|
|
|
2023-10-17 15:47:50 +00:00
|
|
|
num = gi.BoxEdicts(ent->absmin, ent->absmax, touch,
|
|
|
|
MAX_EDICTS, AREA_TRIGGERS);
|
2023-10-16 21:39:22 +00:00
|
|
|
|
2023-10-17 15:47:50 +00:00
|
|
|
/* be careful, it is possible to have an entity in this
|
|
|
|
list removed before we get to it (killtriggered) */
|
|
|
|
for (i = 0; i < num; i++)
|
2023-10-16 21:39:22 +00:00
|
|
|
{
|
|
|
|
hit = touch[i];
|
2023-10-17 15:47:50 +00:00
|
|
|
|
2023-10-16 21:39:22 +00:00
|
|
|
if (!hit->inuse)
|
2023-10-17 15:47:50 +00:00
|
|
|
{
|
2023-10-16 21:39:22 +00:00
|
|
|
continue;
|
2023-10-17 15:47:50 +00:00
|
|
|
}
|
|
|
|
|
2023-10-16 21:39:22 +00:00
|
|
|
if (!hit->touch)
|
2023-10-17 15:47:50 +00:00
|
|
|
{
|
2023-10-16 21:39:22 +00:00
|
|
|
continue;
|
2023-10-17 15:47:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hit->touch(hit, ent, NULL, NULL);
|
2023-10-16 21:39:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2023-10-17 15:47:50 +00:00
|
|
|
* Call after linking a new trigger in during gameplay
|
|
|
|
* to force all entities it covers to immediately touch it
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
G_TouchSolids(edict_t *ent)
|
2023-10-16 21:39:22 +00:00
|
|
|
{
|
2023-10-17 15:47:50 +00:00
|
|
|
int i, num;
|
|
|
|
edict_t *touch[MAX_EDICTS], *hit;
|
2023-10-16 21:39:22 +00:00
|
|
|
|
2023-10-17 15:47:50 +00:00
|
|
|
if (!ent)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2023-10-16 21:39:22 +00:00
|
|
|
|
2023-10-17 15:47:50 +00:00
|
|
|
num = gi.BoxEdicts(ent->absmin, ent->absmax, touch,
|
|
|
|
MAX_EDICTS, AREA_SOLID);
|
|
|
|
|
|
|
|
/* be careful, it is possible to have an entity in this
|
|
|
|
list removed before we get to it (killtriggered) */
|
|
|
|
for (i = 0; i < num; i++)
|
2023-10-16 21:39:22 +00:00
|
|
|
{
|
|
|
|
hit = touch[i];
|
2023-10-17 15:47:50 +00:00
|
|
|
|
2023-10-16 21:39:22 +00:00
|
|
|
if (!hit->inuse)
|
2023-10-17 15:47:50 +00:00
|
|
|
{
|
2023-10-16 21:39:22 +00:00
|
|
|
continue;
|
2023-10-17 15:47:50 +00:00
|
|
|
}
|
|
|
|
|
2023-10-16 21:39:22 +00:00
|
|
|
if (ent->touch)
|
2023-10-17 15:47:50 +00:00
|
|
|
{
|
|
|
|
ent->touch(hit, ent, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
2023-10-16 21:39:22 +00:00
|
|
|
if (!ent->inuse)
|
2023-10-17 15:47:50 +00:00
|
|
|
{
|
2023-10-16 21:39:22 +00:00
|
|
|
break;
|
2023-10-17 15:47:50 +00:00
|
|
|
}
|
2023-10-16 21:39:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2023-10-17 15:47:50 +00:00
|
|
|
* Kills all entities that would touch the proposed new positioning
|
|
|
|
* of ent. Ent should be unlinked before calling this!
|
|
|
|
*/
|
|
|
|
qboolean
|
|
|
|
KillBox(edict_t *ent)
|
2023-10-16 21:39:22 +00:00
|
|
|
{
|
2023-10-17 15:47:50 +00:00
|
|
|
trace_t tr;
|
|
|
|
|
|
|
|
if (!ent)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2023-10-16 21:39:22 +00:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
2023-10-17 15:47:50 +00:00
|
|
|
tr = gi.trace(ent->s.origin, ent->mins, ent->maxs, ent->s.origin,
|
|
|
|
NULL, MASK_PLAYERSOLID);
|
|
|
|
|
2023-10-16 21:39:22 +00:00
|
|
|
if (!tr.ent)
|
2023-10-17 15:47:50 +00:00
|
|
|
{
|
2023-10-16 21:39:22 +00:00
|
|
|
break;
|
2023-10-17 15:47:50 +00:00
|
|
|
}
|
2023-10-16 21:39:22 +00:00
|
|
|
|
2023-10-17 15:47:50 +00:00
|
|
|
/* nail it */
|
|
|
|
T_Damage(tr.ent, ent, ent, vec3_origin, ent->s.origin, vec3_origin,
|
|
|
|
100000, 0, DAMAGE_NO_PROTECTION, MOD_TELEFRAG);
|
2023-10-16 21:39:22 +00:00
|
|
|
|
2023-10-17 15:47:50 +00:00
|
|
|
/* if we didn't kill it, fail */
|
2023-10-16 21:39:22 +00:00
|
|
|
if (tr.ent->solid)
|
2023-10-17 15:47:50 +00:00
|
|
|
{
|
2023-10-16 21:39:22 +00:00
|
|
|
return false;
|
2023-10-17 15:47:50 +00:00
|
|
|
}
|
2023-10-16 21:39:22 +00:00
|
|
|
}
|
|
|
|
|
2023-10-17 15:47:50 +00:00
|
|
|
return true; /* all clear */
|
2023-10-16 21:39:22 +00:00
|
|
|
}
|