1100 lines · 23.8 KB · C
RawDownload
#include <u.h>#include <libc.h>#include <stdio.h>#include "../dat.h"#include "../fns.h" qboolean FindTarget (edict_t *self);extern cvar_t	*maxclients; qboolean ai_checkattack (edict_t *self, float dist); qboolean	enemy_vis;qboolean	enemy_infront;int			enemy_range;float		enemy_yaw; //============================================================================  /*=================AI_SetSightClient Called once each frame to set level.sight_client to theplayer to be checked for in findtarget. If all clients are either dead or in notarget, sight_clientwill be null. In coop games, sight_client will cycle between the clients.=================*/void AI_SetSightClient (void){	edict_t	*ent;	int		start, check; 	if (level.sight_client == NULL)		start = 1;	else		start = level.sight_client - g_edicts; 	check = start;	while (1)	{		check++;		if (check > game.maxclients)			check = 1;		ent = &g_edicts[check];		if (ent->inuse			&& ent->health > 0			&& !(ent->flags & FL_NOTARGET) )		{			level.sight_client = ent;			return;		// got one		}		if (check == start)		{			level.sight_client = NULL;			return;		// nobody to see		}	}} //============================================================================ /*=============ai_move Move the specified distance at current facing.This replaces the QC functions: ai_forward, ai_back, ai_pain, and ai_painforward==============*/void ai_move (edict_t *self, float dist){	M_walkmove (self, self->s.angles[YAW], dist);}  /*=============ai_stand Used for standing around and looking for playersDistance is for slight position adjustments needed by the animations==============*/void ai_stand (edict_t *self, float dist){	vec3_t	v; 	if (dist)		M_walkmove (self, self->s.angles[YAW], dist); 	if (self->monsterinfo.aiflags & AI_STAND_GROUND)	{		if (self->enemy)		{			VectorSubtract (self->enemy->s.origin, self->s.origin, v);			self->ideal_yaw = vectoyaw(v);			if (self->s.angles[YAW] != self->ideal_yaw && self->monsterinfo.aiflags & AI_TEMP_STAND_GROUND)			{				self->monsterinfo.aiflags &= ~(AI_STAND_GROUND | AI_TEMP_STAND_GROUND);				self->monsterinfo.run (self);			}			M_ChangeYaw (self);			ai_checkattack (self, 0);		}		else			FindTarget (self);		return;	} 	if (FindTarget (self))		return;		if (level.time > self->monsterinfo.pausetime)	{		self->monsterinfo.walk (self);		return;	} 	if (!(self->spawnflags & 1) && (self->monsterinfo.idle) && (level.time > self->monsterinfo.idle_time))	{		if (self->monsterinfo.idle_time)		{			self->monsterinfo.idle (self);			self->monsterinfo.idle_time = level.time + 15 + qrandom() * 15;		}		else		{			self->monsterinfo.idle_time = level.time + qrandom() * 15;		}	}}  /*=============ai_walk The monster is walking it's beat=============*/void ai_walk (edict_t *self, float dist){	M_MoveToGoal (self, dist); 	// check for noticing a player	if (FindTarget (self))		return; 	if ((self->monsterinfo.search) && (level.time > self->monsterinfo.idle_time))	{		if (self->monsterinfo.idle_time)		{			self->monsterinfo.search (self);			self->monsterinfo.idle_time = level.time + 15 + qrandom() * 15;		}		else		{			self->monsterinfo.idle_time = level.time + qrandom() * 15;		}	}}  /*=============ai_charge Turns towards target and advancesUse this call with a distnace of 0 to replace ai_face==============*/void ai_charge (edict_t *self, float dist){	vec3_t	v; 	VectorSubtract (self->enemy->s.origin, self->s.origin, v);	self->ideal_yaw = vectoyaw(v);	M_ChangeYaw (self); 	if (dist)		M_walkmove (self, self->s.angles[YAW], dist);}  /*=============ai_turn don't move, but turn towards ideal_yawDistance is for slight position adjustments needed by the animations=============*/void ai_turn (edict_t *self, float dist){	if (dist)		M_walkmove (self, self->s.angles[YAW], dist); 	if (FindTarget (self))		return;		M_ChangeYaw (self);}  /* .enemyWill be world if not currently angry at anyone. .movetargetThe next path spot to walk toward.  If .enemy, ignore .movetarget.When an enemy is killed, the monster will try to return to it's path. .hunt_timeSet to time + something when the player is in sight, but movement straight forhim is blocked.  This causes the monster to use wall following code formovement direction instead of sighting on the player. .ideal_yawA yaw angle of the intended direction, which will be turned towards at upto 45 deg / state.  If the enemy is in view and hunt_time is not active,this will be the exact line towards the enemy. .pausetimeA monster will leave it's stand state and head towards it's .movetarget whentime > .pausetime. walkmove(angle, speed) primitive is all or nothing*/ /*=============range returns the range catagorization of an entity reletive to self0	melee range, will become hostile even if back is turned1	visibility and infront, or visibility and show hostile2	infront and show hostile3	only triggered by damage=============*/int range (edict_t *self, edict_t *other){	vec3_t	v;	float	len; 	VectorSubtract (self->s.origin, other->s.origin, v);	len = VectorLength (v);	if (len < MELEE_DISTANCE)		return RANGE_MELEE;	if (len < 500)		return RANGE_NEAR;	if (len < 1000)		return RANGE_MID;	return RANGE_FAR;} /*=============visible returns 1 if the entity is visible to self, even if not infront ()=============*/qboolean visible (edict_t *self, edict_t *other){	vec3_t	spot1;	vec3_t	spot2;	trace_t	trace; 	VectorCopy (self->s.origin, spot1);	spot1[2] += self->viewheight;	VectorCopy (other->s.origin, spot2);	spot2[2] += other->viewheight;	trace = gi.trace (spot1, vec3_origin, vec3_origin, spot2, self, MASK_OPAQUE);		if (trace.fraction == 1.0)		return true;	return false;}  /*=============infront returns 1 if the entity is in front (in sight) of self=============*/qboolean infront (edict_t *self, edict_t *other){	vec3_t	vec;	float	dot;	vec3_t	forward;		AngleVectors (self->s.angles, forward, NULL, NULL);	VectorSubtract (other->s.origin, self->s.origin, vec);	VectorNormalize (vec);	dot = DotProduct (vec, forward);		if (dot > 0.3)		return true;	return false;}  //============================================================================ void HuntTarget (edict_t *self){	vec3_t	vec; 	self->goalentity = self->enemy;	if (self->monsterinfo.aiflags & AI_STAND_GROUND)		self->monsterinfo.stand (self);	else		self->monsterinfo.run (self);	VectorSubtract (self->enemy->s.origin, self->s.origin, vec);	self->ideal_yaw = vectoyaw(vec);	// wait a while before first attack	if (!(self->monsterinfo.aiflags & AI_STAND_GROUND))		AttackFinished (self, 1);} void FoundTarget (edict_t *self){	// let other monsters see this monster for a while	if (self->enemy->client)	{		level.sight_entity = self;		level.sight_entity_framenum = level.framenum;		level.sight_entity->light_level = 128;	} 	self->show_hostile = level.time + 1;		// wake up other monsters 	VectorCopy(self->enemy->s.origin, self->monsterinfo.last_sighting);	self->monsterinfo.trail_time = level.time; 	if (!self->combattarget)	{		HuntTarget (self);		return;	} 	self->goalentity = self->movetarget = G_PickTarget(self->combattarget);	if (!self->movetarget)	{		self->goalentity = self->movetarget = self->enemy;		HuntTarget (self);		gi.dprintf("%s at %s, combattarget %s not found\n", self->classname, vtos(self->s.origin), self->combattarget);		return;	} 	// clear out our combattarget, these are a one shot deal	self->combattarget = NULL;	self->monsterinfo.aiflags |= AI_COMBAT_POINT; 	// clear the targetname, that point is ours!	self->movetarget->targetname = NULL;	self->monsterinfo.pausetime = 0; 	// run for it	self->monsterinfo.run (self);}  /*===========FindTarget Self is currently not attacking anything, so try to find a target Returns TRUE if an enemy was sighted When a player fires a missile, the point of impact becomes a fakeplayer sothat monsters that see the impact will respond as if they had seen theplayer. To avoid spending too much time, only a single client (or fakeclient) ischecked each frame.  This means multi player games will have slightlyslower noticing monsters.============*/qboolean FindTarget (edict_t *self){	edict_t		*client;	qboolean	heardit;	int			r; 	if (self->monsterinfo.aiflags & AI_GOOD_GUY)	{		if (self->goalentity && self->goalentity->inuse && self->goalentity->classname)		{			if (strcmp(self->goalentity->classname, "target_actor") == 0)				return false;		} 		//FIXME look for monsters?		return false;	} 	// if we're going to a combat point, just proceed	if (self->monsterinfo.aiflags & AI_COMBAT_POINT)		return false; // if the first spawnflag bit is set, the monster will only wake up on// really seeing the player, not another monster getting angry or hearing// something // revised behavior so they will wake up if they "see" a player make a noise// but not weapon impact/explosion noises 	heardit = false;	if ((level.sight_entity_framenum >= (level.framenum - 1)) && !(self->spawnflags & 1) )	{		client = level.sight_entity;		if (client->enemy == self->enemy)		{			return false;		}	}	else if (level.sound_entity_framenum >= (level.framenum - 1))	{		client = level.sound_entity;		heardit = true;	}	else if (!(self->enemy) && (level.sound2_entity_framenum >= (level.framenum - 1)) && !(self->spawnflags & 1) )	{		client = level.sound2_entity;		heardit = true;	}	else	{		client = level.sight_client;		if (!client)			return false;	// no clients to get mad at	} 	// if the entity went away, forget it	if (!client->inuse)		return false; 	if (client == self->enemy)		return true;	// JDC false; 	if (client->client)	{		if (client->flags & FL_NOTARGET)			return false;	}	else if (client->svflags & SVF_MONSTER)	{		if (!client->enemy)			return false;		if (client->enemy->flags & FL_NOTARGET)			return false;	}	else if (heardit)	{		if (client->owner->flags & FL_NOTARGET)			return false;	}	else		return false; 	if (!heardit)	{		r = range (self, client); 		if (r == RANGE_FAR)			return false; // this is where we would check invisibility 		// is client in an spot too dark to be seen?		if (client->light_level <= 5)			return false; 		if (!visible (self, client))		{			return false;		} 		if (r == RANGE_NEAR)		{			if (client->show_hostile < level.time && !infront (self, client))			{				return false;			}		}		else if (r == RANGE_MID)		{			if (!infront (self, client))			{

Lines 1–500 of 1100.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t