Files
commandergenius/project/jni/application/quake/source/sv_move.c

789 lines
20 KiB
C

/*
Copyright (C) 1996-1997 Id Software, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
// sv_move.c -- monster movement
#include "quakedef.h"
#define STEPSIZE 18
/*
=============
SV_CheckBottom
Returns false if any part of the bottom of the entity is off an edge that
is not a staircase.
=============
*/
int c_yes, c_no;
qboolean SV_CheckBottom (edict_t *ent)
{
vec3_t mins, maxs, start, stop;
trace_t trace;
int x, y;
float mid, bottom;
VectorAdd (ent->v.origin, ent->v.mins, mins);
VectorAdd (ent->v.origin, ent->v.maxs, maxs);
// if all of the points under the corners are solid world, don't bother
// with the tougher checks
// the corners must be within 16 of the midpoint
start[2] = mins[2] - 1;
for (x=0 ; x<=1 ; x++)
for (y=0 ; y<=1 ; y++)
{
start[0] = x ? maxs[0] : mins[0];
start[1] = y ? maxs[1] : mins[1];
if (SV_PointContents (start) != CONTENTS_SOLID)
goto realcheck;
}
c_yes++;
return true; // we got out easy
realcheck:
c_no++;
//
// check it for real...
//
start[2] = mins[2];
// the midpoint must be within 16 of the bottom
start[0] = stop[0] = (float)((mins[0] + maxs[0])*0.5);
start[1] = stop[1] = (float)((mins[1] + maxs[1])*0.5);
stop[2] = start[2] - 2*STEPSIZE;
trace = SV_Move (start, vec3_origin, vec3_origin, stop, true, ent);
if (trace.fraction == 1.0)
return false;
mid = bottom = trace.endpos[2];
// the corners must be within 16 of the midpoint
for (x=0 ; x<=1 ; x++)
for (y=0 ; y<=1 ; y++)
{
start[0] = stop[0] = x ? maxs[0] : mins[0];
start[1] = stop[1] = y ? maxs[1] : mins[1];
trace = SV_Move (start, vec3_origin, vec3_origin, stop, true, ent);
if (trace.fraction != 1.0 && trace.endpos[2] > bottom)
bottom = trace.endpos[2];
if (trace.fraction == 1.0 || mid - trace.endpos[2] > STEPSIZE)
return false;
}
c_yes++;
return true;
}
#ifdef USEFPM
qboolean SV_CheckBottomFPM (edict_FPM_t *ent)
{
vec3_t tmpmins, tmpmaxs;
vec3_FPM_t start, stop, mins, maxs;
trace_FPM_t trace;
int x, y;
fixedpoint_t mid, bottom;
VectorAdd (ent->v.origin, ent->v.mins, tmpmins);
VectorAdd (ent->v.origin, ent->v.maxs, tmpmaxs);
VectorToFPM(tmpmins, mins);
VectorToFPM(tmpmins, maxs);
// if all of the points under the corners are solid world, don't bother
// with the tougher checks
// the corners must be within 16 of the midpoint
start[2] = FPM_SUB(mins[2], 1);
for (x=0 ; x<=1 ; x++)
for (y=0 ; y<=1 ; y++)
{
start[0] = x ? maxs[0] : mins[0];
start[1] = y ? maxs[1] : mins[1];
if (SV_PointContentsFPM (start) != CONTENTS_SOLID)
goto realcheck;
}
c_yes++;
return true; // we got out easy
realcheck:
c_no++;
//
// check it for real...
//
start[2] = mins[2];
// the midpoint must be within 16 of the bottom
start[0] = stop[0] = FPM_MUL(FPM_ADD(mins[0], maxs[0]), FPM_FROMFLOATC(0.5));
start[1] = stop[1] = FPM_MUL(FPM_ADD(mins[1], maxs[1]), FPM_FROMFLOATC(0.5));
stop[2] = FPM_SUB(start[2], FPM_FROMLONGC(2*STEPSIZE));
trace = SV_MoveFPM (start, vec3_originFPM, vec3_originFPM, stop, true, ent);
if (trace.fraction == FPM_FROMFLOATC(1.0))
return false;
mid = bottom = trace.endpos[2];
// the corners must be within 16 of the midpoint
for (x=0 ; x<=1 ; x++)
for (y=0 ; y<=1 ; y++)
{
start[0] = stop[0] = x ? maxs[0] : mins[0];
start[1] = stop[1] = y ? maxs[1] : mins[1];
trace = SV_MoveFPM (start, vec3_originFPM, vec3_originFPM, stop, true, ent);
if (trace.fraction != FPM_FROMFLOATC(1.0) && trace.endpos[2] > bottom)
bottom = trace.endpos[2];
if (trace.fraction == FPM_FROMFLOATC(1.0) || mid - trace.endpos[2] > STEPSIZE)
return false;
}
c_yes++;
return true;
}
#endif //USEFPM
/*
=============
SV_movestep
Called by monster program code.
The move will be adjusted for slopes and stairs, but if the move isn't
possible, no move is done, false is returned, and
pr_global_struct->trace_normal is set to the normal of the blocking wall
=============
*/
qboolean SV_movestep (edict_t *ent, vec3_t move, qboolean relink)
{
float dz;
vec3_t oldorg, neworg, end;
trace_t trace;
int i;
edict_t *enemy;
// try the move
VectorCopy (ent->v.origin, oldorg);
VectorAdd (ent->v.origin, move, neworg);
// flying monsters don't step up
if ( (int)ent->v.flags & (FL_SWIM | FL_FLY) )
{
// try one move with vertical motion, then one without
for (i=0 ; i<2 ; i++)
{
VectorAdd (ent->v.origin, move, neworg);
enemy = PROG_TO_EDICT(ent->v.enemy);
if (i == 0 && enemy != sv.edicts)
{
dz = ent->v.origin[2] - PROG_TO_EDICT(ent->v.enemy)->v.origin[2];
if (dz > 40)
neworg[2] -= 8;
if (dz < 30)
neworg[2] += 8;
}
trace = SV_Move (ent->v.origin, ent->v.mins, ent->v.maxs, neworg, false, ent);
if (trace.fraction == 1)
{
if ( ((int)ent->v.flags & FL_SWIM) && SV_PointContents(trace.endpos) == CONTENTS_EMPTY )
return false; // swim monster left water
VectorCopy (trace.endpos, ent->v.origin);
if (relink)
SV_LinkEdict (ent, true);
return true;
}
if (enemy == sv.edicts)
break;
}
return false;
}
// push down from a step height above the wished position
neworg[2] += STEPSIZE;
VectorCopy (neworg, end);
end[2] -= STEPSIZE*2;
trace = SV_Move (neworg, ent->v.mins, ent->v.maxs, end, false, ent);
if (trace.allsolid)
return false;
if (trace.startsolid)
{
neworg[2] -= STEPSIZE;
trace = SV_Move (neworg, ent->v.mins, ent->v.maxs, end, false, ent);
if (trace.allsolid || trace.startsolid)
return false;
}
if (trace.fraction == 1)
{
// if monster had the ground pulled out, go ahead and fall
if ( (int)ent->v.flags & FL_PARTIALGROUND )
{
VectorAdd (ent->v.origin, move, ent->v.origin);
if (relink)
SV_LinkEdict (ent, true);
ent->v.flags = (float)((int)ent->v.flags & ~FL_ONGROUND);
// Con_Printf ("fall down\n");
return true;
}
return false; // walked off an edge
}
// check point traces down for dangling corners
VectorCopy (trace.endpos, ent->v.origin);
if (!SV_CheckBottom (ent))
{
if ( (int)ent->v.flags & FL_PARTIALGROUND )
{ // entity had floor mostly pulled out from underneath it
// and is trying to correct
if (relink)
SV_LinkEdict (ent, true);
return true;
}
VectorCopy (oldorg, ent->v.origin);
return false;
}
if ( (int)ent->v.flags & FL_PARTIALGROUND )
{
// Con_Printf ("back on ground\n");
ent->v.flags = (float)((int)ent->v.flags & ~FL_PARTIALGROUND);
}
ent->v.groundentity = EDICT_TO_PROG(trace.ent);
// the move is ok
if (relink)
SV_LinkEdict (ent, true);
return true;
}
#ifdef USEFPM
qboolean SV_movestepFPM(edict_FPM_t *ent, vec3_FPM_t move, qboolean relink)
{
fixedpoint_t dz;
vec3_FPM_t oldorg, neworg, end;
trace_FPM_t trace;
int i;
edict_FPM_t *enemy;
vec3_FPM_t vorigin, vmins, vmaxs;
vec3_t tmp;
VectorToFPM(ent->v.origin, vorigin);
VectorToFPM(ent->v.mins, vmins);
VectorToFPM(ent->v.maxs, vmaxs);
// try the move
VectorCopy (vorigin, oldorg);
VectorAdd (vorigin, move, neworg);
// flying monsters don't step up
if ( (int)ent->v.flags & (FL_SWIM | FL_FLY) )
{
// try one move with vertical motion, then one without
for (i=0 ; i<2 ; i++)
{
VectorAdd (vorigin, move, neworg);
enemy = PROG_TO_EDICTFPM(ent->v.enemy);
if (i == 0 && enemy != svFPM.edicts)
{
dz = FPM_SUB(vorigin[2], FPM_FROMFLOAT(PROG_TO_EDICT(ent->v.enemy)->v.origin[2]));
if (dz > FPM_FROMLONGC(40))
neworg[2] = FPM_SUB(neworg[2], FPM_FROMLONGC(8));
if (dz < FPM_FROMLONGC(30))
neworg[2] = FPM_ADD(neworg[2], FPM_FROMLONGC(8));
}
trace = SV_MoveFPM (vorigin, vmins, vmaxs, neworg, false, ent);
if (trace.fraction == FPM_FROMLONGC(1))
{
if ( ((int)ent->v.flags & FL_SWIM) && SV_PointContentsFPM(trace.endpos) == CONTENTS_EMPTY )
return false; // swim monster left water
FPMToVector(trace.endpos, ent->v.origin);
//VectorCopy (trace.endpos, ent->v.origin);
if (relink)
SV_LinkEdictFPM (ent, true);
return true;
}
if (enemy == svFPM.edicts)
break;
}
return false;
}
// push down from a step height above the wished position
neworg[2] += STEPSIZE;
VectorCopy (neworg, end);
end[2] -= STEPSIZE*2;
trace = SV_MoveFPM (neworg, vmins, vmaxs, end, false, ent);
if (trace.allsolid)
return false;
if (trace.startsolid)
{
neworg[2] -= STEPSIZE;
trace = SV_MoveFPM (neworg, vmins, vmaxs, end, false, ent);
if (trace.allsolid || trace.startsolid)
return false;
}
if (trace.fraction == 1)
{
// if monster had the ground pulled out, go ahead and fall
if ( (int)ent->v.flags & FL_PARTIALGROUND )
{
FPMToVector(move, tmp);
VectorAdd (ent->v.origin, tmp, ent->v.origin);
if (relink)
SV_LinkEdictFPM (ent, true);
ent->v.flags = (float)((int)ent->v.flags & ~FL_ONGROUND);
// Con_Printf ("fall down\n");
return true;
}
return false; // walked off an edge
}
// check point traces down for dangling corners
FPMToVector(trace.endpos, ent->v.origin);
//VectorCopy (trace.endpos, ent->v.origin);
if (!SV_CheckBottomFPM (ent))
{
if ( (int)ent->v.flags & FL_PARTIALGROUND )
{ // entity had floor mostly pulled out from underneath it
// and is trying to correct
if (relink)
SV_LinkEdictFPM (ent, true);
return true;
}
FPMToVector(oldorg, ent->v.origin);
//VectorCopy (oldorg, ent->v.origin);
return false;
}
if ( (int)ent->v.flags & FL_PARTIALGROUND )
{
// Con_Printf ("back on ground\n");
ent->v.flags = (float)((int)ent->v.flags & ~FL_PARTIALGROUND);
}
ent->v.groundentity = EDICT_TO_PROGFPM(trace.ent);
// the move is ok
if (relink)
SV_LinkEdictFPM (ent, true);
return true;
}
#endif //USEFPM
//============================================================================
/*
======================
SV_StepDirection
Turns to the movement direction, and walks the current distance if
facing it.
======================
*/
void PF_changeyaw (void);
qboolean SV_StepDirection (edict_t *ent, float yaw, float dist)
{
vec3_t move, oldorigin;
float delta;
ent->v.ideal_yaw = yaw;
PF_changeyaw();
yaw = (float)(yaw*M_PI*2 / 360);
move[0] = (float)(cos(yaw)*dist);
move[1] = (float)(sin(yaw)*dist);
move[2] = 0;
VectorCopy (ent->v.origin, oldorigin);
if (SV_movestep (ent, move, false))
{
delta = ent->v.angles[YAW] - ent->v.ideal_yaw;
if (delta > 45 && delta < 315)
{ // not turned far enough, so don't take the step
VectorCopy (oldorigin, ent->v.origin);
}
SV_LinkEdict (ent, true);
return true;
}
SV_LinkEdict (ent, true);
return false;
}
#ifdef USEFPM
void PF_changeyawFPM (void);
qboolean SV_StepDirectionFPM (edict_FPM_t *ent, fixedpoint_t yaw, fixedpoint_t dist)
{
vec3_t oldorigin;
vec3_FPM_t move;
float delta;
ent->v.ideal_yaw = FPM_TOFLOAT(yaw);
PF_changeyawFPM();
yaw = FPM_DIV(FPM_MUL(yaw,FPM_PI), FPM_FROMLONG(180));
move[0] = FPM_MUL(FPM_COS(yaw),dist);
move[1] = FPM_MUL(FPM_SIN(yaw),dist);
move[2] = 0;
VectorCopy (ent->v.origin, oldorigin);
if (SV_movestepFPM (ent, move, false))
{
delta = ent->v.angles[YAW] - ent->v.ideal_yaw;
if (delta > 45 && delta < 315)
{ // not turned far enough, so don't take the step
VectorCopy (oldorigin, ent->v.origin);
}
SV_LinkEdictFPM (ent, true);
return true;
}
SV_LinkEdictFPM (ent, true);
return false;
}
#endif //USEFPM
/*
======================
SV_FixCheckBottom
======================
*/
void SV_FixCheckBottom (edict_t *ent)
{
// Con_Printf ("SV_FixCheckBottom\n");
ent->v.flags = (float)((int)ent->v.flags | FL_PARTIALGROUND);
}
#ifdef USEFPM
void SV_FixCheckBottomFPM (edict_FPM_t *ent)
{
// Con_Printf ("SV_FixCheckBottom\n");
ent->v.flags = (float)((int)ent->v.flags | FL_PARTIALGROUND);
}
#endif //USEFPM
/*
================
SV_NewChaseDir
================
*/
#define DI_NODIR -1
void SV_NewChaseDir (edict_t *actor, edict_t *enemy, float dist)
{
float deltax,deltay;
float d[3];
float tdir, olddir, turnaround;
olddir = anglemod( (float)((int)(actor->v.ideal_yaw/45)*45) );
turnaround = anglemod(olddir - 180);
deltax = enemy->v.origin[0] - actor->v.origin[0];
deltay = enemy->v.origin[1] - actor->v.origin[1];
if (deltax>10)
d[1]= 0;
else if (deltax<-10)
d[1]= 180;
else
d[1]= DI_NODIR;
if (deltay<-10)
d[2]= 270;
else if (deltay>10)
d[2]= 90;
else
d[2]= DI_NODIR;
// try direct route
if (d[1] != DI_NODIR && d[2] != DI_NODIR)
{
if (d[1] == 0)
tdir = (float)(d[2] == 90 ? 45 : 315);
else
tdir = (float)(d[2] == 90 ? 135 : 215);
if (tdir != turnaround && SV_StepDirection(actor, tdir, dist))
return;
}
// try other directions
if ( ((rand()&3) & 1) || abs((int)deltay)>abs((int)deltax))
{
tdir=d[1];
d[1]=d[2];
d[2]=tdir;
}
if (d[1]!=DI_NODIR && d[1]!=turnaround
&& SV_StepDirection(actor, d[1], dist))
return;
if (d[2]!=DI_NODIR && d[2]!=turnaround
&& SV_StepDirection(actor, d[2], dist))
return;
/* there is no direct path to the player, so pick another direction */
if (olddir!=DI_NODIR && SV_StepDirection(actor, olddir, dist))
return;
if (rand()&1) /*randomly determine direction of search*/
{
for (tdir=0 ; tdir<=315 ; tdir += 45)
if (tdir!=turnaround && SV_StepDirection(actor, tdir, dist) )
return;
}
else
{
for (tdir=315 ; tdir >=0 ; tdir -= 45)
if (tdir!=turnaround && SV_StepDirection(actor, tdir, dist) )
return;
}
if (turnaround != DI_NODIR && SV_StepDirection(actor, turnaround, dist) )
return;
actor->v.ideal_yaw = olddir; // can't move
// if a bridge was pulled out from underneath a monster, it may not have
// a valid standing position at all
if (!SV_CheckBottom (actor))
SV_FixCheckBottom (actor);
}
#ifdef USEFPM
void SV_NewChaseDirFPM (edict_FPM_t *actor, edict_FPM_t *enemy, fixedpoint_t dist)
{
fixedpoint_t deltax,deltay;
fixedpoint_t d[3];
fixedpoint_t tdir, olddir, turnaround;
olddir = anglemodFPM(FPM_MUL(FPM_DIV(FPM_FROMFLOAT(actor->v.ideal_yaw),FPM_FROMLONGC(45)),FPM_FROMLONGC(45)) );
turnaround = anglemodFPM(FPM_SUB(olddir, FPM_FROMLONGC(180)));
deltax = FPM_SUB(FPM_FROMFLOAT(enemy->v.origin[0]), FPM_FROMFLOAT(actor->v.origin[0]));
deltay = FPM_SUB(FPM_FROMFLOAT(enemy->v.origin[1]), FPM_FROMFLOAT(actor->v.origin[1]));
if (deltax>FPM_FROMLONGC(10))
d[1]= 0;
else if (deltax<FPM_FROMLONGC(-10))
d[1]= FPM_FROMLONGC(180);
else
d[1]= FPM_FROMLONGC(DI_NODIR);
if (deltay<FPM_FROMLONGC(-10))
d[2]= FPM_FROMLONGC(270);
else if (deltay>FPM_FROMLONGC(10))
d[2]= FPM_FROMLONGC(90);
else
d[2]= FPM_FROMLONGC(DI_NODIR);
// try direct route
if (d[1] != FPM_FROMLONGC(DI_NODIR) && d[2] != FPM_FROMLONGC(DI_NODIR))
{
if (d[1] == 0)
tdir = (d[2] == FPM_FROMLONGC(90) ? FPM_FROMLONGC(45) : FPM_FROMLONGC(315));
else
tdir = (d[2] == FPM_FROMLONGC(90) ? FPM_FROMLONGC(135) : FPM_FROMLONGC(215));
if (tdir != turnaround && SV_StepDirectionFPM(actor, tdir, dist))
return;
}
// try other directions
if ( ((rand()&3) & 1) || abs((int)deltay)>abs((int)deltax))
{
tdir=d[1];
d[1]=d[2];
d[2]=tdir;
}
if (d[1]!=FPM_FROMLONGC(DI_NODIR) && d[1]!=turnaround
&& SV_StepDirectionFPM(actor, d[1], dist))
return;
if (d[2]!=FPM_FROMLONGC(DI_NODIR) && d[2]!=turnaround
&& SV_StepDirectionFPM(actor, d[2], dist))
return;
/* there is no direct path to the player, so pick another direction */
if (olddir!=FPM_FROMLONGC(DI_NODIR) && SV_StepDirectionFPM(actor, olddir, dist))
return;
if (rand()&1) /*randomly determine direction of search*/
{
for (tdir=0 ; tdir<=FPM_FROMLONGC(315) ; tdir = FPM_ADD(tdir, FPM_FROMLONGC(45)))
if (tdir!=turnaround && SV_StepDirectionFPM(actor, tdir, dist) )
return;
}
else
{
for (tdir=FPM_FROMLONGC(315) ; tdir >=0 ; tdir = FPM_SUB(tdir, FPM_FROMLONGC(45)))
if (tdir!=turnaround && SV_StepDirectionFPM(actor, tdir, dist) )
return;
}
if (turnaround != FPM_FROMLONGC(DI_NODIR) && SV_StepDirectionFPM(actor, turnaround, dist) )
return;
actor->v.ideal_yaw = FPM_TOFLOAT(olddir); // can't move
// if a bridge was pulled out from underneath a monster, it may not have
// a valid standing position at all
if (!SV_CheckBottomFPM (actor))
SV_FixCheckBottomFPM (actor);
}
#endif //USEFPM
/*
======================
SV_CloseEnough
======================
*/
qboolean SV_CloseEnough (edict_t *ent, edict_t *goal, float dist)
{
int i;
for (i=0 ; i<3 ; i++)
{
if (goal->v.absmin[i] > ent->v.absmax[i] + dist)
return false;
if (goal->v.absmax[i] < ent->v.absmin[i] - dist)
return false;
}
return true;
}
#ifdef USEFPM
qboolean SV_CloseEnoughFPM (edict_FPM_t *ent, edict_FPM_t *goal, fixedpoint_t dist)
{
int i;
float distf=FPM_TOFLOAT(dist);
for (i=0 ; i<3 ; i++)
{
if (goal->v.absmin[i] > ent->v.absmax[i] + distf)
return false;
if (goal->v.absmax[i] < ent->v.absmin[i] - distf)
return false;
}
return true;
}
#endif //USEFPM
/*
======================
SV_MoveToGoal
======================
*/
void SV_MoveToGoal (void)
{
edict_t *ent, *goal;
float dist;
#ifdef QUAKE2
edict_t *enemy;
#endif
ent = PROG_TO_EDICT(pr_global_struct->self);
goal = PROG_TO_EDICT(ent->v.goalentity);
dist = G_FLOAT(OFS_PARM0);
if ( !( (int)ent->v.flags & (FL_ONGROUND|FL_FLY|FL_SWIM) ) )
{
G_FLOAT(OFS_RETURN) = 0;
return;
}
// if the next step hits the enemy, return immediately
#ifdef QUAKE2
enemy = PROG_TO_EDICT(ent->v.enemy);
if (enemy != sv.edicts && SV_CloseEnough (ent, enemy, dist) )
#else
if ( PROG_TO_EDICT(ent->v.enemy) != sv.edicts && SV_CloseEnough (ent, goal, dist) )
#endif
return;
// bump around...
if ( (rand()&3)==1 ||
!SV_StepDirection (ent, ent->v.ideal_yaw, dist))
{
SV_NewChaseDir (ent, goal, dist);
}
}
#ifdef USEFPM
void SV_MoveToGoalFPM (void)
{
edict_FPM_t *ent, *goal;
fixedpoint_t dist;
#ifdef QUAKE2
edict_FPM_t *enemy;
#endif
ent = PROG_TO_EDICTFPM(pr_global_struct->self);
goal = PROG_TO_EDICTFPM(ent->v.goalentity);
dist = FPM_FROMFLOAT(G_FLOAT(OFS_PARM0));
if ( !( (int)ent->v.flags & (FL_ONGROUND|FL_FLY|FL_SWIM) ) )
{
G_FLOAT(OFS_RETURN) = 0;
return;
}
// if the next step hits the enemy, return immediately
#ifdef QUAKE2
enemy = PROG_TO_EDICTFPM(ent->v.enemy);
if (enemy != svFPM.edicts && SV_CloseEnoughFPM (ent, enemy, dist) )
#else
if ( PROG_TO_EDICTFPM(ent->v.enemy) != svFPM.edicts && SV_CloseEnoughFPM (ent, goal, dist) )
#endif
return;
// bump around...
if ( (rand()&3)==1 ||
!SV_StepDirectionFPM (ent, FPM_FROMFLOAT(ent->v.ideal_yaw), dist))
{
SV_NewChaseDirFPM (ent, goal, dist);
}
}
#endif //USEFPM