Portable Half-Life SDK. GoldSource and Xash3D. Crossplatform.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

412 lines
9.6 KiB

9 years ago
/***
*
* Copyright (c) 1996-2002, Valve LLC. All rights reserved.
*
* This product contains software technology licensed from Id
* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc.
* All Rights Reserved.
*
* Use, distribution, and modification of this source code and/or resulting
* object code is restricted to non-commercial enhancements to products from
* Valve LLC. All other use, distribution, or modification is prohibited
* without written permission from Valve LLC.
*
****/
//
// ========================== PATH_CORNER ===========================
//
#include "extdll.h"
#include "util.h"
#include "cbase.h"
#include "trains.h"
#include "saverestore.h"
class CPathCorner : public CPointEntity
{
public:
9 years ago
void Spawn();
9 years ago
void KeyValue( KeyValueData* pkvd );
float GetDelay( void ) { return m_flWait; }
9 years ago
//void Touch( CBaseEntity *pOther );
virtual int Save( CSave &save );
virtual int Restore( CRestore &restore );
static TYPEDESCRIPTION m_SaveData[];
9 years ago
private:
9 years ago
float m_flWait;
9 years ago
};
LINK_ENTITY_TO_CLASS( path_corner, CPathCorner )
9 years ago
// Global Savedata for Delay
TYPEDESCRIPTION CPathCorner::m_SaveData[] =
9 years ago
{
DEFINE_FIELD( CPathCorner, m_flWait, FIELD_FLOAT ),
};
IMPLEMENT_SAVERESTORE( CPathCorner, CPointEntity )
9 years ago
//
// Cache user-entity-field values until spawn is called.
//
9 years ago
void CPathCorner::KeyValue( KeyValueData *pkvd )
9 years ago
{
9 years ago
if( FStrEq( pkvd->szKeyName, "wait" ) )
9 years ago
{
9 years ago
m_flWait = atof( pkvd->szValue );
9 years ago
pkvd->fHandled = TRUE;
}
else
CPointEntity::KeyValue( pkvd );
}
9 years ago
void CPathCorner::Spawn()
9 years ago
{
9 years ago
ASSERTSZ( !FStringNull( pev->targetname ), "path_corner without a targetname" );
9 years ago
}
#if 0
9 years ago
void CPathCorner::Touch( CBaseEntity *pOther )
9 years ago
{
9 years ago
entvars_t *pevToucher = pOther->pev;
9 years ago
9 years ago
if( FBitSet( pevToucher->flags, FL_MONSTER ) )
{
// monsters don't navigate path corners based on touch anymore
9 years ago
return;
}
// If OTHER isn't explicitly looking for this path_corner, bail out
9 years ago
if( pOther->m_pGoalEnt != this )
9 years ago
{
return;
}
// If OTHER has an enemy, this touch is incidental, ignore
9 years ago
if( !FNullEnt( pevToucher->enemy ) )
9 years ago
{
return; // fighting, not following a path
}
// UNDONE: support non-zero flWait
/*
9 years ago
if( m_flWait != 0 )
ALERT( at_warning, "Non-zero path-cornder waits NYI" );
9 years ago
*/
// Find the next "stop" on the path, make it the goal of the "toucher".
9 years ago
if( FStringNull( pev->target ) )
9 years ago
{
9 years ago
ALERT( at_warning, "PathCornerTouch: no next stop specified" );
9 years ago
}
9 years ago
pOther->m_pGoalEnt = CBaseEntity::Instance( FIND_ENTITY_BY_TARGETNAME( NULL, STRING( pev->target ) ) );
9 years ago
// If "next spot" was not found (does not exist - level design error)
9 years ago
if( !pOther->m_pGoalEnt )
9 years ago
{
9 years ago
ALERT( at_console, "PathCornerTouch--%s couldn't find next stop in path: %s", STRING( pev->classname ), STRING( pev->target ) );
9 years ago
return;
}
// Turn towards the next stop in the path.
9 years ago
pevToucher->ideal_yaw = UTIL_VecToYaw( pOther->m_pGoalEnt->pev->origin - pevToucher->origin );
9 years ago
}
#endif
TYPEDESCRIPTION CPathTrack::m_SaveData[] =
9 years ago
{
DEFINE_FIELD( CPathTrack, m_length, FIELD_FLOAT ),
DEFINE_FIELD( CPathTrack, m_pnext, FIELD_CLASSPTR ),
DEFINE_FIELD( CPathTrack, m_paltpath, FIELD_CLASSPTR ),
DEFINE_FIELD( CPathTrack, m_pprevious, FIELD_CLASSPTR ),
DEFINE_FIELD( CPathTrack, m_altName, FIELD_STRING ),
};
IMPLEMENT_SAVERESTORE( CPathTrack, CBaseEntity )
LINK_ENTITY_TO_CLASS( path_track, CPathTrack )
9 years ago
//
// Cache user-entity-field values until spawn is called.
//
9 years ago
void CPathTrack::KeyValue( KeyValueData *pkvd )
9 years ago
{
9 years ago
if( FStrEq( pkvd->szKeyName, "altpath" ) )
9 years ago
{
9 years ago
m_altName = ALLOC_STRING( pkvd->szValue );
9 years ago
pkvd->fHandled = TRUE;
}
else
CPointEntity::KeyValue( pkvd );
}
9 years ago
void CPathTrack::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value )
9 years ago
{
int on;
// Use toggles between two paths
9 years ago
if( m_paltpath )
9 years ago
{
on = !FBitSet( pev->spawnflags, SF_PATH_ALTERNATE );
9 years ago
if( ShouldToggle( useType, on ) )
9 years ago
{
9 years ago
if( on )
9 years ago
SetBits( pev->spawnflags, SF_PATH_ALTERNATE );
else
ClearBits( pev->spawnflags, SF_PATH_ALTERNATE );
}
}
else // Use toggles between enabled/disabled
{
on = !FBitSet( pev->spawnflags, SF_PATH_DISABLED );
9 years ago
if( ShouldToggle( useType, on ) )
9 years ago
{
9 years ago
if( on )
9 years ago
SetBits( pev->spawnflags, SF_PATH_DISABLED );
else
ClearBits( pev->spawnflags, SF_PATH_DISABLED );
}
}
}
9 years ago
void CPathTrack::Link( void )
9 years ago
{
edict_t *pentTarget;
9 years ago
if( !FStringNull( pev->target ) )
9 years ago
{
9 years ago
pentTarget = FIND_ENTITY_BY_TARGETNAME( NULL, STRING( pev->target ) );
if( !FNullEnt(pentTarget) )
9 years ago
{
m_pnext = CPathTrack::Instance( pentTarget );
9 years ago
if( m_pnext ) // If no next pointer, this is the end of a path
9 years ago
{
m_pnext->SetPrevious( this );
}
}
else
9 years ago
ALERT( at_console, "Dead end link %s\n", STRING( pev->target ) );
9 years ago
}
// Find "alternate" path
9 years ago
if( m_altName )
9 years ago
{
9 years ago
pentTarget = FIND_ENTITY_BY_TARGETNAME( NULL, STRING( m_altName ) );
if( !FNullEnt( pentTarget ) )
9 years ago
{
m_paltpath = CPathTrack::Instance( pentTarget );
9 years ago
if( m_paltpath ) // If no next pointer, this is the end of a path
9 years ago
{
m_paltpath->SetPrevious( this );
}
}
}
}
9 years ago
void CPathTrack::Spawn( void )
9 years ago
{
pev->solid = SOLID_TRIGGER;
9 years ago
UTIL_SetSize( pev, Vector( -8, -8, -8 ), Vector( 8, 8, 8 ) );
9 years ago
m_pnext = NULL;
m_pprevious = NULL;
// DEBUGGING CODE
#if PATH_SPARKLE_DEBUG
SetThink( &Sparkle );
pev->nextthink = gpGlobals->time + 0.5;
#endif
}
void CPathTrack::Activate( void )
{
9 years ago
if( !FStringNull( pev->targetname ) ) // Link to next, and back-link
9 years ago
Link();
}
9 years ago
CPathTrack *CPathTrack::ValidPath( CPathTrack *ppath, int testFlag )
9 years ago
{
9 years ago
if( !ppath )
9 years ago
return NULL;
9 years ago
if( testFlag && FBitSet( ppath->pev->spawnflags, SF_PATH_DISABLED ) )
9 years ago
return NULL;
return ppath;
}
9 years ago
void CPathTrack::Project( CPathTrack *pstart, CPathTrack *pend, Vector *origin, float dist )
9 years ago
{
9 years ago
if( pstart && pend )
9 years ago
{
9 years ago
Vector dir = pend->pev->origin - pstart->pev->origin;
9 years ago
dir = dir.Normalize();
*origin = pend->pev->origin + dir * dist;
}
}
CPathTrack *CPathTrack::GetNext( void )
{
9 years ago
if( m_paltpath && FBitSet( pev->spawnflags, SF_PATH_ALTERNATE ) && !FBitSet( pev->spawnflags, SF_PATH_ALTREVERSE ) )
9 years ago
return m_paltpath;
9 years ago
9 years ago
return m_pnext;
}
CPathTrack *CPathTrack::GetPrevious( void )
{
9 years ago
if( m_paltpath && FBitSet( pev->spawnflags, SF_PATH_ALTERNATE ) && FBitSet( pev->spawnflags, SF_PATH_ALTREVERSE ) )
9 years ago
return m_paltpath;
9 years ago
9 years ago
return m_pprevious;
}
void CPathTrack::SetPrevious( CPathTrack *pprev )
{
// Only set previous if this isn't my alternate path
9 years ago
if( pprev && !FStrEq( STRING( pprev->pev->targetname ), STRING( m_altName ) ) )
9 years ago
m_pprevious = pprev;
}
// Assumes this is ALWAYS enabled
9 years ago
CPathTrack *CPathTrack::LookAhead( Vector *origin, float dist, int move )
9 years ago
{
CPathTrack *pcurrent;
float originalDist = dist;
9 years ago
9 years ago
pcurrent = this;
Vector currentPos = *origin;
9 years ago
if( dist < 0 ) // Travelling backwards through path
9 years ago
{
dist = -dist;
9 years ago
while( dist > 0 )
9 years ago
{
Vector dir = pcurrent->pev->origin - currentPos;
float length = dir.Length();
9 years ago
if( !length )
9 years ago
{
9 years ago
if( !ValidPath( pcurrent->GetPrevious(), move ) ) // If there is no previous node, or it's disabled, return now.
9 years ago
{
9 years ago
if( !move )
9 years ago
Project( pcurrent->GetNext(), pcurrent, origin, dist );
return NULL;
}
pcurrent = pcurrent->GetPrevious();
}
9 years ago
else if( length > dist ) // enough left in this path to move
9 years ago
{
9 years ago
*origin = currentPos + ( dir * ( dist / length ) );
9 years ago
return pcurrent;
}
else
{
dist -= length;
currentPos = pcurrent->pev->origin;
*origin = currentPos;
9 years ago
if( !ValidPath( pcurrent->GetPrevious(), move ) ) // If there is no previous node, or it's disabled, return now.
9 years ago
return NULL;
pcurrent = pcurrent->GetPrevious();
}
}
*origin = currentPos;
return pcurrent;
}
else
{
9 years ago
while( dist > 0 )
9 years ago
{
9 years ago
if( !ValidPath( pcurrent->GetNext(), move ) ) // If there is no next node, or it's disabled, return now.
9 years ago
{
9 years ago
if( !move )
9 years ago
Project( pcurrent->GetPrevious(), pcurrent, origin, dist );
return NULL;
}
Vector dir = pcurrent->GetNext()->pev->origin - currentPos;
float length = dir.Length();
9 years ago
if( !length && !ValidPath( pcurrent->GetNext()->GetNext(), move ) )
9 years ago
{
if ( dist == originalDist ) // HACK -- up against a dead end
return NULL;
return pcurrent;
}
9 years ago
if( length > dist ) // enough left in this path to move
9 years ago
{
*origin = currentPos + (dir * (dist / length));
return pcurrent;
}
else
{
dist -= length;
currentPos = pcurrent->GetNext()->pev->origin;
pcurrent = pcurrent->GetNext();
*origin = currentPos;
}
}
*origin = currentPos;
}
return pcurrent;
}
9 years ago
9 years ago
// Assumes this is ALWAYS enabled
9 years ago
CPathTrack *CPathTrack::Nearest( Vector origin )
9 years ago
{
9 years ago
int deadCount;
float minDist, dist;
Vector delta;
CPathTrack *ppath, *pnearest;
9 years ago
delta = origin - pev->origin;
delta.z = 0;
minDist = delta.Length();
pnearest = this;
ppath = GetNext();
// Hey, I could use the old 2 racing pointers solution to this, but I'm lazy :)
deadCount = 0;
9 years ago
while( ppath && ppath != this )
9 years ago
{
deadCount++;
9 years ago
if( deadCount > 9999 )
9 years ago
{
9 years ago
ALERT( at_error, "Bad sequence of path_tracks from %s", STRING( pev->targetname ) );
9 years ago
return NULL;
}
delta = origin - ppath->pev->origin;
delta.z = 0;
dist = delta.Length();
9 years ago
if( dist < minDist )
9 years ago
{
minDist = dist;
pnearest = ppath;
}
ppath = ppath->GetNext();
}
return pnearest;
}
CPathTrack *CPathTrack::Instance( edict_t *pent )
9 years ago
{
if( FClassnameIs( pent, "path_track" ) )
return (CPathTrack *)GET_PRIVATE( pent );
9 years ago
return NULL;
}
// DEBUGGING CODE
9 years ago
#if PATH_SPARKLE_DEBUG
9 years ago
void CPathTrack::Sparkle( void )
9 years ago
{
pev->nextthink = gpGlobals->time + 0.2;
9 years ago
if( FBitSet( pev->spawnflags, SF_PATH_DISABLED ) )
UTIL_ParticleEffect( pev->origin, Vector( 0, 0,100 ), 210, 10 );
9 years ago
else
9 years ago
UTIL_ParticleEffect( pev->origin, Vector( 0, 0, 100 ), 84, 10 );
9 years ago
}
#endif