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.

3738 lines
101 KiB

7 years ago
/***
*
9 years ago
* Copyright (c) 1996-2002, Valve LLC. All rights reserved.
7 years ago
*
* This product contains software technology licensed from Id
* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc.
* All Rights Reserved.
*
* This source code contains proprietary and confidential information of
* Valve LLC and its suppliers. Access to this code is restricted to
* persons who have executed a written SDK license with Valve. Any access,
* use or distribution of this code by or to any unlicensed person is illegal.
*
****/
//=========================================================
// nodes.cpp - AI node tree stuff.
//=========================================================
#include <sys/stat.h>
#include <sys/types.h>
#include "extdll.h"
#include "util.h"
#include "cbase.h"
#include "monsters.h"
#include "nodes.h"
#include "nodes_compat.h"
7 years ago
#include "animation.h"
#include "doors.h"
#define HULL_STEP_SIZE 16// how far the test hull moves on each step
#define NODE_HEIGHT 8 // how high to lift nodes off the ground after we drop them all (make stair/ramp mapping easier)
// to help eliminate node clutter by level designers, this is used to cap how many other nodes
// any given node is allowed to 'see' in the first stage of graph creation "LinkVisibleNodes()".
#define MAX_NODE_INITIAL_LINKS 128
#define MAX_NODES 1024
8 years ago
extern DLL_GLOBAL edict_t *g_pBodyQueueHead;
7 years ago
8 years ago
Vector VecBModelOrigin( entvars_t *pevBModel );
7 years ago
8 years ago
CGraph WorldGraph;
7 years ago
LINK_ENTITY_TO_CLASS( info_node, CNodeEnt )
LINK_ENTITY_TO_CLASS( info_node_air, CNodeEnt )
9 years ago
4 years ago
#if __DOS__
#include <direct.h>
#define CreateDirectoryA(p, n) mkdir(p)
3 years ago
#elif !_WIN32
7 years ago
#include <unistd.h>
9 years ago
#include <sys/stat.h>
#define CreateDirectoryA(p, n) mkdir(p, 0777)
7 years ago
#endif
8 years ago
7 years ago
//=========================================================
// CGraph - InitGraph - prepares the graph for use. Frees any
// memory currently in use by the world graph, NULLs
// all pointers, and zeros the node count.
//=========================================================
8 years ago
void CGraph::InitGraph( void )
7 years ago
{
// Make the graph unavailable
//
m_fGraphPresent = FALSE;
m_fGraphPointersSet = FALSE;
m_fRoutingComplete = FALSE;
// Free the link pool
//
8 years ago
if( m_pLinkPool )
7 years ago
{
8 years ago
free( m_pLinkPool );
7 years ago
m_pLinkPool = NULL;
}
8 years ago
7 years ago
// Free the node info
//
8 years ago
if( m_pNodes )
7 years ago
{
8 years ago
free( m_pNodes );
7 years ago
m_pNodes = NULL;
}
8 years ago
if( m_di )
7 years ago
{
8 years ago
free( m_di );
7 years ago
m_di = NULL;
}
// Free the routing info.
//
8 years ago
if( m_pRouteInfo )
7 years ago
{
8 years ago
free( m_pRouteInfo );
7 years ago
m_pRouteInfo = NULL;
}
8 years ago
if( m_pHashLinks )
7 years ago
{
8 years ago
free( m_pHashLinks );
7 years ago
m_pHashLinks = NULL;
}
// Zero node and link counts
//
m_cNodes = 0;
m_cLinks = 0;
m_nRouteInfo = 0;
m_iLastActiveIdleSearch = 0;
m_iLastCoverSearch = 0;
}
//=========================================================
// CGraph - AllocNodes - temporary function that mallocs a
// reasonable number of nodes so we can build the path which
// will be saved to disk.
//=========================================================
8 years ago
int CGraph::AllocNodes( void )
7 years ago
{
// malloc all of the nodes
8 years ago
WorldGraph.m_pNodes = (CNode *)calloc( sizeof(CNode), MAX_NODES );
7 years ago
// could not malloc space for all the nodes!
8 years ago
if( !WorldGraph.m_pNodes )
7 years ago
{
8 years ago
ALERT( at_aiconsole, "**ERROR**\nCouldn't malloc %d nodes!\n", WorldGraph.m_cNodes );
7 years ago
return FALSE;
}
return TRUE;
}
//=========================================================
// CGraph - LinkEntForLink - sometimes the ent that blocks
// a path is a usable door, in which case the monster just
// needs to face the door and fire it. In other cases, the
// monster needs to operate a button or lever to get the
// door to open. This function will return a pointer to the
// button if the monster needs to hit a button to open the
// door, or returns a pointer to the door if the monster
// need only use the door.
//
// pNode is the node the monster will be standing on when it
// will need to stop and trigger the ent.
//=========================================================
8 years ago
entvars_t *CGraph::LinkEntForLink( CLink *pLink, CNode *pNode )
7 years ago
{
CBaseEntity *pSearch;
CBaseEntity *pTrigger;
8 years ago
entvars_t *pevTrigger;
entvars_t *pevLinkEnt;
TraceResult tr;
7 years ago
pevLinkEnt = pLink->m_pLinkEnt;
8 years ago
if( !pevLinkEnt )
7 years ago
return NULL;
pSearch = NULL;// start search at the top of the ent list.
8 years ago
if( FClassnameIs( pevLinkEnt, "func_door" ) || FClassnameIs( pevLinkEnt, "func_door_rotating" ) )
9 years ago
{
7 years ago
///!!!UNDONE - check for TOGGLE or STAY open doors here. If a door is in the way, and is
// TOGGLE or STAY OPEN, even monsters that can't open doors can go that way.
8 years ago
if( ( pevLinkEnt->spawnflags & SF_DOOR_USE_ONLY ) )
{
// door is use only, so the door is all the monster has to worry about
7 years ago
return pevLinkEnt;
}
8 years ago
while( 1 )
7 years ago
{
pTrigger = UTIL_FindEntityByTarget ( pSearch, STRING( pevLinkEnt->targetname ) );// find the button or trigger
if ( !pTrigger )
{// no trigger found
// right now this is a problem among auto-open doors, or any door that opens through the use
// of a trigger brush. Trigger brushes have no models, and don't show up in searches. Just allow
// monsters to open these sorts of doors for now.
return pevLinkEnt;
}
8 years ago
7 years ago
pSearch = pTrigger;
pevTrigger = pTrigger->pev;
8 years ago
if( FClassnameIs( pevTrigger, "func_button" ) || FClassnameIs( pevTrigger, "func_rot_button" ) )
{
8 years ago
// only buttons are handled right now.
7 years ago
// trace from the node to the trigger, make sure it's one we can see from the node.
// !!!HACKHACK Use bodyqueue here cause there are no ents we really wish to ignore!
8 years ago
UTIL_TraceLine( pNode->m_vecOrigin, VecBModelOrigin( pevTrigger ), ignore_monsters, g_pBodyQueueHead, &tr );
7 years ago
8 years ago
if( VARS(tr.pHit) == pevTrigger )
{
// good to go!
7 years ago
return VARS( tr.pHit );
}
}
}
}
else
{
8 years ago
ALERT( at_aiconsole, "Unsupported PathEnt:\n'%s'\n", STRING( pevLinkEnt->classname ) );
7 years ago
return NULL;
}
}
//=========================================================
// CGraph - HandleLinkEnt - a brush ent is between two
// nodes that would otherwise be able to see each other.
// Given the monster's capability, determine whether
// or not the monster can go this way.
//=========================================================
8 years ago
int CGraph::HandleLinkEnt( int iNode, entvars_t *pevLinkEnt, int afCapMask, NODEQUERY queryType )
7 years ago
{
edict_t *pentWorld;
//edict_t *pentWorld;
8 years ago
CBaseEntity *pDoor;
TraceResult tr;
7 years ago
8 years ago
if( !m_fGraphPresent || !m_fGraphPointersSet )
{
// protect us in the case that the node graph isn't available
8 years ago
ALERT( at_aiconsole, "Graph not ready!\n" );
7 years ago
return FALSE;
}
8 years ago
if( FNullEnt( pevLinkEnt ) )
7 years ago
{
8 years ago
ALERT( at_aiconsole, "dead path ent!\n" );
7 years ago
return TRUE;
}
//pentWorld = NULL;
7 years ago
// func_door
8 years ago
if( FClassnameIs( pevLinkEnt, "func_door" ) || FClassnameIs( pevLinkEnt, "func_door_rotating" ) )
{
// ent is a door.
7 years ago
pDoor = ( CBaseEntity::Instance( pevLinkEnt ) );
8 years ago
if( ( pevLinkEnt->spawnflags & SF_DOOR_USE_ONLY ) )
{
// door is use only.
8 years ago
if( ( afCapMask & bits_CAP_OPEN_DOORS ) )
{
// let monster right through if he can open doors
7 years ago
return TRUE;
}
else
{
// monster should try for it if the door is open and looks as if it will stay that way
8 years ago
if( pDoor->GetToggleState()== TS_AT_TOP && ( pevLinkEnt->spawnflags & SF_DOOR_NO_AUTO_RETURN ) )
7 years ago
{
return TRUE;
}
return FALSE;
}
}
else
{
// door must be opened with a button or trigger field.
8 years ago
7 years ago
// monster should try for it if the door is open and looks as if it will stay that way
8 years ago
if( pDoor->GetToggleState() == TS_AT_TOP && ( pevLinkEnt->spawnflags & SF_DOOR_NO_AUTO_RETURN ) )
7 years ago
{
return TRUE;
}
8 years ago
if( ( afCapMask & bits_CAP_OPEN_DOORS ) )
7 years ago
{
8 years ago
if( !( pevLinkEnt->spawnflags & SF_DOOR_NOMONSTERS ) || queryType == NODEGRAPH_STATIC )
7 years ago
return TRUE;
}
return FALSE;
}
}
8 years ago
// func_breakable
else if( FClassnameIs( pevLinkEnt, "func_breakable" ) && queryType == NODEGRAPH_STATIC )
7 years ago
{
return TRUE;
}
else
{
8 years ago
ALERT( at_aiconsole, "Unhandled Ent in Path %s\n", STRING( pevLinkEnt->classname ) );
7 years ago
return FALSE;
}
return FALSE;
}
#if 0
//=========================================================
// FindNearestLink - finds the connection (line) nearest
// the given point. Returns FALSE if fails, or TRUE if it
// has stuffed the index into the nearest link pool connection
// into the passed int pointer, and a BOOL telling whether or
// not the point is along the line into the passed BOOL pointer.
//=========================================================
8 years ago
int CGraph::FindNearestLink( const Vector &vecTestPoint, int *piNearestLink, BOOL *pfAlongLine )
7 years ago
{
8 years ago
int i, j;// loops
7 years ago
8 years ago
int iNearestLink;// index into the link pool, this is the nearest node at any time.
float flMinDist;// the distance of of the nearest case so far
float flDistToLine;// the distance of the current test case
7 years ago
8 years ago
BOOL fCurrentAlongLine;
BOOL fSuccess;
7 years ago
8 years ago
//float flConstant;// line constant
Vector vecSpot1, vecSpot2;
Vector2D vec2Spot1, vec2Spot2, vec2TestPoint;
Vector2D vec2Normal;// line normal
Vector2D vec2Line;
TraceResult tr;
7 years ago
iNearestLink = -1;// prepare for failure
fSuccess = FALSE;
flMinDist = 9999;// anything will be closer than this
// go through all of the nodes, and each node's connections
int cSkip = 0;// how many links proper pairing allowed us to skip
7 years ago
int cChecked = 0;// how many links were checked
8 years ago
for( i = 0; i < m_cNodes; i++ )
7 years ago
{
8 years ago
vecSpot1 = m_pNodes[i].m_vecOrigin;
7 years ago
8 years ago
if( m_pNodes[i].m_cNumLinks <= 0 )
{
// this shouldn't happen!
8 years ago
ALERT( at_aiconsole, "**Node %d has no links\n", i );
7 years ago
continue;
}
8 years ago
for( j = 0; j < m_pNodes[i].m_cNumLinks; j++ )
7 years ago
{
/*
!!!This optimization only works when the node graph consists of properly linked pairs.
8 years ago
if( INodeLink( i, j ) <= i )
7 years ago
{
// since we're going through the nodes in order, don't check
// any connections whose second node is lower in the list
// than the node we're currently working with. This eliminates
// redundant checks.
cSkip++;
continue;
}
*/
8 years ago
vecSpot2 = PNodeLink( i, j )->m_vecOrigin;
7 years ago
// these values need a little attention now and then, or sometimes ramps cause trouble.
8 years ago
if( fabs( vecSpot1.z - vecTestPoint.z ) > 48 && fabs( vecSpot2.z - vecTestPoint.z ) > 48 )
7 years ago
{
// if both endpoints of the line are 32 units or more above or below the monster,
// the monster won't be able to get to them, so we do a bit of trivial rejection here.
// this may change if monsters are allowed to jump down.
//
// !!!LATER: some kind of clever X/Y hashing should be used here, too
continue;
}
8 years ago
// now we have two endpoints for a line segment that we've not already checked.
// since all lines that make it this far are within -/+ 32 units of the test point's
// Z Plane, we can get away with doing the point->line check in 2d.
7 years ago
cChecked++;
vec2Spot1 = vecSpot1.Make2D();
vec2Spot2 = vecSpot2.Make2D();
vec2TestPoint = vecTestPoint.Make2D();
7 years ago
// get the line normal.
vec2Line = ( vec2Spot1 - vec2Spot2 ).Normalize();
vec2Normal.x = -vec2Line.y;
vec2Normal.y = vec2Line.x;
8 years ago
if( DotProduct( vec2Line, ( vec2TestPoint - vec2Spot1 ) ) > 0 )
{
// point outside of line
7 years ago
flDistToLine = ( vec2TestPoint - vec2Spot1 ).Length();
fCurrentAlongLine = FALSE;
}
8 years ago
else if( DotProduct( vec2Line, ( vec2TestPoint - vec2Spot2 ) ) < 0 )
{
// point outside of line
7 years ago
flDistToLine = ( vec2TestPoint - vec2Spot2 ).Length();
fCurrentAlongLine = FALSE;
}
else
{
// point inside line
8 years ago
flDistToLine = fabs( DotProduct( vec2TestPoint - vec2Spot2, vec2Normal ) );
7 years ago
fCurrentAlongLine = TRUE;
}
8 years ago
if( flDistToLine < flMinDist )
{
// just found a line nearer than any other so far
8 years ago
UTIL_TraceLine( vecTestPoint, SourceNode( i, j ).m_vecOrigin, ignore_monsters, g_pBodyQueueHead, &tr );
7 years ago
8 years ago
if( tr.flFraction != 1.0 )
{
// crap. can't see the first node of this link, try to see the other
7 years ago
UTIL_TraceLine ( vecTestPoint, DestNode( i, j ).m_vecOrigin, ignore_monsters, g_pBodyQueueHead, &tr );
8 years ago
if( tr.flFraction != 1.0 )
{
// can't use this link, cause can't see either node!
7 years ago
continue;
}
}
8 years ago
7 years ago
fSuccess = TRUE;// we know there will be something to return.
flMinDist = flDistToLine;
8 years ago
iNearestLink = m_pNodes[i].m_iFirstLink + j;
*piNearestLink = m_pNodes[i].m_iFirstLink + j;
7 years ago
*pfAlongLine = fCurrentAlongLine;
}
}
}
/*
8 years ago
if( fSuccess )
7 years ago
{
8 years ago
WRITE_BYTE( MSG_BROADCAST, SVC_TEMPENTITY );
WRITE_BYTE( MSG_BROADCAST, TE_SHOWLINE );
9 years ago
8 years ago
WRITE_COORD( MSG_BROADCAST, m_pNodes[m_pLinkPool[iNearestLink].m_iSrcNode].m_vecOrigin.x );
WRITE_COORD( MSG_BROADCAST, m_pNodes[m_pLinkPool[iNearestLink].m_iSrcNode].m_vecOrigin.y );
WRITE_COORD( MSG_BROADCAST, m_pNodes[m_pLinkPool[iNearestLink].m_iSrcNode].m_vecOrigin.z + NODE_HEIGHT );
7 years ago
8 years ago
WRITE_COORD( MSG_BROADCAST, m_pNodes[m_pLinkPool[iNearestLink].m_iDestNode].m_vecOrigin.x );
WRITE_COORD( MSG_BROADCAST, m_pNodes[m_pLinkPool[iNearestLink].m_iDestNode].m_vecOrigin.y );
WRITE_COORD( MSG_BROADCAST, m_pNodes[m_pLinkPool[iNearestLink].m_iDestNode].m_vecOrigin.z + NODE_HEIGHT );
7 years ago
}
*/
8 years ago
ALERT( at_aiconsole, "%d Checked\n", cChecked );
7 years ago
return fSuccess;
}
#endif
8 years ago
int CGraph::HullIndex( const CBaseEntity *pEntity )
7 years ago
{
8 years ago
if( pEntity->pev->movetype == MOVETYPE_FLY )
7 years ago
return NODE_FLY_HULL;
8 years ago
if( pEntity->pev->mins == Vector( -12, -12, 0 ) )
7 years ago
return NODE_SMALL_HULL;
8 years ago
else if( pEntity->pev->mins == VEC_HUMAN_HULL_MIN )
7 years ago
return NODE_HUMAN_HULL;
8 years ago
else if( pEntity->pev->mins == Vector( -32, -32, 0 ) )
7 years ago
return NODE_LARGE_HULL;
8 years ago
//ALERT( at_aiconsole, "Unknown Hull Mins!\n" );
7 years ago
return NODE_HUMAN_HULL;
}
int CGraph::NodeType( const CBaseEntity *pEntity )
7 years ago
{
8 years ago
if( pEntity->pev->movetype == MOVETYPE_FLY )
7 years ago
{
if (pEntity->pev->waterlevel != 0 && pEntity->pev->watertype != CONTENT_FOG)
{
return bits_NODE_WATER;
}
else
{
return bits_NODE_AIR;
}
}
return bits_NODE_LAND;
}
// Sum up graph weights on the path from iStart to iDest to determine path length
float CGraph::PathLength( int iStart, int iDest, int iHull, int afCapMask )
{
8 years ago
float distance = 0;
int iNext;
7 years ago
8 years ago
int iMaxLoop = m_cNodes;
7 years ago
int iCurrentNode = iStart;
int iCap = CapIndex( afCapMask );
8 years ago
while( iCurrentNode != iDest )
7 years ago
{
8 years ago
if( iMaxLoop-- <= 0 )
7 years ago
{
9 years ago
ALERT( at_console, "Route Failure\n" );
7 years ago
return 0;
}
iNext = NextNodeInRoute( iCurrentNode, iDest, iHull, iCap );
8 years ago
if( iCurrentNode == iNext )
7 years ago
{
8 years ago
//ALERT( at_aiconsole, "SVD: Can't get there from here..\n" );
7 years ago
return 0;
}
int iLink;
8 years ago
HashSearch( iCurrentNode, iNext, iLink );
if( iLink < 0 )
7 years ago
{
8 years ago
ALERT( at_console, "HashLinks is broken from %d to %d.\n", iCurrentNode, iDest );
7 years ago
return 0;
}
8 years ago
CLink &link = Link( iLink );
7 years ago
distance += link.m_flWeight;
iCurrentNode = iNext;
}
return distance;
}
// Parse the routing table at iCurrentNode for the next node on the shortest path to iDest
int CGraph::NextNodeInRoute( int iCurrentNode, int iDest, int iHull, int iCap )
{
int iNext = iCurrentNode;
8 years ago
int nCount = iDest + 1;
signed char *pRoute = m_pRouteInfo + m_pNodes[iCurrentNode].m_pNextBestNode[iHull][iCap];
7 years ago
// Until we decode the next best node
//
8 years ago
while( nCount > 0 )
7 years ago
{
signed char ch = *pRoute++;
8 years ago
//ALERT( at_aiconsole, "C(%d)", ch );
if( ch < 0 )
7 years ago
{
// Sequence phrase
//
ch = -ch;
8 years ago
if( nCount <= ch )
7 years ago
{
iNext = iDest;
nCount = 0;
8 years ago
//ALERT( at_aiconsole, "SEQ: iNext/iDest=%d\n", iNext );
7 years ago
}
else
{
8 years ago
//ALERT( at_aiconsole, "SEQ: nCount + ch (%d + %d)\n", nCount, ch );
7 years ago
nCount = nCount - ch;
}
}
else
{
8 years ago
//ALERT( at_aiconsole, "C(%d)", *pRoute );
7 years ago
// Repeat phrase
//
8 years ago
if( nCount <= ch + 1 )
7 years ago
{
iNext = iCurrentNode + *pRoute;
8 years ago
if( iNext >= m_cNodes )
iNext -= m_cNodes;
else if( iNext < 0 )
iNext += m_cNodes;
7 years ago
nCount = 0;
8 years ago
//ALERT( at_aiconsole, "REP: iNext=%d\n", iNext );
7 years ago
}
else
{
8 years ago
//ALERT( at_aiconsole, "REP: nCount - ch+1 (%d - %d+1)\n", nCount, ch );
7 years ago
nCount = nCount - ch - 1;
}
pRoute++;
}
}
return iNext;
}
//=========================================================
// CGraph - FindShortestPath
//
// accepts a capability mask (afCapMask), and will only
// find a path usable by a monster with those capabilities
// returns the number of nodes copied into supplied array
//=========================================================
8 years ago
int CGraph::FindShortestPath( int *piPath, int iStart, int iDest, int iHull, int afCapMask )
7 years ago
{
8 years ago
int iVisitNode;
int iCurrentNode;
int iNumPathNodes;
int iHullMask = 0;
9 years ago
8 years ago
if( !m_fGraphPresent || !m_fGraphPointersSet )
{
// protect us in the case that the node graph isn't available or built
8 years ago
ALERT( at_aiconsole, "Graph not ready!\n" );
7 years ago
return FALSE;
}
8 years ago
if( iStart < 0 || iStart > m_cNodes )
{
// The start node is bad?
8 years ago
ALERT( at_aiconsole, "Can't build a path, iStart is %d!\n", iStart );
7 years ago
return FALSE;
}
8 years ago
if( iStart == iDest )
7 years ago
{
piPath[0] = iStart;
piPath[1] = iDest;
return 2;
}
// Is routing information present.
//
8 years ago
if( m_fRoutingComplete )
7 years ago
{
int iCap = CapIndex( afCapMask );
iNumPathNodes = 0;
piPath[iNumPathNodes++] = iStart;
iCurrentNode = iStart;
int iNext;
8 years ago
//ALERT( at_aiconsole, "GOAL: %d to %d\n", iStart, iDest );
7 years ago
// Until we arrive at the destination
//
8 years ago
while( iCurrentNode != iDest )
7 years ago
{
iNext = NextNodeInRoute( iCurrentNode, iDest, iHull, iCap );
8 years ago
if( iCurrentNode == iNext )
7 years ago
{
8 years ago
//ALERT( at_aiconsole, "SVD: Can't get there from here..\n" );
7 years ago
return 0;
break;
}
8 years ago
if( iNumPathNodes >= MAX_PATH_SIZE )
7 years ago
{
8 years ago
//ALERT( at_aiconsole, "SVD: Don't return the entire path.\n" );
7 years ago
break;
}
piPath[iNumPathNodes++] = iNext;
iCurrentNode = iNext;
}
8 years ago
//ALERT( at_aiconsole, "SVD: Path with %d nodes.\n", iNumPathNodes );
7 years ago
}
else
{
9 years ago
int i;
8 years ago
CQueuePriority queue;
7 years ago
switch( iHull )
{
case NODE_SMALL_HULL:
iHullMask = bits_LINK_SMALL_HULL;
break;
case NODE_HUMAN_HULL:
iHullMask = bits_LINK_HUMAN_HULL;
break;
case NODE_LARGE_HULL:
iHullMask = bits_LINK_LARGE_HULL;
break;
case NODE_FLY_HULL:
iHullMask = bits_LINK_FLY_HULL;
break;
}
// Mark all the nodes as unvisited.
//
for ( i = 0; i < m_cNodes; i++)
{
m_pNodes[i].m_flClosestSoFar = -1.0f;
7 years ago
}
8 years ago
m_pNodes[iStart].m_flClosestSoFar = 0.0;
m_pNodes[iStart].m_iPreviousNode = iStart;// tag this as the origin node
7 years ago
queue.Insert( iStart, 0.0 );// insert start node
8 years ago
while( !queue.Empty() )
7 years ago
{
// now pull a node out of the queue
float flCurrentDistance;
8 years ago
iCurrentNode = queue.Remove( flCurrentDistance );
7 years ago
// For straight-line weights, the following Shortcut works. For arbitrary weights,
// it doesn't.
//
8 years ago
if( iCurrentNode == iDest )
break;
7 years ago
8 years ago
CNode *pCurrentNode = &m_pNodes[iCurrentNode];
8 years ago
for( i = 0; i < pCurrentNode->m_cNumLinks; i++ )
{
// run through all of this node's neighbors
8 years ago
iVisitNode = INodeLink( iCurrentNode, i );
if( ( m_pLinkPool[m_pNodes[iCurrentNode].m_iFirstLink + i].m_afLinkInfo & iHullMask ) != iHullMask )
{
// monster is too large to walk this connection
8 years ago
//ALERT( at_aiconsole, "fat ass %d/%d\n",m_pLinkPool[m_pNodes[iCurrentNode].m_iFirstLink + i].m_afLinkInfo, iMonsterHull );
7 years ago
continue;
}
// check the connection from the current node to the node we're about to mark visited and push into the queue
8 years ago
if( m_pLinkPool[m_pNodes[iCurrentNode].m_iFirstLink + i].m_pLinkEnt != NULL )
{
// there's a brush ent in the way! Don't mark this node or put it into the queue unless the monster can negotiate it
8 years ago
if( !HandleLinkEnt( iCurrentNode, m_pLinkPool[m_pNodes[iCurrentNode].m_iFirstLink + i].m_pLinkEnt, afCapMask, NODEGRAPH_STATIC ) )
{
// monster should not try to go this way.
7 years ago
continue;
}
}
8 years ago
float flOurDistance = flCurrentDistance + m_pLinkPool[m_pNodes[iCurrentNode].m_iFirstLink + i].m_flWeight;
if( m_pNodes[iVisitNode].m_flClosestSoFar < -0.5f
|| flOurDistance < m_pNodes[iVisitNode].m_flClosestSoFar - 0.001f )
7 years ago
{
m_pNodes[iVisitNode].m_flClosestSoFar = flOurDistance;
m_pNodes[iVisitNode].m_iPreviousNode = iCurrentNode;
8 years ago
queue.Insert( iVisitNode, flOurDistance );
7 years ago
}
}
}
if( m_pNodes[iDest].m_flClosestSoFar < -0.5f )
{
// Destination is unreachable, no path found.
7 years ago
return 0;
}
// the queue is not empty
7 years ago
// now we must walk backwards through the m_iPreviousNode field, and count how many connections there are in the path
iCurrentNode = iDest;
iNumPathNodes = 1;// count the dest
8 years ago
while( iCurrentNode != iStart )
7 years ago
{
iNumPathNodes++;
8 years ago
iCurrentNode = m_pNodes[iCurrentNode].m_iPreviousNode;
7 years ago
}
iCurrentNode = iDest;
8 years ago
for( i = iNumPathNodes - 1; i >= 0; i-- )
7 years ago
{
8 years ago
piPath[i] = iCurrentNode;
iCurrentNode = m_pNodes[iCurrentNode].m_iPreviousNode;
7 years ago
}
}
#if 0
8 years ago
if( m_fRoutingComplete )
7 years ago
{
// This will draw the entire path that was generated for the monster.
8 years ago
for( int i = 0; i < iNumPathNodes - 1; i++ )
7 years ago
{
MESSAGE_BEGIN( MSG_BROADCAST, SVC_TEMPENTITY );
8 years ago
WRITE_BYTE( TE_SHOWLINE );
7 years ago
8 years ago
WRITE_COORD( m_pNodes[piPath[i]].m_vecOrigin.x );
WRITE_COORD( m_pNodes[piPath[i]].m_vecOrigin.y );
WRITE_COORD( m_pNodes[piPath[i]].m_vecOrigin.z + NODE_HEIGHT );
9 years ago
8 years ago
WRITE_COORD( m_pNodes[piPath[i + 1]].m_vecOrigin.x );
WRITE_COORD( m_pNodes[piPath[i + 1]].m_vecOrigin.y );
WRITE_COORD( m_pNodes[piPath[i + 1]].m_vecOrigin.z + NODE_HEIGHT );
7 years ago
MESSAGE_END();
}
}
#endif
#if 0 // MAZE map
MESSAGE_BEGIN( MSG_BROADCAST, SVC_TEMPENTITY );
8 years ago
WRITE_BYTE( TE_SHOWLINE );
WRITE_COORD( m_pNodes[4].m_vecOrigin.x );
WRITE_COORD( m_pNodes[4].m_vecOrigin.y );
WRITE_COORD( m_pNodes[4].m_vecOrigin.z + NODE_HEIGHT );
WRITE_COORD( m_pNodes[9].m_vecOrigin.x );
WRITE_COORD( m_pNodes[9].m_vecOrigin.y );
WRITE_COORD( m_pNodes[9].m_vecOrigin.z + NODE_HEIGHT );
7 years ago
MESSAGE_END();
#endif
return iNumPathNodes;
}
inline ULONG Hash( void *p, int len )
7 years ago
{
CRC32_t ulCrc;
CRC32_INIT( &ulCrc );
CRC32_PROCESS_BUFFER( &ulCrc, p, len );
return CRC32_FINAL( ulCrc );
7 years ago
}
void inline CalcBounds( int &Lower, int &Upper, int Goal, int Best )
7 years ago
{
int Temp = 2 * Goal - Best;
8 years ago
if( Best > Goal )
{
Lower = Q_max( 0, Temp );
Upper = Best;
}
else
{
Upper = Q_min( 255, Temp );
Lower = Best;
}
7 years ago
}
// Convert from [-8192,8192] to [0, 255]
//
inline int CALC_RANGE( int x, int lower, int upper )
7 years ago
{
return NUM_RANGES * ( x - lower ) / ( ( upper - lower + 1 ) );
7 years ago
}
8 years ago
void inline UpdateRange( int &minValue, int &maxValue, int Goal, int Best )
7 years ago
{
int Lower, Upper;
CalcBounds( Lower, Upper, Goal, Best );
8 years ago
if( Upper < maxValue )
maxValue = Upper;
if( minValue < Lower )
minValue = Lower;
7 years ago
}
void CGraph::CheckNode( Vector vecOrigin, int iNode )
7 years ago
{
// Have we already seen this point before?.
//
if( m_di[iNode].m_CheckedEvent == m_CheckedCounter )
return;
9 years ago
m_di[iNode].m_CheckedEvent = m_CheckedCounter;
7 years ago
float flDist = ( vecOrigin - m_pNodes[iNode].m_vecOriginPeek ).Length();
7 years ago
if( flDist < m_flShortest )
7 years ago
{
TraceResult tr;
// make sure that vecOrigin can trace to this node!
8 years ago
UTIL_TraceLine( vecOrigin, m_pNodes[iNode].m_vecOriginPeek, ignore_monsters, 0, &tr );
7 years ago
if( tr.flFraction == 1.0f )
7 years ago
{
m_iNearest = iNode;
m_flShortest = flDist;
UpdateRange( m_minX, m_maxX, CALC_RANGE( vecOrigin.x, m_RegionMin[0], m_RegionMax[0] ), m_pNodes[iNode].m_Region[0] );
UpdateRange( m_minY, m_maxY, CALC_RANGE( vecOrigin.y, m_RegionMin[1], m_RegionMax[1] ), m_pNodes[iNode].m_Region[1] );
UpdateRange( m_minZ, m_maxZ, CALC_RANGE( vecOrigin.z, m_RegionMin[2], m_RegionMax[2] ), m_pNodes[iNode].m_Region[2] );
7 years ago
// From maxCircle, calculate maximum bounds box. All points must be
// simultaneously inside all bounds of the box.
//
8 years ago
m_minBoxX = CALC_RANGE( vecOrigin.x - flDist, m_RegionMin[0], m_RegionMax[0] );
m_maxBoxX = CALC_RANGE( vecOrigin.x + flDist, m_RegionMin[0], m_RegionMax[0] );
m_minBoxY = CALC_RANGE( vecOrigin.y - flDist, m_RegionMin[1], m_RegionMax[1] );
m_maxBoxY = CALC_RANGE( vecOrigin.y + flDist, m_RegionMin[1], m_RegionMax[1] );
m_minBoxZ = CALC_RANGE( vecOrigin.z - flDist, m_RegionMin[2], m_RegionMax[2] );
m_maxBoxZ = CALC_RANGE( vecOrigin.z + flDist, m_RegionMin[2], m_RegionMax[2] );
7 years ago
}
}
}
//=========================================================
// CGraph - FindNearestNode - returns the index of the node nearest
// the given vector -1 is failure (couldn't find a valid
// near node )
//=========================================================
8 years ago
int CGraph::FindNearestNode( const Vector &vecOrigin, CBaseEntity *pEntity )
7 years ago
{
return FindNearestNode( vecOrigin, NodeType( pEntity ) );
}
8 years ago
int CGraph::FindNearestNode( const Vector &vecOrigin, int afNodeTypes )
7 years ago
{
8 years ago
int i;
7 years ago
TraceResult tr;
if( !m_fGraphPresent || !m_fGraphPointersSet )
{
// protect us in the case that the node graph isn't available
8 years ago
ALERT( at_aiconsole, "Graph not ready!\n" );
7 years ago
return -1;
}
// Check with the cache
//
8 years ago
ULONG iHash = ( CACHE_SIZE - 1 ) & Hash( (void *)(const float *)vecOrigin, sizeof(vecOrigin) );
if( m_Cache[iHash].v == vecOrigin )
7 years ago
{
8 years ago
//ALERT( at_aiconsole, "Cache Hit.\n" );
7 years ago
return m_Cache[iHash].n;
}
/* else
7 years ago
{
8 years ago
//ALERT( at_aiconsole, "Cache Miss.\n" );
7 years ago
}
*/
7 years ago
// Mark all points as unchecked.
//
m_CheckedCounter++;
if( m_CheckedCounter == 0 )
7 years ago
{
for( i = 0; i < m_cNodes; i++ )
{
m_di[i].m_CheckedEvent = 0;
}
m_CheckedCounter++;
}
m_iNearest = -1;
m_flShortest = 999999.0f; // just a big number.
7 years ago
// If we can find a visible point, then let CalcBounds set the limits, but if
// we have no visible point at all to start with, then don't restrict the limits.
//
#if 1
m_minX = 0; m_maxX = 255;
m_minY = 0; m_maxY = 255;
m_minZ = 0; m_maxZ = 255;
m_minBoxX = 0; m_maxBoxX = 255;
m_minBoxY = 0; m_maxBoxY = 255;
m_minBoxZ = 0; m_maxBoxZ = 255;
#else
8 years ago
m_minBoxX = CALC_RANGE( vecOrigin.x - flDist, m_RegionMin[0], m_RegionMax[0] );
m_maxBoxX = CALC_RANGE( vecOrigin.x + flDist, m_RegionMin[0], m_RegionMax[0] );
m_minBoxY = CALC_RANGE( vecOrigin.y - flDist, m_RegionMin[1], m_RegionMax[1] );
m_maxBoxY = CALC_RANGE( vecOrigin.y + flDist, m_RegionMin[1], m_RegionMax[1] );
m_minBoxZ = CALC_RANGE( vecOrigin.z - flDist, m_RegionMin[2], m_RegionMax[2] );
m_maxBoxZ = CALC_RANGE( vecOrigin.z + flDist, m_RegionMin[2], m_RegionMax[2] );
CalcBounds( m_minX, m_maxX, CALC_RANGE( vecOrigin.x, m_RegionMin[0], m_RegionMax[0] ), m_pNodes[m_iNearest].m_Region[0] );
CalcBounds( m_minY, m_maxY, CALC_RANGE( vecOrigin.y, m_RegionMin[1], m_RegionMax[1] ), m_pNodes[m_iNearest].m_Region[1] );
CalcBounds( m_minZ, m_maxZ, CALC_RANGE( vecOrigin.z, m_RegionMin[2], m_RegionMax[2] ), m_pNodes[m_iNearest].m_Region[2] );
7 years ago
#endif
8 years ago
int halfX = ( m_minX + m_maxX ) / 2;
int halfY = ( m_minY + m_maxY ) / 2;
int halfZ = ( m_minZ + m_maxZ ) / 2;
7 years ago
int j;
7 years ago
for( i = halfX; i >= m_minX; i-- )
{
for( j = m_RangeStart[0][i]; j <= m_RangeEnd[0][i]; j++ )
{
8 years ago
if( !( m_pNodes[m_di[j].m_SortedBy[0]].m_afNodeInfo & afNodeTypes ) )
continue;
7 years ago
int rgY = m_pNodes[m_di[j].m_SortedBy[0]].m_Region[1];
if( rgY > m_maxBoxY )
break;
if( rgY < m_minBoxY )
continue;
7 years ago
int rgZ = m_pNodes[m_di[j].m_SortedBy[0]].m_Region[2];
if( rgZ < m_minBoxZ )
continue;
9 years ago
if( rgZ > m_maxBoxZ )
continue;
CheckNode( vecOrigin, m_di[j].m_SortedBy[0] );
}
}
for( i = Q_max( m_minY, halfY + 1 ); i <= m_maxY; i++ )
{
for( j = m_RangeStart[1][i]; j <= m_RangeEnd[1][i]; j++ )
{
if( !( m_pNodes[m_di[j].m_SortedBy[1]].m_afNodeInfo & afNodeTypes ) )
continue;
7 years ago
int rgZ = m_pNodes[m_di[j].m_SortedBy[1]].m_Region[2];
if( rgZ > m_maxBoxZ )
break;
if( rgZ < m_minBoxZ )
continue;
7 years ago
int rgX = m_pNodes[m_di[j].m_SortedBy[1]].m_Region[0];
if( rgX < m_minBoxX )
continue;
9 years ago
if( rgX > m_maxBoxX )
continue;
8 years ago
CheckNode( vecOrigin, m_di[j].m_SortedBy[1] );
}
}
for( i = Q_min( m_maxZ, halfZ ); i >= m_minZ; i-- )
{
8 years ago
for( j = m_RangeStart[2][i]; j <= m_RangeEnd[2][i]; j++ )
{
if( !( m_pNodes[m_di[j].m_SortedBy[2]].m_afNodeInfo & afNodeTypes ) )
continue;
7 years ago
int rgX = m_pNodes[m_di[j].m_SortedBy[2]].m_Region[0];
if( rgX > m_maxBoxX )
break;
if( rgX < m_minBoxX )
continue;
7 years ago
int rgY = m_pNodes[m_di[j].m_SortedBy[2]].m_Region[1];
if( rgY < m_minBoxY )
continue;
if( rgY > m_maxBoxY )
continue;
8 years ago
CheckNode( vecOrigin, m_di[j].m_SortedBy[2] );
}
}
9 years ago
for( i = Q_max( m_minX, halfX + 1 ); i <= m_maxX; i++ )
8 years ago
{
for( j = m_RangeStart[0][i]; j <= m_RangeEnd[0][i]; j++ )
{
if( !( m_pNodes[m_di[j].m_SortedBy[0]].m_afNodeInfo & afNodeTypes ) )
continue;
7 years ago
int rgY = m_pNodes[m_di[j].m_SortedBy[0]].m_Region[1];
8 years ago
if( rgY > m_maxBoxY )
break;
if( rgY < m_minBoxY )
continue;
7 years ago
int rgZ = m_pNodes[m_di[j].m_SortedBy[0]].m_Region[2];
8 years ago
if( rgZ < m_minBoxZ )
continue;
if( rgZ > m_maxBoxZ )
continue;
CheckNode( vecOrigin, m_di[j].m_SortedBy[0] );
}
}
for( i = Q_min( m_maxY, halfY ); i >= m_minY; i-- )
8 years ago
{
for( j = m_RangeStart[1][i]; j <= m_RangeEnd[1][i]; j++ )
{
if( !( m_pNodes[m_di[j].m_SortedBy[1]].m_afNodeInfo & afNodeTypes ) )
continue;
7 years ago
int rgZ = m_pNodes[m_di[j].m_SortedBy[1]].m_Region[2];
8 years ago
if( rgZ > m_maxBoxZ )
break;
if( rgZ < m_minBoxZ )
continue;
7 years ago
int rgX = m_pNodes[m_di[j].m_SortedBy[1]].m_Region[0];
8 years ago
if( rgX < m_minBoxX )
continue;
if( rgX > m_maxBoxX )
continue;
CheckNode( vecOrigin, m_di[j].m_SortedBy[1] );
}
}
for( i = Q_max( m_minZ, halfZ + 1 ); i <= m_maxZ; i++ )
8 years ago
{
for( j = m_RangeStart[2][i]; j <= m_RangeEnd[2][i]; j++ )
{
if( !( m_pNodes[m_di[j].m_SortedBy[2]].m_afNodeInfo & afNodeTypes ) )
continue;
7 years ago
int rgX = m_pNodes[m_di[j].m_SortedBy[2]].m_Region[0];
8 years ago
if( rgX > m_maxBoxX )
break;
if( rgX < m_minBoxX )
continue;
7 years ago
int rgY = m_pNodes[m_di[j].m_SortedBy[2]].m_Region[1];
8 years ago
if( rgY < m_minBoxY )
continue;
if( rgY > m_maxBoxY )
continue;
CheckNode(vecOrigin, m_di[j].m_SortedBy[2]);
}
}
7 years ago
#if 0
// Verify our answers.
//
int iNearestCheck = -1;
m_flShortest = 8192;// find nodes within this radius
8 years ago
for( i = 0; i < m_cNodes; i++ )
7 years ago
{
8 years ago
float flDist = ( vecOrigin - m_pNodes[i].m_vecOriginPeek ).Length();
7 years ago
8 years ago
if( flDist < m_flShortest )
7 years ago
{
// make sure that vecOrigin can trace to this node!
8 years ago
UTIL_TraceLine( vecOrigin, m_pNodes[i].m_vecOriginPeek, ignore_monsters, 0, &tr );
7 years ago
8 years ago
if( tr.flFraction == 1.0 )
7 years ago
{
iNearestCheck = i;
m_flShortest = flDist;
}
}
}
8 years ago
if( iNearestCheck != m_iNearest )
7 years ago
{
ALERT( at_aiconsole, "NOT closest %d(%f,%f,%f) %d(%f,%f,%f).\n",
iNearestCheck,
m_pNodes[iNearestCheck].m_vecOriginPeek.x,
m_pNodes[iNearestCheck].m_vecOriginPeek.y,
m_pNodes[iNearestCheck].m_vecOriginPeek.z,
m_iNearest,
8 years ago
( m_iNearest == -1?0.0:m_pNodes[m_iNearest].m_vecOriginPeek.x ),
( m_iNearest == -1?0.0:m_pNodes[m_iNearest].m_vecOriginPeek.y ),
( m_iNearest == -1?0.0:m_pNodes[m_iNearest].m_vecOriginPeek.z ) );
7 years ago
}
8 years ago
if( m_iNearest == -1 )
7 years ago
{
8 years ago
ALERT( at_aiconsole, "All that work for nothing.\n" );
7 years ago
}
#endif
m_Cache[iHash].v = vecOrigin;
m_Cache[iHash].n = m_iNearest;
return m_iNearest;
}
//=========================================================
// CGraph - ShowNodeConnections - draws a line from the given node
// to all connected nodes
//=========================================================
8 years ago
void CGraph::ShowNodeConnections( int iNode )
7 years ago
{
8 years ago
Vector vecSpot;
CNode *pNode;
CNode *pLinkNode;
int i;
if( !m_fGraphPresent || !m_fGraphPointersSet )
{
// protect us in the case that the node graph isn't available or built
ALERT( at_aiconsole, "Graph not ready!\n" );
7 years ago
return;
}
8 years ago
if( iNode < 0 )
7 years ago
{
ALERT( at_aiconsole, "Can't show connections for node %d\n", iNode );
return;
}
8 years ago
pNode = &m_pNodes[iNode];
7 years ago
UTIL_ParticleEffect( pNode->m_vecOrigin, g_vecZero, 255, 20 );// show node position
8 years ago
if( pNode->m_cNumLinks <= 0 )
{
// no connections!
7 years ago
ALERT ( at_aiconsole, "**No Connections!\n" );
}
8 years ago
for( i = 0; i < pNode->m_cNumLinks; i++ )
7 years ago
{
8 years ago
pLinkNode = &Node( NodeLink( iNode, i ).m_iDestNode );
7 years ago
vecSpot = pLinkNode->m_vecOrigin;
MESSAGE_BEGIN( MSG_BROADCAST, SVC_TEMPENTITY );
8 years ago
WRITE_BYTE( TE_SHOWLINE );
WRITE_COORD( m_pNodes[iNode].m_vecOrigin.x );
WRITE_COORD( m_pNodes[iNode].m_vecOrigin.y );
WRITE_COORD( m_pNodes[iNode].m_vecOrigin.z + NODE_HEIGHT );
7 years ago
WRITE_COORD( vecSpot.x );
WRITE_COORD( vecSpot.y );
WRITE_COORD( vecSpot.z + NODE_HEIGHT );
MESSAGE_END();
}
}
//=========================================================
// CGraph - LinkVisibleNodes - the first, most basic
// function of node graph creation, this connects every
// node to every other node that it can see. Expects a
// pointer to an empty connection pool and a file pointer
// to write progress to. Returns the total number of initial
// links.
//
// If there's a problem with this process, the index
// of the offending node will be written to piBadNode
//=========================================================
8 years ago
int CGraph::LinkVisibleNodes( CLink *pLinkPool, FILE *file, int *piBadNode )
7 years ago
{
8 years ago
int i, j, z;
edict_t *pTraceEnt;
int cTotalLinks, cLinksThisNode, cMaxInitialLinks;
TraceResult tr;
7 years ago
// !!!BUGBUG - this function returns 0 if there is a problem in the middle of connecting the graph
// it also returns 0 if none of the nodes in a level can see each other. piBadNode is ALWAYS read
// by BuildNodeGraph() if this function returns a 0, so make sure that it doesn't get some random
// number back.
*piBadNode = 0;
8 years ago
if( m_cNodes <= 0 )
7 years ago
{
8 years ago
ALERT( at_aiconsole, "No Nodes!\n" );
7 years ago
return FALSE;
}
// if the file pointer is bad, don't blow up, just don't write the
// file.
8 years ago
if( !file )
7 years ago
{
8 years ago
ALERT( at_aiconsole, "**LinkVisibleNodes:\ncan't write to file." );
7 years ago
}
else
{
8 years ago
fprintf( file, "----------------------------------------------------------------------------\n" );
fprintf( file, "LinkVisibleNodes - Initial Connections\n" );
fprintf( file, "----------------------------------------------------------------------------\n" );
7 years ago
}
cTotalLinks = 0;// start with no connections
8 years ago
7 years ago
// to keep track of the maximum number of initial links any node had so far.
// this lets us keep an eye on MAX_NODE_INITIAL_LINKS to ensure that we are
// being generous enough.
cMaxInitialLinks = 0;
8 years ago
for( i = 0; i < m_cNodes; i++ )
7 years ago
{
cLinksThisNode = 0;// reset this count for each node.
8 years ago
if( file )
7 years ago
{
8 years ago
fprintf( file, "Node #%4d:\n\n", i );
7 years ago
}
8 years ago
for( z = 0; z < MAX_NODE_INITIAL_LINKS; z++ )
{
// clear out the important fields in the link pool for this node
pLinkPool[cTotalLinks + z].m_iSrcNode = i;// so each link knows which node it originates from
pLinkPool[cTotalLinks + z].m_iDestNode = 0;
pLinkPool[cTotalLinks + z].m_pLinkEnt = NULL;
7 years ago
}
8 years ago
m_pNodes[i].m_iFirstLink = cTotalLinks;
7 years ago
// now build a list of every other node that this node can see
8 years ago
for( j = 0; j < m_cNodes; j++ )
7 years ago
{
8 years ago
if( j == i )
{
// don't connect to self!
7 years ago
continue;
}
#if 0
8 years ago
if( ( m_pNodes[i].m_afNodeInfo & bits_NODE_WATER ) != ( m_pNodes[j].m_afNodeInfo & bits_NODE_WATER ) )
7 years ago
{
// don't connect water nodes to air nodes or land nodes. It just wouldn't be prudent at this juncture.
continue;
}
#else
8 years ago
if( ( m_pNodes[i].m_afNodeInfo & bits_NODE_GROUP_REALM ) != ( m_pNodes[j].m_afNodeInfo & bits_NODE_GROUP_REALM ) )
7 years ago
{
// don't connect air nodes to water nodes to land nodes. It just wouldn't be prudent at this juncture.
continue;
}
#endif
tr.pHit = NULL;// clear every time so we don't get stuck with last trace's hit ent
pTraceEnt = 0;
8 years ago
UTIL_TraceLine( m_pNodes[i].m_vecOrigin,
m_pNodes[j].m_vecOrigin,
ignore_monsters,
g_pBodyQueueHead,//!!!HACKHACK no real ent to supply here, using a global we don't care about
&tr );
if( tr.fStartSolid )
7 years ago
continue;
if( tr.flFraction != 1.0f )
8 years ago
{
// trace hit a brush ent, trace backwards to make sure that this ent is the only thing in the way.
7 years ago
pTraceEnt = tr.pHit;// store the ent that the trace hit, for comparison
8 years ago
UTIL_TraceLine( m_pNodes[j].m_vecOrigin,
m_pNodes[i].m_vecOrigin,
ignore_monsters,
g_pBodyQueueHead,//!!!HACKHACK no real ent to supply here, using a global we don't care about
&tr );
7 years ago
// there is a solid_bsp ent in the way of these two nodes, so we must record several things about in order to keep
// track of it in the pathfinding code, as well as through save and restore of the node graph. ANY data that is manipulated
// as part of the process of adding a LINKENT to a connection here must also be done in CGraph::SetGraphPointers, where reloaded
// graphs are prepared for use.
8 years ago
if( tr.pHit == pTraceEnt && !FClassnameIs( tr.pHit, "worldspawn" ) )
7 years ago
{
// get a pointer
8 years ago
pLinkPool[cTotalLinks].m_pLinkEnt = VARS( tr.pHit );
7 years ago
// record the modelname, so that we can save/load node trees
8 years ago
memcpy( pLinkPool[cTotalLinks].m_szLinkEntModelname, STRING( VARS( tr.pHit )->model ), 4 );
7 years ago
// set the flag for this ent that indicates that it is attached to the world graph
// if this ent is removed from the world, it must also be removed from the connections
// that it formerly blocked.
8 years ago
if( !FBitSet( VARS( tr.pHit )->flags, FL_GRAPHED ) )
7 years ago
{
VARS( tr.pHit )->flags += FL_GRAPHED;
}
}
else
8 years ago
{
// even if the ent wasn't there, these nodes couldn't be connected. Skip.
7 years ago
continue;
}
}
8 years ago
if( file )
7 years ago
{
8 years ago
fprintf( file, "%4d", j );
7 years ago
8 years ago
if( !FNullEnt( pLinkPool[cTotalLinks].m_pLinkEnt ) )
{
// record info about the ent in the way, if any.
fprintf( file, " Entity on connection: %s, name: %s Model: %s", STRING( VARS( pTraceEnt )->classname ), STRING( VARS( pTraceEnt )->targetname ), STRING( VARS( tr.pHit )->model ) );
7 years ago
}
8 years ago
//fprintf( file, "\n", j );
fprintf( file, "\n" );
7 years ago
}
8 years ago
pLinkPool[cTotalLinks].m_iDestNode = j;
7 years ago
cLinksThisNode++;
cTotalLinks++;
// If we hit this, either a level designer is placing too many nodes in the same area, or
// we need to allow for a larger initial link pool.
8 years ago
if( cLinksThisNode == MAX_NODE_INITIAL_LINKS )
7 years ago
{
8 years ago
ALERT( at_aiconsole, "**LinkVisibleNodes:\nNode %d has NodeLinks > MAX_NODE_INITIAL_LINKS", i );
fprintf( file, "** NODE %d HAS NodeLinks > MAX_NODE_INITIAL_LINKS **\n", i );
7 years ago
*piBadNode = i;
8 years ago
return FALSE;
7 years ago
}
8 years ago
else if( cTotalLinks > MAX_NODE_INITIAL_LINKS * m_cNodes )
{
// this is paranoia
ALERT( at_aiconsole, "**LinkVisibleNodes:\nTotalLinks > MAX_NODE_INITIAL_LINKS * NUMNODES" );
7 years ago
*piBadNode = i;
8 years ago
return FALSE;
7 years ago
}
8 years ago
if( cLinksThisNode == 0 )
7 years ago
{
8 years ago
fprintf( file, "**NO INITIAL LINKS**\n" );
7 years ago
}
// record the connection info in the link pool
8 years ago
WorldGraph.m_pNodes[i].m_cNumLinks = cLinksThisNode;
7 years ago
// keep track of the most initial links ANY node had, so we can figure out
// if we have a large enough default link pool
8 years ago
if( cLinksThisNode > cMaxInitialLinks )
7 years ago
{
cMaxInitialLinks = cLinksThisNode;
}
}
8 years ago
if( file )
7 years ago
{
8 years ago
fprintf( file, "----------------------------------------------------------------------------\n" );
7 years ago
}
}
8 years ago
fprintf( file, "\n%4d Total Initial Connections - %4d Maximum connections for a single node.\n", cTotalLinks, cMaxInitialLinks );
fprintf( file, "----------------------------------------------------------------------------\n\n\n" );
7 years ago
return cTotalLinks;
}
//=========================================================
// CGraph - RejectInlineLinks - expects a pointer to a link
// pool, and a pointer to and already-open file ( if you
// want status reports written to disk ). RETURNS the number
// of connections that were rejected
//=========================================================
8 years ago
int CGraph::RejectInlineLinks( CLink *pLinkPool, FILE *file )
7 years ago
{
8 years ago
int i, j, k;
int cRejectedLinks;
7 years ago
8 years ago
BOOL fRestartLoop;// have to restart the J loop if we eliminate a link.
7 years ago
8 years ago
CNode *pSrcNode;
CNode *pCheckNode;// the node we are testing for (one of pSrcNode's connections)
CNode *pTestNode;// the node we are checking against ( also one of pSrcNode's connections)
7 years ago
8 years ago
float flDistToTestNode, flDistToCheckNode;
7 years ago
8 years ago
Vector2D vec2DirToTestNode, vec2DirToCheckNode;
7 years ago
8 years ago
if( file )
7 years ago
{
8 years ago
fprintf( file, "----------------------------------------------------------------------------\n" );
fprintf( file, "InLine Rejection:\n" );
fprintf( file, "----------------------------------------------------------------------------\n" );
7 years ago
}
cRejectedLinks = 0;
8 years ago
for( i = 0; i < m_cNodes; i++ )
7 years ago
{
8 years ago
pSrcNode = &m_pNodes[i];
7 years ago
8 years ago
if( file )
7 years ago
{
8 years ago
fprintf( file, "Node %3d:\n", i );
7 years ago
}
8 years ago
for( j = 0; j < pSrcNode->m_cNumLinks; j++ )
7 years ago
{
8 years ago
pCheckNode = &m_pNodes[pLinkPool[pSrcNode->m_iFirstLink + j].m_iDestNode];
7 years ago
vec2DirToCheckNode = ( pCheckNode->m_vecOrigin - pSrcNode->m_vecOrigin ).Make2D();
flDistToCheckNode = vec2DirToCheckNode.Length();
vec2DirToCheckNode = vec2DirToCheckNode.Normalize();
8 years ago
pLinkPool[pSrcNode->m_iFirstLink + j].m_flWeight = flDistToCheckNode;
7 years ago
fRestartLoop = FALSE;
8 years ago
for( k = 0; k < pSrcNode->m_cNumLinks && !fRestartLoop; k++ )
7 years ago
{
8 years ago
if( k == j )
{
// don't check against same node
7 years ago
continue;
}
8 years ago
pTestNode = &m_pNodes[pLinkPool[pSrcNode->m_iFirstLink + k].m_iDestNode];
7 years ago
vec2DirToTestNode = ( pTestNode->m_vecOrigin - pSrcNode->m_vecOrigin ).Make2D();
flDistToTestNode = vec2DirToTestNode.Length();
vec2DirToTestNode = vec2DirToTestNode.Normalize();
if( DotProduct( vec2DirToCheckNode, vec2DirToTestNode ) >= 0.998f )
7 years ago
{
// there's a chance that TestNode intersects the line to CheckNode. If so, we should disconnect the link to CheckNode.
8 years ago
if( flDistToTestNode < flDistToCheckNode )
7 years ago
{
8 years ago
if( file )
7 years ago
{
fprintf( file, "REJECTED NODE %3d through Node %3d, Dot = %8f\n", pLinkPool[pSrcNode->m_iFirstLink + j].m_iDestNode, pLinkPool[pSrcNode->m_iFirstLink + k].m_iDestNode, (double)DotProduct( vec2DirToCheckNode, vec2DirToTestNode ) );
7 years ago
}
8 years ago
pLinkPool[pSrcNode->m_iFirstLink + j] = pLinkPool[pSrcNode->m_iFirstLink + ( pSrcNode->m_cNumLinks - 1 )];
7 years ago
pSrcNode->m_cNumLinks--;
j--;
cRejectedLinks++;// keeping track of how many links are cut, so that we can return that value.
fRestartLoop = TRUE;
}
}
}
}
8 years ago
if( file )
7 years ago
{
8 years ago
fprintf( file, "----------------------------------------------------------------------------\n\n" );
7 years ago
}
}
return cRejectedLinks;
}
//=========================================================
// TestHull is a modelless clip hull that verifies reachable
// nodes by walking from every node to each of it's connections
//=========================================================
class CTestHull : public CBaseMonster
{
public:
void Spawn( entvars_t *pevMasterNode );
8 years ago
virtual int ObjectCaps( void ) { return CBaseMonster :: ObjectCaps() & ~FCAP_ACROSS_TRANSITION; }
7 years ago
void EXPORT CallBuildNodeGraph ( void );
8 years ago
void BuildNodeGraph( void );
void EXPORT ShowBadNode( void );
void EXPORT DropDelay( void );
void EXPORT PathFind( void );
7 years ago
8 years ago
Vector vecBadNodeOrigin;
7 years ago
};
LINK_ENTITY_TO_CLASS( testhull, CTestHull )
7 years ago
//=========================================================
// CTestHull::Spawn
//=========================================================
8 years ago
void CTestHull::Spawn( entvars_t *pevMasterNode )
7 years ago
{
8 years ago
SET_MODEL( ENT( pev ), "models/player.mdl" );
UTIL_SetSize( pev, VEC_HUMAN_HULL_MIN, VEC_HUMAN_HULL_MAX );
7 years ago
8 years ago
pev->solid = SOLID_SLIDEBOX;
pev->movetype = MOVETYPE_STEP;
pev->effects = 0;
pev->health = 50;
pev->yaw_speed = 8;
7 years ago
8 years ago
if( WorldGraph.m_fGraphPresent )
7 years ago
{// graph loaded from disk, so we don't need the test hull
SetThink(&CTestHull :: SUB_Remove );
SetNextThink( 0 );
}
else
{
9 years ago
SetThink( &CTestHull::DropDelay );
SetNextThink( 1.0f );
7 years ago
}
// Make this invisible
// UNDONE: Shouldn't we just use EF_NODRAW? This doesn't need to go to the client.
pev->rendermode = kRenderTransTexture;
pev->renderamt = 0;
}
//=========================================================
// TestHull::DropDelay - spawns TestHull on top of
// the 0th node and drops it to the ground.
//=========================================================
8 years ago
void CTestHull::DropDelay( void )
7 years ago
{
UTIL_CenterPrintAll( "Node Graph out of Date. Rebuilding..." );
UTIL_SetOrigin ( this, WorldGraph.m_pNodes[ 0 ].m_vecOrigin );
9 years ago
SetThink( &CTestHull::CallBuildNodeGraph );
7 years ago
SetNextThink( 1.0f );
7 years ago
}
//=========================================================
// nodes start out as ents in the world. As they are spawned,
// the node info is recorded then the ents are discarded.
//=========================================================
8 years ago
void CNodeEnt::KeyValue( KeyValueData *pkvd )
7 years ago
{
8 years ago
if( FStrEq( pkvd->szKeyName, "hinttype" ) )
7 years ago
{
m_sHintType = (short)atoi( pkvd->szValue );
pkvd->fHandled = TRUE;
}
8 years ago
if( FStrEq( pkvd->szKeyName, "activity" ) )
7 years ago
{
m_sHintActivity = (short)atoi( pkvd->szValue );
pkvd->fHandled = TRUE;
}
else
CBaseEntity::KeyValue( pkvd );
}
//=========================================================
//=========================================================
8 years ago
void CNodeEnt::Spawn( void )
7 years ago
{
pev->movetype = MOVETYPE_NONE;
pev->solid = SOLID_NOT;// always solid_not
8 years ago
if( WorldGraph.m_fGraphPresent )
{
// graph loaded from disk, so discard all these node ents as soon as they spawn
7 years ago
REMOVE_ENTITY( edict() );
return;
}
8 years ago
if( WorldGraph.m_cNodes == 0 )
{
// this is the first node to spawn, spawn the test hull entity that builds and walks the node tree
CTestHull *pHull = GetClassPtr( (CTestHull *)NULL );
7 years ago
pHull->Spawn( pev );
}
8 years ago
if( WorldGraph.m_cNodes >= MAX_NODES )
7 years ago
{
8 years ago
ALERT( at_aiconsole, "cNodes > MAX_NODES\n" );
7 years ago
return;
}
8 years ago
WorldGraph.m_pNodes[WorldGraph.m_cNodes].m_vecOriginPeek =
WorldGraph.m_pNodes[WorldGraph.m_cNodes].m_vecOrigin = pev->origin;
WorldGraph.m_pNodes[WorldGraph.m_cNodes].m_flHintYaw = pev->angles.y;
WorldGraph.m_pNodes[WorldGraph.m_cNodes].m_sHintType = m_sHintType;
WorldGraph.m_pNodes[WorldGraph.m_cNodes].m_sHintActivity = m_sHintActivity;
7 years ago
8 years ago
if( FClassnameIs( pev, "info_node_air" ) )
WorldGraph.m_pNodes[WorldGraph.m_cNodes].m_afNodeInfo = bits_NODE_AIR;
7 years ago
else
8 years ago
WorldGraph.m_pNodes[WorldGraph.m_cNodes].m_afNodeInfo = 0;
7 years ago
WorldGraph.m_cNodes++;
REMOVE_ENTITY( edict() );
}
//=========================================================
// CTestHull - ShowBadNode - makes a bad node fizzle. When
// there's a problem with node graph generation, the test
// hull will be placed up the bad node's location and will generate
// particles
//=========================================================
8 years ago
void CTestHull::ShowBadNode( void )
7 years ago
{
pev->movetype = MOVETYPE_FLY;
pev->angles.y = pev->angles.y + 4;
8 years ago
UTIL_MakeVectors( pev->angles );
7 years ago
8 years ago
UTIL_ParticleEffect( pev->origin, g_vecZero, 255, 25 );
UTIL_ParticleEffect( pev->origin + gpGlobals->v_forward * 64, g_vecZero, 255, 25 );
UTIL_ParticleEffect( pev->origin - gpGlobals->v_forward * 64, g_vecZero, 255, 25 );
UTIL_ParticleEffect( pev->origin + gpGlobals->v_right * 64, g_vecZero, 255, 25 );
UTIL_ParticleEffect( pev->origin - gpGlobals->v_right * 64, g_vecZero, 255, 25 );
7 years ago
SetNextThink( 0.1f );
7 years ago
}
extern BOOL gTouchDisabled;
7 years ago
void CTestHull::CallBuildNodeGraph( void )
{
// TOUCH HACK -- Don't allow this entity to call anyone's "touch" function
gTouchDisabled = TRUE;
BuildNodeGraph();
gTouchDisabled = FALSE;
// Undo TOUCH HACK
}
//=========================================================
// BuildNodeGraph - think function called by the empty walk
// hull that is spawned by the first node to spawn. This
// function links all nodes that can see each other, then
// eliminates all inline links, then uses a monster-sized
// hull that walks between each node and each of its links
// to ensure that a monster can actually fit through the space
//=========================================================
8 years ago
void CTestHull::BuildNodeGraph( void )
7 years ago
{
8 years ago
//TraceResult tr;
FILE *file;
7 years ago
8 years ago
char szNrpFilename [MAX_PATH];// text node report filename
7 years ago
8 years ago
CLink *pTempPool; // temporary link pool
7 years ago
8 years ago
CNode *pSrcNode;// node we're currently working with
CNode *pDestNode;// the other node in comparison operations
7 years ago
8 years ago
BOOL fSkipRemainingHulls;//if smallest hull can't fit, don't check any others
BOOL fPairsValid;// are all links in the graph evenly paired?
7 years ago
8 years ago
int i, j, hull;
7 years ago
8 years ago
int iBadNode;// this is the node that caused graph generation to fail
7 years ago
//int cMaxInitialLinks = 0;
//int cMaxValidLinks = 0;
7 years ago
//int iPoolIndex = 0;
8 years ago
int cPoolLinks;// number of links in the pool.
7 years ago
8 years ago
Vector vecDirToCheckNode;
Vector vecDirToTestNode;
Vector vecStepCheckDir;
Vector vecTraceSpot;
Vector vecSpot;
7 years ago
8 years ago
Vector2D vec2DirToCheckNode;
Vector2D vec2DirToTestNode;
Vector2D vec2StepCheckDir;
Vector2D vec2TraceSpot;
Vector2D vec2Spot;
7 years ago
8 years ago
float flYaw;// use this stuff to walk the hull between nodes
float flDist;
int step;
7 years ago
SetThink(&CTestHull :: SUB_Remove );// no matter what happens, the hull gets rid of itself.
SetNextThink( 0 );
//malloc a swollen temporary connection pool that we trim down after we know exactly how many connections there are.
8 years ago
pTempPool = (CLink *)calloc( sizeof(CLink), ( WorldGraph.m_cNodes * MAX_NODE_INITIAL_LINKS ) );
if( !pTempPool )
7 years ago
{
8 years ago
ALERT( at_aiconsole, "**Could not malloc TempPool!\n" );
7 years ago
return;
}
// make sure directories have been made
GET_GAME_DIR( szNrpFilename );
strcat( szNrpFilename, "/maps" );
5 years ago
CreateDirectoryA( szNrpFilename, NULL );
7 years ago
strcat( szNrpFilename, "/graphs" );
5 years ago
CreateDirectoryA( szNrpFilename, NULL );
7 years ago
strcat( szNrpFilename, "/" );
strcat( szNrpFilename, STRING( gpGlobals->mapname ) );
strcat( szNrpFilename, ".nrp" );
8 years ago
file = fopen( szNrpFilename, "w+" );
7 years ago
8 years ago
if( !file )
{
// file error
ALERT( at_aiconsole, "Couldn't create %s!\n", szNrpFilename );
7 years ago
8 years ago
if( pTempPool )
7 years ago
{
8 years ago
free( pTempPool );
7 years ago
}
return;
}
8 years ago
fprintf( file, "Node Graph Report for map: %s.bsp\n", STRING( gpGlobals->mapname ) );
fprintf( file, "%d Total Nodes\n\n", WorldGraph.m_cNodes );
7 years ago
8 years ago
for( i = 0; i < WorldGraph.m_cNodes; i++ )
{
// print all node numbers and their locations to the file.
WorldGraph.m_pNodes[i].m_cNumLinks = 0;
WorldGraph.m_pNodes[i].m_iFirstLink = 0;
memset( WorldGraph.m_pNodes[i].m_pNextBestNode, 0, sizeof(WorldGraph.m_pNodes[i].m_pNextBestNode) );
7 years ago
8 years ago
fprintf( file, "Node# %4d\n", i );
fprintf( file, "Location %4d,%4d,%4d\n",(int)WorldGraph.m_pNodes[i].m_vecOrigin.x, (int)WorldGraph.m_pNodes[i].m_vecOrigin.y, (int)WorldGraph.m_pNodes[i].m_vecOrigin.z );
fprintf( file, "HintType: %4d\n", WorldGraph.m_pNodes[i].m_sHintType );
fprintf( file, "HintActivity: %4d\n", WorldGraph.m_pNodes[i].m_sHintActivity );
fprintf( file, "HintYaw: %4f\n", (double)WorldGraph.m_pNodes[i].m_flHintYaw );
8 years ago
fprintf( file, "-------------------------------------------------------------------------------\n" );
7 years ago
}
8 years ago
fprintf( file, "\n\n" );
7 years ago
// Automatically recognize WATER nodes and drop the LAND nodes to the floor.
//
8 years ago
for( i = 0; i < WorldGraph.m_cNodes; i++)
7 years ago
{
8 years ago
if( WorldGraph.m_pNodes[i].m_afNodeInfo & bits_NODE_AIR )
7 years ago
{
// do nothing
}
8 years ago
else if( UTIL_PointContents( WorldGraph.m_pNodes[i].m_vecOrigin ) == CONTENTS_WATER )
7 years ago
{
8 years ago
WorldGraph.m_pNodes[i].m_afNodeInfo |= bits_NODE_WATER;
7 years ago
}
else
{
8 years ago
WorldGraph.m_pNodes[i].m_afNodeInfo |= bits_NODE_LAND;
7 years ago
// trace to the ground, then pop up 8 units and place node there to make it
// easier for them to connect (think stairs, chairs, and bumps in the floor).
// After the routing is done, push them back down.
//
8 years ago
TraceResult tr;
7 years ago
8 years ago
UTIL_TraceLine( WorldGraph.m_pNodes[i].m_vecOrigin,
WorldGraph.m_pNodes[i].m_vecOrigin - Vector( 0, 0, 384 ),
ignore_monsters,
g_pBodyQueueHead,//!!!HACKHACK no real ent to supply here, using a global we don't care about
&tr );
7 years ago
// This trace is ONLY used if we hit an entity flagged with FL_WORLDBRUSH
8 years ago
TraceResult trEnt;
UTIL_TraceLine( WorldGraph.m_pNodes[i].m_vecOrigin,
WorldGraph.m_pNodes[i].m_vecOrigin - Vector( 0, 0, 384 ),
dont_ignore_monsters,
g_pBodyQueueHead,//!!!HACKHACK no real ent to supply here, using a global we don't care about
&trEnt );
7 years ago
// Did we hit something closer than the floor?
8 years ago
if( trEnt.flFraction < tr.flFraction )
7 years ago
{
// If it was a world brush entity, copy the node location
8 years ago
if( trEnt.pHit && ( trEnt.pHit->v.flags & FL_WORLDBRUSH ) )
7 years ago
tr.vecEndPos = trEnt.vecEndPos;
}
WorldGraph.m_pNodes[i].m_vecOriginPeek.z =
WorldGraph.m_pNodes[i].m_vecOrigin.z = tr.vecEndPos.z + NODE_HEIGHT;
}
}
cPoolLinks = WorldGraph.LinkVisibleNodes( pTempPool, file, &iBadNode );
8 years ago
if( !cPoolLinks )
7 years ago
{
8 years ago
ALERT( at_aiconsole, "**ConnectVisibleNodes FAILED!\n" );
9 years ago
SetThink( &CTestHull::ShowBadNode );// send the hull off to show the offending node.
7 years ago
//pev->solid = SOLID_NOT;
8 years ago
pev->origin = WorldGraph.m_pNodes[iBadNode].m_vecOrigin;
if( pTempPool )
7 years ago
{
8 years ago
free( pTempPool );
7 years ago
}
8 years ago
if( file )
{
// close the file
fclose( file );
7 years ago
}
return;
}
// send the walkhull to all of this node's connections now. We'll do this here since
// so much of it relies on being able to control the test hull.
8 years ago
fprintf( file, "----------------------------------------------------------------------------\n" );
fprintf( file, "Walk Rejection:\n");
7 years ago
8 years ago
for( i = 0; i < WorldGraph.m_cNodes; i++ )
7 years ago
{
8 years ago
pSrcNode = &WorldGraph.m_pNodes[i];
9 years ago
8 years ago
fprintf( file, "-------------------------------------------------------------------------------\n" );
fprintf( file, "Node %4d:\n\n", i );
7 years ago
8 years ago
for( j = 0; j < pSrcNode->m_cNumLinks; j++ )
7 years ago
{
// assume that all hulls can walk this link, then eliminate the ones that can't.
8 years ago
pTempPool[pSrcNode->m_iFirstLink + j].m_afLinkInfo = bits_LINK_SMALL_HULL | bits_LINK_HUMAN_HULL | bits_LINK_LARGE_HULL | bits_LINK_FLY_HULL;
7 years ago
// do a check for each hull size.
8 years ago
7 years ago
// if we can't fit a tiny hull through a connection, no other hulls with fit either, so we
// should just fall out of the loop. Do so by setting the SkipRemainingHulls flag.
fSkipRemainingHulls = FALSE;
8 years ago
for( hull = 0; hull < MAX_NODE_HULLS; hull++ )
7 years ago
{
8 years ago
if( fSkipRemainingHulls && ( hull == NODE_HUMAN_HULL || hull == NODE_LARGE_HULL ) ) // skip the remaining walk hulls
7 years ago
continue;
8 years ago
switch( hull )
7 years ago
{
case NODE_SMALL_HULL:
8 years ago
UTIL_SetSize( pev, Vector( -12, -12, 0 ), Vector( 12, 12, 24 ) );
7 years ago
break;
case NODE_HUMAN_HULL:
8 years ago
UTIL_SetSize( pev, VEC_HUMAN_HULL_MIN, VEC_HUMAN_HULL_MAX );
7 years ago
break;
case NODE_LARGE_HULL:
8 years ago
UTIL_SetSize( pev, Vector( -32, -32, 0 ), Vector( 32, 32, 64 ) );
7 years ago
break;
case NODE_FLY_HULL:
8 years ago
UTIL_SetSize( pev, Vector( -32, -32, 0 ), Vector( 32, 32, 64 ) );
// UTIL_SetSize( pev, Vector( 0, 0, 0 ), Vector( 0, 0, 0 ) );
7 years ago
break;
}
UTIL_SetOrigin ( this, pSrcNode->m_vecOrigin );// place the hull on the node
8 years ago
if( !FBitSet( pev->flags, FL_ONGROUND ) )
7 years ago
{
8 years ago
ALERT( at_aiconsole, "OFFGROUND!\n" );
7 years ago
}
// now build a yaw that points to the dest node, and get the distance.
8 years ago
if( j < 0 )
7 years ago
{
8 years ago
ALERT( at_aiconsole, "**** j = %d ****\n", j );
if( pTempPool )
7 years ago
{
8 years ago
free( pTempPool );
7 years ago
}
8 years ago
if( file )
{
// close the file
fclose( file );
7 years ago
}
return;
}
8 years ago
pDestNode = &WorldGraph.m_pNodes[pTempPool[pSrcNode->m_iFirstLink + j].m_iDestNode];
7 years ago
vecSpot = pDestNode->m_vecOrigin;
//vecSpot.z = pev->origin.z;
8 years ago
if( hull < NODE_FLY_HULL )
7 years ago
{
int SaveFlags = pev->flags;
int MoveMode = WALKMOVE_WORLDONLY;
8 years ago
if( pSrcNode->m_afNodeInfo & bits_NODE_WATER )
7 years ago
{
pev->flags |= FL_SWIM;
MoveMode = WALKMOVE_NORMAL;
}
8 years ago
flYaw = UTIL_VecToYaw( pDestNode->m_vecOrigin - pev->origin );
7 years ago
flDist = ( vecSpot - pev->origin ).Length2D();
int fWalkFailed = FALSE;
// in this loop we take tiny steps from the current node to the nodes that it links to, one at a time.
// pev->angles.y = flYaw;
8 years ago
for( step = 0; step < flDist && !fWalkFailed; step += HULL_STEP_SIZE )
7 years ago
{
float stepSize = HULL_STEP_SIZE;
8 years ago
if( ( step + stepSize ) >= ( flDist - 1 ) )
stepSize = ( flDist - step ) - 1;
7 years ago
8 years ago
if( !WALK_MOVE( ENT( pev ), flYaw, stepSize, MoveMode ) )
{
// can't take the next step
7 years ago
fWalkFailed = TRUE;
break;
}
}
8 years ago
if( !fWalkFailed && ( pev->origin - vecSpot ).Length() > 64 )
7 years ago
{
8 years ago
// ALERT( at_console, "bogus walk\n" );
7 years ago
// we thought we
fWalkFailed = TRUE;
}
8 years ago
if( fWalkFailed )
7 years ago
{
8 years ago
//pTempPool[pSrcNode->m_iFirstLink + j] = pTempPool[pSrcNode->m_iFirstLink + ( pSrcNode->m_cNumLinks - 1 )];
7 years ago
// now me must eliminate the hull that couldn't walk this connection
8 years ago
switch( hull )
7 years ago
{
case NODE_SMALL_HULL: // if this hull can't fit, nothing can, so drop the connection
8 years ago
fprintf( file, "NODE_SMALL_HULL step %d\n", step );
pTempPool[pSrcNode->m_iFirstLink + j].m_afLinkInfo &= ~( bits_LINK_SMALL_HULL | bits_LINK_HUMAN_HULL | bits_LINK_LARGE_HULL );
7 years ago
fSkipRemainingHulls = TRUE;// don't bother checking larger hulls
break;
case NODE_HUMAN_HULL:
8 years ago
fprintf( file, "NODE_HUMAN_HULL step %d\n", step );
pTempPool[pSrcNode->m_iFirstLink + j].m_afLinkInfo &= ~( bits_LINK_HUMAN_HULL | bits_LINK_LARGE_HULL );
7 years ago
fSkipRemainingHulls = TRUE;// don't bother checking larger hulls
break;
case NODE_LARGE_HULL:
8 years ago
fprintf( file, "NODE_LARGE_HULL step %d\n", step );
pTempPool[pSrcNode->m_iFirstLink + j].m_afLinkInfo &= ~bits_LINK_LARGE_HULL;
7 years ago
break;
}
}
pev->flags = SaveFlags;
}
else
{
TraceResult tr;
UTIL_TraceHull( pSrcNode->m_vecOrigin + Vector( 0, 0, 32 ), pDestNode->m_vecOriginPeek + Vector( 0, 0, 32 ), ignore_monsters, large_hull, ENT( pev ), &tr );
if( tr.fStartSolid || tr.flFraction < 1.0f )
7 years ago
{
8 years ago
pTempPool[pSrcNode->m_iFirstLink + j].m_afLinkInfo &= ~bits_LINK_FLY_HULL;
7 years ago
}
}
}
8 years ago
if( pTempPool[pSrcNode->m_iFirstLink + j].m_afLinkInfo == 0 )
7 years ago
{
8 years ago
fprintf( file, "Rejected Node %3d - Unreachable by ", pTempPool[ pSrcNode->m_iFirstLink + j].m_iDestNode );
pTempPool[pSrcNode->m_iFirstLink + j] = pTempPool[pSrcNode->m_iFirstLink + ( pSrcNode->m_cNumLinks - 1 )];
fprintf( file, "Any Hull\n" );
7 years ago
pSrcNode->m_cNumLinks--;
cPoolLinks--;// we just removed a link, so decrement the total number of links in the pool.
j--;
}
}
}
8 years ago
fprintf( file, "-------------------------------------------------------------------------------\n\n\n" );
7 years ago
8 years ago
cPoolLinks -= WorldGraph.RejectInlineLinks( pTempPool, file );
7 years ago
// now malloc a pool just large enough to hold the links that are actually used
8 years ago
WorldGraph.m_pLinkPool = (CLink *)calloc( sizeof(CLink), cPoolLinks );
7 years ago
8 years ago
if( !WorldGraph.m_pLinkPool )
{
// couldn't make the link pool!
ALERT( at_aiconsole, "Couldn't malloc LinkPool!\n" );
if( pTempPool )
7 years ago
{
8 years ago
free( pTempPool );
7 years ago
}
8 years ago
if( file )
{
// close the file
fclose( file );
7 years ago
}
return;
}
WorldGraph.m_cLinks = cPoolLinks;
//copy only the used portions of the TempPool into the graph's link pool
7 years ago
int iFinalPoolIndex = 0;
int iOldFirstLink;
8 years ago
for( i = 0; i < WorldGraph.m_cNodes; i++ )
7 years ago
{
8 years ago
iOldFirstLink = WorldGraph.m_pNodes[i].m_iFirstLink;// store this, because we have to re-assign it before entering the copy loop
7 years ago
8 years ago
WorldGraph.m_pNodes[i].m_iFirstLink = iFinalPoolIndex;
7 years ago
8 years ago
for( j = 0; j < WorldGraph.m_pNodes[i].m_cNumLinks; j++ )
7 years ago
{
8 years ago
WorldGraph.m_pLinkPool[iFinalPoolIndex++] = pTempPool[iOldFirstLink + j];
7 years ago
}
}
// Node sorting numbers linked nodes close to each other
//
WorldGraph.SortNodes();
// This is used for HashSearch
//
WorldGraph.BuildLinkLookups();
fPairsValid = TRUE; // assume that the connection pairs are all valid to start
8 years ago
fprintf( file, "\n\n-------------------------------------------------------------------------------\n" );
fprintf( file, "Link Pairings:\n" );
7 years ago
// link integrity check. The idea here is that if Node A links to Node B, node B should
// link to node A. If not, we have a situation that prevents us from using a basic
// optimization in the FindNearestLink function.
8 years ago
for( i = 0; i < WorldGraph.m_cNodes; i++ )
7 years ago
{
8 years ago
for( j = 0; j < WorldGraph.m_pNodes[i].m_cNumLinks; j++ )
7 years ago
{
int iLink;
8 years ago
WorldGraph.HashSearch( WorldGraph.INodeLink( i, j ), i, iLink );
if( iLink < 0 )
7 years ago
{
fPairsValid = FALSE;// unmatched link pair.
8 years ago
fprintf( file, "WARNING: Node %3d does not connect back to Node %3d\n", WorldGraph.INodeLink( i, j ), i );
7 years ago
}
}
}
// !!!LATER - if all connections are properly paired, when can enable an optimization in the pathfinding code
// (in the find nearest line function)
8 years ago
if( fPairsValid )
7 years ago
{
8 years ago
fprintf( file, "\nAll Connections are Paired!\n" );
7 years ago
}
4 years ago
#if _MSC_VER
#define SIZET_FMT "%Iu"
#else
#define SIZET_FMT "%zu"
#endif
8 years ago
fprintf( file, "-------------------------------------------------------------------------------\n" );
fprintf( file, "\n\n-------------------------------------------------------------------------------\n" );
fprintf( file, "Total Number of Connections in Pool: %d\n", cPoolLinks );
fprintf( file, "-------------------------------------------------------------------------------\n" );
fprintf( file, "Connection Pool: " SIZET_FMT " bytes\n", sizeof(CLink) * cPoolLinks );
8 years ago
fprintf( file, "-------------------------------------------------------------------------------\n" );
7 years ago
8 years ago
ALERT( at_aiconsole, "%d Nodes, %d Connections\n", WorldGraph.m_cNodes, cPoolLinks );
7 years ago
// This is used for FindNearestNode
//
WorldGraph.BuildRegionTables();
// Push all of the LAND nodes down to the ground now. Leave the water and air nodes alone.
//
8 years ago
for( i = 0; i < WorldGraph.m_cNodes; i++ )
7 years ago
{
8 years ago
if( ( WorldGraph.m_pNodes[i].m_afNodeInfo & bits_NODE_LAND ) )
7 years ago
{
8 years ago
WorldGraph.m_pNodes[i].m_vecOrigin.z -= NODE_HEIGHT;
7 years ago
}
}
8 years ago
if( pTempPool )
{
// free the temp pool
free( pTempPool );
7 years ago
}
8 years ago
if( file )
7 years ago
{
8 years ago
fclose( file );
7 years ago
}
// We now have some graphing capabilities.
//
WorldGraph.m_fGraphPresent = TRUE;//graph is in memory.
WorldGraph.m_fGraphPointersSet = TRUE;// since the graph was generated, the pointers are ready
WorldGraph.m_fRoutingComplete = FALSE; // Optimal routes aren't computed, yet.
// Compute and compress the routing information.
//
WorldGraph.ComputeStaticRoutingTables();
// save the node graph for this level
WorldGraph.FSaveGraph( STRING( gpGlobals->mapname ) );
8 years ago
ALERT( at_console, "Done.\n" );
7 years ago
}
//=========================================================
// returns a hardcoded path.
//=========================================================
8 years ago
void CTestHull::PathFind( void )
7 years ago
{
8 years ago
int iPath[50];
int iPathSize;
int i;
CNode *pNode, *pNextNode;
if( !WorldGraph.m_fGraphPresent || !WorldGraph.m_fGraphPointersSet )
{
// protect us in the case that the node graph isn't available
ALERT( at_aiconsole, "Graph not ready!\n" );
7 years ago
return;
}
8 years ago
iPathSize = WorldGraph.FindShortestPath( iPath, 0, 19, 0, 0 ); // UNDONE use hull constant
7 years ago
8 years ago
if( !iPathSize )
7 years ago
{
8 years ago
ALERT( at_aiconsole, "No Path!\n" );
7 years ago
return;
}
8 years ago
ALERT( at_aiconsole, "%d\n", iPathSize );
7 years ago
8 years ago
pNode = &WorldGraph.m_pNodes[iPath[0]];
7 years ago
8 years ago
for( i = 0; i < iPathSize - 1; i++ )
7 years ago
{
8 years ago
pNextNode = &WorldGraph.m_pNodes[iPath[i + 1]];
7 years ago
MESSAGE_BEGIN( MSG_BROADCAST, SVC_TEMPENTITY );
8 years ago
WRITE_BYTE( TE_SHOWLINE );
7 years ago
WRITE_COORD( pNode->m_vecOrigin.x );
WRITE_COORD( pNode->m_vecOrigin.y );
WRITE_COORD( pNode->m_vecOrigin.z + NODE_HEIGHT );
8 years ago
WRITE_COORD( pNextNode->m_vecOrigin.x );
WRITE_COORD( pNextNode->m_vecOrigin.y );
WRITE_COORD( pNextNode->m_vecOrigin.z + NODE_HEIGHT );
7 years ago
MESSAGE_END();
pNode = pNextNode;
}
}
//=========================================================
// CStack Constructor
//=========================================================
8 years ago
CStack::CStack( void )
7 years ago
{
m_level = 0;
}
//=========================================================
// pushes a value onto the stack
//=========================================================
8 years ago
void CStack::Push( int value )
7 years ago
{
8 years ago
if( m_level >= MAX_STACK_NODES )
7 years ago
{
8 years ago
printf( "Error!\n" );
7 years ago
return;
}
m_stack[m_level] = value;
m_level++;
}
//=========================================================
// pops a value off of the stack
//=========================================================
8 years ago
int CStack::Pop( void )
7 years ago
{
8 years ago
if( m_level <= 0 )
7 years ago
return -1;
m_level--;
8 years ago
return m_stack[m_level];
7 years ago
}
//=========================================================
// returns the value on the top of the stack
//=========================================================
8 years ago
int CStack::Top( void )
7 years ago
{
8 years ago
return m_stack[m_level - 1];
7 years ago
}
//=========================================================
// copies every element on the stack into an array LIFO
//=========================================================
8 years ago
void CStack::CopyToArray( int *piArray )
7 years ago
{
8 years ago
int i;
7 years ago
8 years ago
for( i = 0; i < m_level; i++ )
7 years ago
{
8 years ago
piArray[i] = m_stack[i];
7 years ago
}
}
//=========================================================
// CQueue constructor
//=========================================================
8 years ago
CQueue::CQueue( void )
7 years ago
{
m_cSize = 0;
m_head = 0;
m_tail = -1;
}
//=========================================================
// inserts a value into the queue
//=========================================================
8 years ago
void CQueue::Insert( int iValue, float fPriority )
7 years ago
{
8 years ago
if( Full() )
7 years ago
{
8 years ago
printf( "Queue is full!\n" );
7 years ago
return;
}
m_tail++;
8 years ago
if( m_tail == MAX_STACK_NODES )
{
//wrap around
7 years ago
m_tail = 0;
}
8 years ago
m_queue[m_tail].Id = iValue;
m_queue[m_tail].Priority = fPriority;
7 years ago
m_cSize++;
}
//=========================================================
// removes a value from the queue (FIFO)
//=========================================================
8 years ago
int CQueue::Remove( float &fPriority )
7 years ago
{
8 years ago
if( m_head == MAX_STACK_NODES )
{
// wrap
7 years ago
m_head = 0;
}
m_cSize--;
8 years ago
fPriority = m_queue[m_head].Priority;
return m_queue[m_head++].Id;
7 years ago
}
//=========================================================
// CQueue constructor
//=========================================================
8 years ago
CQueuePriority::CQueuePriority( void )
7 years ago
{
m_cSize = 0;
}
//=========================================================
// inserts a value into the priority queue
//=========================================================
8 years ago
void CQueuePriority::Insert( int iValue, float fPriority )
7 years ago
{
8 years ago
if( Full() )
7 years ago
{
8 years ago
printf( "Queue is full!\n" );
7 years ago
return;
}
8 years ago
m_heap[m_cSize].Priority = fPriority;
m_heap[m_cSize].Id = iValue;
m_cSize++;
Heap_SiftUp();
7 years ago
}
//=========================================================
// removes the smallest item from the priority queue
//
//=========================================================
8 years ago
int CQueuePriority::Remove( float &fPriority )
7 years ago
{
8 years ago
int iReturn = m_heap[0].Id;
fPriority = m_heap[0].Priority;
7 years ago
m_cSize--;
8 years ago
m_heap[0] = m_heap[m_cSize];
7 years ago
8 years ago
Heap_SiftDown( 0 );
return iReturn;
7 years ago
}
8 years ago
#define HEAP_LEFT_CHILD(x) ( 2 * ( x ) + 1 )
#define HEAP_RIGHT_CHILD(x) ( 2 * ( x ) + 2 )
#define HEAP_PARENT(x) ( ( ( x ) - 1 ) / 2 )
7 years ago
8 years ago
void CQueuePriority::Heap_SiftDown( int iSubRoot )
7 years ago
{
int parent = iSubRoot;
8 years ago
int child = HEAP_LEFT_CHILD( parent );
7 years ago
8 years ago
struct tag_HEAP_NODE Ref = m_heap[parent];
7 years ago
8 years ago
while( child < m_cSize )
7 years ago
{
8 years ago
int rightchild = HEAP_RIGHT_CHILD( parent );
if( rightchild < m_cSize )
7 years ago
{
8 years ago
if( m_heap[rightchild].Priority < m_heap[child].Priority )
7 years ago
{
child = rightchild;
}
}
8 years ago
if( Ref.Priority <= m_heap[child].Priority )
7 years ago
break;
8 years ago
m_heap[parent] = m_heap[child];
7 years ago
parent = child;
8 years ago
child = HEAP_LEFT_CHILD( parent );
7 years ago
}
8 years ago
m_heap[parent] = Ref;
7 years ago
}
8 years ago
void CQueuePriority::Heap_SiftUp( void )
7 years ago
{
8 years ago
int child = m_cSize - 1;
while( child )
7 years ago
{
8 years ago
int parent = HEAP_PARENT( child );
if( m_heap[parent].Priority <= m_heap[child].Priority )
7 years ago
break;
struct tag_HEAP_NODE Tmp;
8 years ago
Tmp = m_heap[child];
m_heap[child] = m_heap[parent];
m_heap[parent] = Tmp;
7 years ago
child = parent;
}
}
//=========================================================
// CGraph - FLoadGraph - attempts to load a node graph from disk.
// if the current level is maps/snar.bsp, maps/graphs/snar.nod
// will be loaded. If file cannot be loaded, the node tree
// will be created and saved to disk.
//=========================================================
int CGraph::FLoadGraph( const char *szMapName )
7 years ago
{
8 years ago
char szFilename[MAX_PATH];
int iVersion;
int length;
byte *aMemFile;
byte *pMemFile;
7 years ago
// make sure the directories have been made
8 years ago
char szDirName[MAX_PATH];
7 years ago
GET_GAME_DIR( szDirName );
strcat( szDirName, "/maps" );
5 years ago
CreateDirectoryA( szDirName, NULL );
7 years ago
strcat( szDirName, "/graphs" );
5 years ago
CreateDirectoryA( szDirName, NULL );
7 years ago
8 years ago
strcpy( szFilename, "maps/graphs/" );
strcat( szFilename, szMapName );
7 years ago
strcat( szFilename, ".nod" );
8 years ago
pMemFile = aMemFile = LOAD_FILE_FOR_ME( szFilename, &length );
7 years ago
8 years ago
if( !aMemFile )
7 years ago
return FALSE;
// Read the graph version number
//
length -= sizeof(int);
if( length < 0 )
goto ShortFile;
iVersion = *(int *) pMemFile;
pMemFile += sizeof(int);
7 years ago
if( iVersion == GRAPH_VERSION || iVersion == GRAPH_VERSION_RETAIL )
{
7 years ago
// Read the graph class
//
if ( iVersion == GRAPH_VERSION )
{
length -= sizeof(CGraph);
if( length < 0 )
goto ShortFile;
memcpy( this, pMemFile, sizeof(CGraph) );
pMemFile += sizeof(CGraph);
7 years ago
// Set the pointers to zero, just in case we run out of memory.
//
m_pNodes = NULL;
m_pLinkPool = NULL;
m_di = NULL;
m_pRouteInfo = NULL;
m_pHashLinks = NULL;
}
#if _GRAPH_VERSION != _GRAPH_VERSION_RETAIL
else
{
ALERT( at_aiconsole, "Loading CGraph in GRAPH_VERSION 16 compatibility mode\n" );
length -= sizeof(CGraph_Retail);
if( length < 0 )
goto ShortFile;
reinterpret_cast<CGraph_Retail*>(pMemFile) -> copyOverTo(this);
pMemFile += sizeof(CGraph_Retail);
}
#endif
7 years ago
// Malloc for the nodes
//
8 years ago
m_pNodes = (CNode *)calloc( sizeof(CNode), m_cNodes );
7 years ago
8 years ago
if( !m_pNodes )
7 years ago
{
8 years ago
ALERT( at_aiconsole, "**ERROR**\nCouldn't malloc %d nodes!\n", m_cNodes );
7 years ago
goto NoMemory;
}
// Read in all the nodes
//
length -= sizeof(CNode) * m_cNodes;
8 years ago
if( length < 0 )
goto ShortFile;
memcpy( m_pNodes, pMemFile, sizeof(CNode) * m_cNodes );
7 years ago
pMemFile += sizeof(CNode) * m_cNodes;
// Malloc for the link pool
//
8 years ago
m_pLinkPool = (CLink *)calloc( sizeof(CLink), m_cLinks );
7 years ago
8 years ago
if( !m_pLinkPool )
7 years ago
{
8 years ago
ALERT( at_aiconsole, "**ERROR**\nCouldn't malloc %d link!\n", m_cLinks );
7 years ago
goto NoMemory;
}
// Read in all the links
//
if( iVersion == GRAPH_VERSION )
{
length -= sizeof(CLink) * m_cLinks;
if( length < 0 )
goto ShortFile;
memcpy( m_pLinkPool, pMemFile, sizeof(CLink) * m_cLinks );
pMemFile += sizeof(CLink) * m_cLinks;
}
#if _GRAPH_VERSION != _GRAPH_VERSION_RETAIL
else
{
ALERT( at_aiconsole, "Loading CLink array in GRAPH_VERSION 16 compatibility mode\n" );
length -= sizeof(CLink_Retail) * m_cLinks;
if( length < 0 )
goto ShortFile;
reinterpret_cast<CLink_Retail*>(pMemFile) -> copyOverTo(m_pLinkPool);
pMemFile += sizeof(CLink_Retail) * m_cLinks;
}
#endif
7 years ago
// Malloc for the sorting info.
//
m_di = (DIST_INFO *)calloc( sizeof(DIST_INFO), m_cNodes );
8 years ago
if( !m_di )
7 years ago
{
8 years ago
ALERT( at_aiconsole, "***ERROR**\nCouldn't malloc %d entries sorting nodes!\n", m_cNodes );
7 years ago
goto NoMemory;
}
// Read it in.
//
8 years ago
length -= sizeof(DIST_INFO) * m_cNodes;
if( length < 0 )
goto ShortFile;
memcpy( m_di, pMemFile, sizeof(DIST_INFO) * m_cNodes );
pMemFile += sizeof(DIST_INFO) * m_cNodes;
7 years ago
// Malloc for the routing info.
//
m_fRoutingComplete = FALSE;
m_pRouteInfo = (signed char *)calloc( sizeof(signed char), m_nRouteInfo );
8 years ago
if( !m_pRouteInfo )
7 years ago
{
ALERT( at_aiconsole, "***ERROR**\nCouldn't malloc %d route bytes!\n", m_nRouteInfo );
7 years ago
goto NoMemory;
}
m_CheckedCounter = 0;
8 years ago
for(int i = 0; i < m_cNodes; i++ )
7 years ago
{
m_di[i].m_CheckedEvent = 0;
}
8 years ago
7 years ago
// Read in the route information.
//
8 years ago
length -= sizeof(char) * m_nRouteInfo;
if( length < 0 )
goto ShortFile;
memcpy( m_pRouteInfo, pMemFile, sizeof(char) * m_nRouteInfo );
pMemFile += sizeof(char) * m_nRouteInfo;
m_fRoutingComplete = TRUE;
7 years ago
// malloc for the hash links
//
8 years ago
m_pHashLinks = (short *)calloc( sizeof(short), m_nHashLinks );
if( !m_pHashLinks )
7 years ago
{
ALERT( at_aiconsole, "***ERROR**\nCouldn't malloc %d hash link bytes!\n", m_nHashLinks );
7 years ago
goto NoMemory;
}
// Read in the hash link information
//
8 years ago
length -= sizeof(short) * m_nHashLinks;
if( length < 0 )
goto ShortFile;
memcpy( m_pHashLinks, pMemFile, sizeof(short) * m_nHashLinks );
// pMemFile += sizeof(short) * m_nHashLinks;
7 years ago
// Set the graph present flag, clear the pointers set flag
//
m_fGraphPresent = TRUE;
m_fGraphPointersSet = FALSE;
8 years ago
FREE_FILE( aMemFile );
if( length != 0 )
7 years ago
{
8 years ago
ALERT( at_aiconsole, "***WARNING***:Node graph was longer than expected by %d bytes.!\n", length );
7 years ago
}
return TRUE;
}
else
{
// This file was written by a different build of the dll!
//
ALERT( at_aiconsole, "**ERROR** Graph version is %d, expected %d\n", iVersion, GRAPH_VERSION );
goto ShortFile;
}
7 years ago
ShortFile:
NoMemory:
8 years ago
FREE_FILE( aMemFile );
7 years ago
return FALSE;
}
//=========================================================
// CGraph - FSaveGraph - It's not rocket science.
// this WILL overwrite existing files.
//=========================================================
int CGraph::FSaveGraph( const char *szMapName )
7 years ago
{
8 years ago
int iVersion = GRAPH_VERSION;
char szFilename[MAX_PATH];
FILE *file;
7 years ago
8 years ago
if( !m_fGraphPresent || !m_fGraphPointersSet )
{
// protect us in the case that the node graph isn't available or built
ALERT( at_aiconsole, "Graph not ready!\n" );
7 years ago
return FALSE;
}
// make sure directories have been made
GET_GAME_DIR( szFilename );
strcat( szFilename, "/maps" );
5 years ago
CreateDirectoryA( szFilename, NULL );
7 years ago
strcat( szFilename, "/graphs" );
5 years ago
CreateDirectoryA( szFilename, NULL );
7 years ago
strcat( szFilename, "/" );
strcat( szFilename, szMapName );
strcat( szFilename, ".nod" );
8 years ago
file = fopen( szFilename, "wb" );
7 years ago
8 years ago
ALERT( at_aiconsole, "Created: %s\n", szFilename );
7 years ago
8 years ago
if( !file )
{
// couldn't create
ALERT( at_aiconsole, "Couldn't Create: %s\n", szFilename );
7 years ago
return FALSE;
}
else
{
8 years ago
// write the version
fwrite( &iVersion, sizeof(int), 1, file );
7 years ago
8 years ago
// write the CGraph class
fwrite( this, sizeof(CGraph), 1, file );
7 years ago
8 years ago
// write the nodes
fwrite( m_pNodes, sizeof(CNode), m_cNodes, file );
7 years ago
8 years ago
// write the links
fwrite( m_pLinkPool, sizeof(CLink), m_cLinks, file );
7 years ago
8 years ago
fwrite( m_di, sizeof(DIST_INFO), m_cNodes, file );
7 years ago
// Write the route info.
//
8 years ago
if( m_pRouteInfo && m_nRouteInfo )
7 years ago
{
8 years ago
fwrite( m_pRouteInfo, sizeof(signed char), m_nRouteInfo, file );
7 years ago
}
8 years ago
if( m_pHashLinks && m_nHashLinks )
7 years ago
{
8 years ago
fwrite( m_pHashLinks, sizeof(short), m_nHashLinks, file );
7 years ago
}
8 years ago
fclose( file );
7 years ago
return TRUE;
}
}
//=========================================================
// CGraph - FSetGraphPointers - Takes the modelnames of
// all of the brush ents that block connections in the node
// graph and resolves them into pointers to those entities.
// this is done after loading the graph from disk, whereupon
// the pointers are not valid.
//=========================================================
8 years ago
int CGraph::FSetGraphPointers( void )
7 years ago
{
8 years ago
int i;
7 years ago
CBaseEntity *pLinkEnt;
8 years ago
for( i = 0; i < m_cLinks; i++ )
{
// go through all of the links
if( m_pLinkPool[i].m_pLinkEnt != NULL )
7 years ago
{
char name[5];
// when graphs are saved, any valid pointers are will be non-zero, signifying that we should
// reset those pointers upon reloading. Any pointers that were NULL when the graph was saved
// will be NULL when reloaded, and will ignored by this function.
// m_szLinkEntModelname is not necessarily NULL terminated (so we can store it in a more alignment-friendly 4 bytes)
8 years ago
memcpy( name, m_pLinkPool[i].m_szLinkEntModelname, 4 );
7 years ago
name[4] = 0;
pLinkEnt = UTIL_FindEntityByString( NULL, "model", name );
if ( !pLinkEnt )
{
8 years ago
// the ent isn't around anymore? Either there is a major problem, or it was removed from the world
// ( like a func_breakable that's been destroyed or something ). Make sure that LinkEnt is null.
ALERT( at_aiconsole, "**Could not find model %s\n", name );
m_pLinkPool[i].m_pLinkEnt = NULL;
7 years ago
}
else
{
m_pLinkPool[ i ].m_pLinkEnt = pLinkEnt->pev;
8 years ago
if( !FBitSet( m_pLinkPool[i].m_pLinkEnt->flags, FL_GRAPHED ) )
7 years ago
{
8 years ago
m_pLinkPool[i].m_pLinkEnt->flags += FL_GRAPHED;
7 years ago
}
}
}
}
// the pointers are now set.
m_fGraphPointersSet = TRUE;
return TRUE;
}
//=========================================================
// CGraph - CheckNODFile - this function checks the date of
// the BSP file that was just loaded and the date of the a
// ssociated .NOD file. If the NOD file is not present, or
// is older than the BSP file, we rebuild it.
//
// returns FALSE if the .NOD file doesn't qualify and needs
// to be rebuilt.
//
// !!!BUGBUG - the file times we get back are 20 hours ahead!
// since this happens consistantly, we can still correctly
// determine which of the 2 files is newer. This needs fixed,
// though. ( I now suspect that we are getting GMT back from
// these functions and must compensate for local time ) (sjb)
//=========================================================
int CGraph::CheckNODFile( const char *szMapName )
7 years ago
{
8 years ago
int retValue;
7 years ago
8 years ago
char szBspFilename[MAX_PATH];
char szGraphFilename[MAX_PATH];
7 years ago
8 years ago
strcpy( szBspFilename, "maps/" );
strcat( szBspFilename, szMapName );
strcat( szBspFilename, ".bsp" );
strcpy( szGraphFilename, "maps/graphs/" );
strcat( szGraphFilename, szMapName );
strcat( szGraphFilename, ".nod" );
7 years ago
retValue = TRUE;
int iCompare;
8 years ago
if( COMPARE_FILE_TIME( szBspFilename, szGraphFilename, &iCompare ) )
7 years ago
{
8 years ago
if( iCompare > 0 )
{
// BSP file is newer.
ALERT( at_aiconsole, ".NOD File will be updated\n\n" );
7 years ago
retValue = FALSE;
}
}
else
{
retValue = FALSE;
}
return retValue;
}
#define ENTRY_STATE_EMPTY -1
struct tagNodePair
{
short iSrc;
short iDest;
};
8 years ago
void CGraph::HashInsert( int iSrcNode, int iDestNode, int iKey )
7 years ago
{
struct tagNodePair np;
np.iSrc = iSrcNode;
np.iDest = iDestNode;
CRC32_t dwHash;
8 years ago
CRC32_INIT( &dwHash );
CRC32_PROCESS_BUFFER( &dwHash, &np, sizeof(np) );
dwHash = CRC32_FINAL( dwHash );
int di = m_HashPrimes[dwHash&15];
int i = ( dwHash >> 4 ) % m_nHashLinks;
while( m_pHashLinks[i] != ENTRY_STATE_EMPTY )
{
i += di;
if( i >= m_nHashLinks )
i -= m_nHashLinks;
}
7 years ago
m_pHashLinks[i] = iKey;
}
8 years ago
void CGraph::HashSearch( int iSrcNode, int iDestNode, int &iKey )
7 years ago
{
struct tagNodePair np;
8 years ago
np.iSrc = iSrcNode;
7 years ago
np.iDest = iDestNode;
CRC32_t dwHash;
8 years ago
CRC32_INIT( &dwHash );
CRC32_PROCESS_BUFFER( &dwHash, &np, sizeof(np) );
dwHash = CRC32_FINAL( dwHash );
int di = m_HashPrimes[dwHash&15];
int i = ( dwHash >> 4 ) % m_nHashLinks;
while( m_pHashLinks[i] != ENTRY_STATE_EMPTY )
{
CLink &link = Link( m_pHashLinks[i] );
if( iSrcNode == link.m_iSrcNode && iDestNode == link.m_iDestNode )
{
7 years ago
break;
8 years ago
}
else
{
i += di;
if( i >= m_nHashLinks )
i -= m_nHashLinks;
}
}
7 years ago
iKey = m_pHashLinks[i];
}
#define NUMBER_OF_PRIMES 177
int Primes[NUMBER_OF_PRIMES] =
{ 1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67,
71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151,
157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239,
241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337,
347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433,
439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541,
547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641,
643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743,
751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857,
859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971,
977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 0 };
8 years ago
void CGraph::HashChoosePrimes( int TableSize )
7 years ago
{
8 years ago
int iPrime, iZone;
int LargestPrime = TableSize / 2;
if( LargestPrime > Primes[NUMBER_OF_PRIMES - 2] )
{
LargestPrime = Primes[NUMBER_OF_PRIMES - 2];
}
int Spacing = LargestPrime / 16;
// Pick a set primes that are evenly spaced from (0 to LargestPrime)
// We divide this interval into 16 equal sized zones. We want to find
// one prime number that best represents that zone.
//
for( iZone = 1, iPrime = 0; iPrime < 16; iZone += Spacing )
{
// Search for a prime number that is less than the target zone
// number given by iZone.
//
int Lower = Primes[0];
for( int jPrime = 0; Primes[jPrime] != 0; jPrime++ )
{
if( jPrime != 0 && TableSize % Primes[jPrime] == 0 )
continue;
int Upper = Primes[jPrime];
if( Lower <= iZone && iZone <= Upper )
{
// Choose the closest lower prime number.
//
if( iZone - Lower <= Upper - iZone )
{
m_HashPrimes[iPrime++] = Lower;
}
else
{
m_HashPrimes[iPrime++] = Upper;
}
break;
}
Lower = Upper;
}
}
// Alternate negative and positive numbers
//
for( iPrime = 0; iPrime < 16; iPrime += 2 )
{
m_HashPrimes[iPrime] = TableSize - m_HashPrimes[iPrime];
}
// Shuffle the set of primes to reduce correlation with bits in
// hash key.
//
for( iPrime = 0; iPrime < 16 - 1; iPrime++ )
{
int Pick = RANDOM_LONG( 0, 15 - iPrime);
int Temp = m_HashPrimes[Pick];
m_HashPrimes[Pick] = m_HashPrimes[15 - iPrime];
m_HashPrimes[15 - iPrime] = Temp;
}
7 years ago
}
// Renumber nodes so that nodes that link together are together.
//
#define UNNUMBERED_NODE -1
8 years ago
void CGraph::SortNodes( void )
7 years ago
{
// We are using m_iPreviousNode to be the new node number.
// After assigning new node numbers to everything, we move
// things and patchup the links.
//
9 years ago
int i, iNodeCnt = 0;
7 years ago
m_pNodes[0].m_iPreviousNode = iNodeCnt++;
8 years ago
for( i = 1; i < m_cNodes; i++ )
7 years ago
{
m_pNodes[i].m_iPreviousNode = UNNUMBERED_NODE;
}
8 years ago
for( i = 0; i < m_cNodes; i++ )
7 years ago
{
// Run through all of this node's neighbors
//
8 years ago
for( int j = 0; j < m_pNodes[i].m_cNumLinks; j++ )
7 years ago
{
8 years ago
int iDestNode = INodeLink( i, j );
if( m_pNodes[iDestNode].m_iPreviousNode == UNNUMBERED_NODE )
7 years ago
{
m_pNodes[iDestNode].m_iPreviousNode = iNodeCnt++;
}
}
}
// Assign remaining node numbers to unlinked nodes.
//
8 years ago
for( i = 0; i < m_cNodes; i++ )
7 years ago
{
8 years ago
if( m_pNodes[i].m_iPreviousNode == UNNUMBERED_NODE )
7 years ago
{
m_pNodes[i].m_iPreviousNode = iNodeCnt++;
}
}
// Alter links to reflect new node numbers.
//
8 years ago
for( i = 0; i < m_cLinks; i++ )
7 years ago
{
m_pLinkPool[i].m_iSrcNode = m_pNodes[m_pLinkPool[i].m_iSrcNode].m_iPreviousNode;
m_pLinkPool[i].m_iDestNode = m_pNodes[m_pLinkPool[i].m_iDestNode].m_iPreviousNode;
}
// Rearrange nodes to reflect new node numbering.
//
8 years ago
for( i = 0; i < m_cNodes; i++ )
7 years ago
{
8 years ago
while( m_pNodes[i].m_iPreviousNode != i )
7 years ago
{
// Move current node off to where it should be, and bring
// that other node back into the current slot.
//
int iDestNode = m_pNodes[i].m_iPreviousNode;
CNode TempNode = m_pNodes[iDestNode];
m_pNodes[iDestNode] = m_pNodes[i];
m_pNodes[i] = TempNode;
}
}
}
8 years ago
void CGraph::BuildLinkLookups( void )
7 years ago
{
9 years ago
int i;
8 years ago
m_nHashLinks = 3 * m_cLinks / 2 + 3;
7 years ago
8 years ago
HashChoosePrimes( m_nHashLinks );
m_pHashLinks = (short *)calloc( sizeof(short), m_nHashLinks );
if( !m_pHashLinks )
7 years ago
{
8 years ago
ALERT( at_aiconsole, "Couldn't allocated Link Lookup Table.\n" );
7 years ago
return;
}
8 years ago
for( i = 0; i < m_nHashLinks; i++ )
7 years ago
{
m_pHashLinks[i] = ENTRY_STATE_EMPTY;
}
8 years ago
for( i = 0; i < m_cLinks; i++ )
7 years ago
{
8 years ago
CLink &link = Link( i );
HashInsert( link.m_iSrcNode, link.m_iDestNode, i );
7 years ago
}
#if 0
8 years ago
for( i = 0; i < m_cLinks; i++ )
7 years ago
{
8 years ago
CLink &link = Link( i );
7 years ago
int iKey;
8 years ago
HashSearch( link.m_iSrcNode, link.m_iDestNode, iKey );
if( iKey != i )
7 years ago
{
8 years ago
ALERT( at_aiconsole, "HashLinks don't match (%d versus %d)\n", i, iKey );
7 years ago
}
}
#endif
}
8 years ago
void CGraph::BuildRegionTables( void )
7 years ago
{
9 years ago
int i, j;
8 years ago
if( m_di )
free( m_di );
7 years ago
// Go ahead and setup for range searching the nodes for FindNearestNodes
//
8 years ago
m_di = (DIST_INFO *)calloc( sizeof(DIST_INFO), m_cNodes );
if( !m_di )
7 years ago
{
8 years ago
ALERT( at_aiconsole, "Couldn't allocated node ordering array.\n" );
7 years ago
return;
}
// Calculate regions for all the nodes.
//
//
8 years ago
for( i = 0; i < 3; i++ )
7 years ago
{
m_RegionMin[i] = 999999999.0; // just a big number out there;
m_RegionMax[i] = -999999999.0; // just a big number out there;
}
8 years ago
for( i = 0; i < m_cNodes; i++ )
7 years ago
{
8 years ago
if( m_pNodes[i].m_vecOrigin.x < m_RegionMin[0] )
7 years ago
m_RegionMin[0] = m_pNodes[i].m_vecOrigin.x;
8 years ago
if( m_pNodes[i].m_vecOrigin.y < m_RegionMin[1] )
7 years ago
m_RegionMin[1] = m_pNodes[i].m_vecOrigin.y;
8 years ago
if( m_pNodes[i].m_vecOrigin.z < m_RegionMin[2] )
7 years ago
m_RegionMin[2] = m_pNodes[i].m_vecOrigin.z;
8 years ago
if( m_pNodes[i].m_vecOrigin.x > m_RegionMax[0] )
7 years ago
m_RegionMax[0] = m_pNodes[i].m_vecOrigin.x;
8 years ago
if( m_pNodes[i].m_vecOrigin.y > m_RegionMax[1] )
7 years ago
m_RegionMax[1] = m_pNodes[i].m_vecOrigin.y;
8 years ago
if( m_pNodes[i].m_vecOrigin.z > m_RegionMax[2] )
7 years ago
m_RegionMax[2] = m_pNodes[i].m_vecOrigin.z;
}
8 years ago
for( i = 0; i < m_cNodes; i++ )
7 years ago
{
8 years ago
m_pNodes[i].m_Region[0] = CALC_RANGE( m_pNodes[i].m_vecOrigin.x, m_RegionMin[0], m_RegionMax[0] );
m_pNodes[i].m_Region[1] = CALC_RANGE( m_pNodes[i].m_vecOrigin.y, m_RegionMin[1], m_RegionMax[1] );
m_pNodes[i].m_Region[2] = CALC_RANGE( m_pNodes[i].m_vecOrigin.z, m_RegionMin[2], m_RegionMax[2] );
7 years ago
}
8 years ago
for( i = 0; i < 3; i++ )
7 years ago
{
8 years ago
for( j = 0; j < NUM_RANGES; j++ )
7 years ago
{
m_RangeStart[i][j] = 255;
m_RangeEnd[i][j] = 0;
}
8 years ago
for( j = 0; j < m_cNodes; j++ )
7 years ago
{
m_di[j].m_SortedBy[i] = j;
}
8 years ago
for( j = 0; j < m_cNodes - 1; j++ )
7 years ago
{
int jNode = m_di[j].m_SortedBy[i];
int jCodeX = m_pNodes[jNode].m_Region[0];
int jCodeY = m_pNodes[jNode].m_Region[1];
int jCodeZ = m_pNodes[jNode].m_Region[2];
int jCode;
8 years ago
switch( i )
7 years ago
{
case 0:
8 years ago
jCode = ( jCodeX << 16 ) + ( jCodeY << 8 ) + jCodeZ;
7 years ago
break;
case 1:
8 years ago
jCode = ( jCodeY << 16 ) + ( jCodeZ << 8 ) + jCodeX;
7 years ago
break;
case 2:
8 years ago
jCode = ( jCodeZ << 16 ) + ( jCodeX << 8 ) + jCodeY;
7 years ago
break;
}
8 years ago
for( int k = j + 1; k < m_cNodes; k++ )
7 years ago
{
int kNode = m_di[k].m_SortedBy[i];
int kCodeX = m_pNodes[kNode].m_Region[0];
int kCodeY = m_pNodes[kNode].m_Region[1];
int kCodeZ = m_pNodes[kNode].m_Region[2];
int kCode;
8 years ago
switch( i )
7 years ago
{
case 0:
8 years ago
kCode = ( kCodeX << 16 ) + ( kCodeY << 8 ) + kCodeZ;
7 years ago
break;
case 1:
8 years ago
kCode = ( kCodeY << 16 ) + ( kCodeZ << 8 ) + kCodeX;
7 years ago
break;
case 2:
8 years ago
kCode = ( kCodeZ << 16 ) + ( kCodeX << 8 ) + kCodeY;
7 years ago
break;
}
8 years ago
if( kCode < jCode )
7 years ago
{
// Swap j and k entries.
//
int Tmp = m_di[j].m_SortedBy[i];
m_di[j].m_SortedBy[i] = m_di[k].m_SortedBy[i];
m_di[k].m_SortedBy[i] = Tmp;
}
}
}
}
// Generate lookup tables.
//
8 years ago
for( i = 0; i < m_cNodes; i++ )
7 years ago
{
int CodeX = m_pNodes[m_di[i].m_SortedBy[0]].m_Region[0];
int CodeY = m_pNodes[m_di[i].m_SortedBy[1]].m_Region[1];
int CodeZ = m_pNodes[m_di[i].m_SortedBy[2]].m_Region[2];
8 years ago
if( i < m_RangeStart[0][CodeX] )
{
m_RangeStart[0][CodeX] = i;
}
if( i < m_RangeStart[1][CodeY] )
{
m_RangeStart[1][CodeY] = i;
}
if( i < m_RangeStart[2][CodeZ] )
{
m_RangeStart[2][CodeZ] = i;
}
if( m_RangeEnd[0][CodeX] < i )
{
m_RangeEnd[0][CodeX] = i;
}
if( m_RangeEnd[1][CodeY] < i )
{
m_RangeEnd[1][CodeY] = i;
}
if( m_RangeEnd[2][CodeZ] < i )
{
m_RangeEnd[2][CodeZ] = i;
}
7 years ago
}
// Initialize the cache.
//
8 years ago
memset( m_Cache, 0, sizeof(m_Cache) );
7 years ago
}
8 years ago
void CGraph::ComputeStaticRoutingTables( void )
7 years ago
{
9 years ago
int iFrom;
8 years ago
int nRoutes = m_cNodes * m_cNodes;
#define FROM_TO(x,y) ( ( x ) * m_cNodes + ( y ) )
7 years ago
short *Routes = new short[nRoutes];
int *pMyPath = new int[m_cNodes];
unsigned short *BestNextNodes = new unsigned short[m_cNodes];
signed char *pRoute = new signed char[m_cNodes*2];
8 years ago
if( Routes && pMyPath && BestNextNodes && pRoute )
7 years ago
{
int nTotalCompressedSize = 0;
8 years ago
for( int iHull = 0; iHull < MAX_NODE_HULLS; iHull++ )
7 years ago
{
8 years ago
for( int iCap = 0; iCap < 2; iCap++ )
7 years ago
{
int iCapMask;
8 years ago
switch( iCap )
7 years ago
{
case 0:
iCapMask = 0;
break;
case 1:
iCapMask = bits_CAP_OPEN_DOORS | bits_CAP_AUTO_DOORS | bits_CAP_USE;
break;
}
// Initialize Routing table to uncalculated.
//
8 years ago
for( iFrom = 0; iFrom < m_cNodes; iFrom++ )
7 years ago
{
8 years ago
for( int iTo = 0; iTo < m_cNodes; iTo++ )
7 years ago
{
8 years ago
Routes[FROM_TO( iFrom, iTo )] = -1;
7 years ago
}
}
8 years ago
for( iFrom = 0; iFrom < m_cNodes; iFrom++ )
7 years ago
{
8 years ago
for( int iTo = m_cNodes - 1; iTo >= 0; iTo-- )
7 years ago
{
8 years ago
if( Routes[FROM_TO( iFrom, iTo )] != -1 )
continue;
7 years ago
8 years ago
int cPathSize = FindShortestPath( pMyPath, iFrom, iTo, iHull, iCapMask );
7 years ago
// Use the computed path to update the routing table.
//
8 years ago
if( cPathSize > 1 )
7 years ago
{
8 years ago
for( int iNode = 0; iNode < cPathSize - 1; iNode++ )
7 years ago
{
int iStart = pMyPath[iNode];
8 years ago
int iNext = pMyPath[iNode + 1];
for( int iNode1 = iNode + 1; iNode1 < cPathSize; iNode1++ )
7 years ago
{
int iEnd = pMyPath[iNode1];
Routes[FROM_TO(iStart, iEnd)] = iNext;
}
}
#if 0
// Well, at first glance, this should work, but actually it's safer
// to be told explictly that you can take a series of node in a
// particular direction. Some links don't appear to have links in
// the opposite direction.
//
8 years ago
for( iNode = cPathSize-1; iNode >= 1; iNode-- )
7 years ago
{
int iStart = pMyPath[iNode];
8 years ago
int iNext = pMyPath[iNode - 1];
for( int iNode1 = iNode-1; iNode1 >= 0; iNode1-- )
7 years ago
{
int iEnd = pMyPath[iNode1];
8 years ago
Routes[FROM_TO( iStart, iEnd )] = iNext;
7 years ago
}
}
#endif
}
else
{
8 years ago
Routes[FROM_TO( iFrom, iTo )] = iFrom;
Routes[FROM_TO( iTo, iFrom )] = iTo;
7 years ago
}
}
}
8 years ago
for( iFrom = 0; iFrom < m_cNodes; iFrom++ )
7 years ago
{
8 years ago
for( int iTo = 0; iTo < m_cNodes; iTo++ )
7 years ago
{
8 years ago
BestNextNodes[iTo] = Routes[FROM_TO( iFrom, iTo )];
7 years ago
}
// Compress this node's routing table.
//
int iLastNode = 9999999; // just really big.
int cSequence = 0;
int cRepeats = 0;
int CompressedSize = 0;
signed char *p = pRoute;
8 years ago
for( int i = 0; i < m_cNodes; i++ )
7 years ago
{
8 years ago
BOOL CanRepeat = ( ( BestNextNodes[i] == iLastNode ) && cRepeats < 127 );
BOOL CanSequence = ( BestNextNodes[i] == i && cSequence < 128 );
7 years ago
8 years ago
if( cRepeats )
7 years ago
{
8 years ago
if( CanRepeat )
7 years ago
{
cRepeats++;
}
else
{
// Emit the repeat phrase.
//
CompressedSize += 2; // (count-1, iLastNode-i)
*p++ = cRepeats - 1;
int a = iLastNode - iFrom;
int b = iLastNode - iFrom + m_cNodes;
int c = iLastNode - iFrom - m_cNodes;
8 years ago
if( -128 <= a && a <= 127 )
7 years ago
{
*p++ = a;
}
8 years ago
else if( -128 <= b && b <= 127 )
7 years ago
{
*p++ = b;
}
8 years ago
else if( -128 <= c && c <= 127 )
7 years ago
{
*p++ = c;
}
else
{
8 years ago
ALERT( at_aiconsole, "Nodes need sorting (%d,%d)!\n", iLastNode, iFrom );
7 years ago
}
cRepeats = 0;
8 years ago
if( CanSequence )
7 years ago
{
// Start a sequence.
//
cSequence++;
}
else
{
// Start another repeat.
//
cRepeats++;
}
}
}
8 years ago
else if( cSequence )
7 years ago
{
8 years ago
if( CanSequence )
7 years ago
{
cSequence++;
}
else
{
// It may be advantageous to combine
// a single-entry sequence phrase with the
// next repeat phrase.
//
8 years ago
if( cSequence == 1 && CanRepeat )
7 years ago
{
// Combine with repeat phrase.
//
cRepeats = 2;
cSequence = 0;
}
else
{
// Emit the sequence phrase.
//
CompressedSize += 1; // (-count)
*p++ = -cSequence;
cSequence = 0;
// Start a repeat sequence.
//
cRepeats++;
}
}
}
else
{
8 years ago
if( CanSequence )
7 years ago
{
// Start a sequence phrase.
//
cSequence++;
}
else
{
// Start a repeat sequence.
//
cRepeats++;
}
}
iLastNode = BestNextNodes[i];
}
8 years ago
if( cRepeats )
7 years ago
{
// Emit the repeat phrase.
//
CompressedSize += 2;
*p++ = cRepeats - 1;
#if 0
iLastNode = iFrom + *pRoute;
8 years ago
if( iLastNode >= m_cNodes )
iLastNode -= m_cNodes;
else if( iLastNode < 0 )
iLastNode += m_cNodes;
7 years ago
#endif
int a = iLastNode - iFrom;
int b = iLastNode - iFrom + m_cNodes;
int c = iLastNode - iFrom - m_cNodes;
8 years ago
if( -128 <= a && a <= 127 )
7 years ago
{
*p++ = a;
}
8 years ago
else if( -128 <= b && b <= 127 )
7 years ago
{
*p++ = b;
}
8 years ago
else if( -128 <= c && c <= 127 )
7 years ago
{
*p++ = c;
}
else
{
8 years ago
ALERT( at_aiconsole, "Nodes need sorting (%d,%d)!\n", iLastNode, iFrom );
7 years ago
}
}
8 years ago
if( cSequence )
7 years ago
{
// Emit the Sequence phrase.
//
CompressedSize += 1;
*p++ = -cSequence;
}
// Go find a place to store this thing and point to it.
//
int nRoute = p - pRoute;
8 years ago
if( m_pRouteInfo )
7 years ago
{
9 years ago
int i;
8 years ago
for( i = 0; i < m_nRouteInfo - nRoute; i++ )
7 years ago
{
8 years ago
if( memcmp( m_pRouteInfo + i, pRoute, nRoute ) == 0 )
7 years ago
{
break;
}
}
8 years ago
if( i < m_nRouteInfo - nRoute )
7 years ago
{
8 years ago
m_pNodes[iFrom].m_pNextBestNode[iHull][iCap] = i;
7 years ago
}
else
{
8 years ago
signed char *Tmp = (signed char *)calloc( sizeof(signed char), ( m_nRouteInfo + nRoute ) );
memcpy( Tmp, m_pRouteInfo, m_nRouteInfo );
free( m_pRouteInfo );
7 years ago
m_pRouteInfo = Tmp;
8 years ago
memcpy( m_pRouteInfo + m_nRouteInfo, pRoute, nRoute );
m_pNodes[iFrom].m_pNextBestNode[iHull][iCap] = m_nRouteInfo;
7 years ago
m_nRouteInfo += nRoute;
nTotalCompressedSize += CompressedSize;
}
}
else
{
m_nRouteInfo = nRoute;
8 years ago
m_pRouteInfo = (signed char *)calloc( sizeof(signed char), nRoute );
memcpy( m_pRouteInfo, pRoute, nRoute );
m_pNodes[iFrom].m_pNextBestNode[iHull][iCap] = 0;
7 years ago
nTotalCompressedSize += CompressedSize;
}
}
}
8 years ago
}
ALERT( at_aiconsole, "Size of Routes = %d\n", nTotalCompressedSize );
7 years ago
}
8 years ago
if( Routes )
delete[] Routes;
if( BestNextNodes )
delete[] BestNextNodes;
if( pRoute )
delete[] pRoute;
if( pMyPath )
delete[] pMyPath;
7 years ago
Routes = 0;
BestNextNodes = 0;
pRoute = 0;
pMyPath = 0;
#if 0
TestRoutingTables();
#endif
m_fRoutingComplete = TRUE;
}
// Test those routing tables. Doesn't really work, yet.
//
8 years ago
void CGraph::TestRoutingTables( void )
7 years ago
{
9 years ago
int i;
7 years ago
int *pMyPath = new int[m_cNodes];
int *pMyPath2 = new int[m_cNodes];
8 years ago
if( pMyPath && pMyPath2 )
7 years ago
{
8 years ago
for( int iHull = 0; iHull < MAX_NODE_HULLS; iHull++ )
7 years ago
{
8 years ago
for( int iCap = 0; iCap < 2; iCap++ )
7 years ago
{
int iCapMask;
8 years ago
switch( iCap )
7 years ago
{
case 0:
iCapMask = 0;
break;
case 1:
iCapMask = bits_CAP_OPEN_DOORS | bits_CAP_AUTO_DOORS | bits_CAP_USE;
break;
}
8 years ago
for( int iFrom = 0; iFrom < m_cNodes; iFrom++ )
7 years ago
{
8 years ago
for( int iTo = 0; iTo < m_cNodes; iTo++ )
7 years ago
{
m_fRoutingComplete = FALSE;
8 years ago
int cPathSize1 = FindShortestPath( pMyPath, iFrom, iTo, iHull, iCapMask );
7 years ago
m_fRoutingComplete = TRUE;
8 years ago
int cPathSize2 = FindShortestPath( pMyPath2, iFrom, iTo, iHull, iCapMask );
7 years ago
// Unless we can look at the entire path, we can verify that it's correct.
//
8 years ago
if( cPathSize2 == MAX_PATH_SIZE )
continue;
7 years ago
// Compare distances.
//
#if 1
float flDistance1 = 0.0;
8 years ago
for( i = 0; i < cPathSize1 - 1; i++ )
7 years ago
{
// Find the link from pMyPath[i] to pMyPath[i+1]
//
8 years ago
if( pMyPath[i] == pMyPath[i + 1] )
continue;
7 years ago
int iVisitNode;
BOOL bFound = FALSE;
8 years ago
for( int iLink = 0; iLink < m_pNodes[pMyPath[i]].m_cNumLinks; iLink++ )
7 years ago
{
8 years ago
iVisitNode = INodeLink( pMyPath[i], iLink );
if( iVisitNode == pMyPath[i + 1] )
7 years ago
{
8 years ago
flDistance1 += m_pLinkPool[m_pNodes[pMyPath[i]].m_iFirstLink + iLink].m_flWeight;
7 years ago
bFound = TRUE;
break;
}
}
8 years ago
if( !bFound )
7 years ago
{
8 years ago
ALERT( at_aiconsole, "No link.\n" );
7 years ago
}
}
float flDistance2 = 0.0;
8 years ago
for( i = 0; i < cPathSize2 - 1; i++ )
7 years ago
{
// Find the link from pMyPath2[i] to pMyPath2[i+1]
//
8 years ago
if( pMyPath2[i] == pMyPath2[i + 1] )
continue;
7 years ago
int iVisitNode;
BOOL bFound = FALSE;
8 years ago
for( int iLink = 0; iLink < m_pNodes[pMyPath2[i]].m_cNumLinks; iLink++ )
7 years ago
{
8 years ago
iVisitNode = INodeLink( pMyPath2[i], iLink );
if( iVisitNode == pMyPath2[i + 1] )
7 years ago
{
8 years ago
flDistance2 += m_pLinkPool[m_pNodes[pMyPath2[i]].m_iFirstLink + iLink].m_flWeight;
7 years ago
bFound = TRUE;
break;
}
}
8 years ago
if( !bFound )
7 years ago
{
8 years ago
ALERT( at_aiconsole, "No link.\n" );
7 years ago
}
}
if( fabs( flDistance1 - flDistance2 ) > 0.1f )
7 years ago
{
#else
8 years ago
if( cPathSize1 != cPathSize2 || memcmp( pMyPath, pMyPath2, sizeof(int) * cPathSize1 ) != 0 )
7 years ago
{
#endif
8 years ago
ALERT( at_aiconsole, "Routing is inconsistent!!!\n" );
ALERT( at_aiconsole, "(%d to %d |%d/%d)1:", iFrom, iTo, iHull, iCap );
for( i = 0; i < cPathSize1; i++ )
7 years ago
{
8 years ago
ALERT( at_aiconsole, "%d ", pMyPath[i] );
7 years ago
}
8 years ago
ALERT( at_aiconsole, "\n(%d to %d |%d/%d)2:", iFrom, iTo, iHull, iCap );
for( i = 0; i < cPathSize2; i++ )
7 years ago
{
8 years ago
ALERT( at_aiconsole, "%d ", pMyPath2[i] );
7 years ago
}
8 years ago
ALERT( at_aiconsole, "\n" );
7 years ago
m_fRoutingComplete = FALSE;
8 years ago
cPathSize1 = FindShortestPath( pMyPath, iFrom, iTo, iHull, iCapMask );
7 years ago
m_fRoutingComplete = TRUE;
8 years ago
cPathSize2 = FindShortestPath( pMyPath2, iFrom, iTo, iHull, iCapMask );
7 years ago
goto EnoughSaid;
}
}
}
}
}
}
EnoughSaid:
8 years ago
if( pMyPath )
delete[] pMyPath;
if( pMyPath2 )
delete[] pMyPath2;
7 years ago
pMyPath = 0;
pMyPath2 = 0;
}
//=========================================================
// CNodeViewer - Draws a graph of the shorted path from all nodes
// to current location (typically the player). It then draws
// as many connects as it can per frame, trying not to overflow the buffer
//=========================================================
class CNodeViewer : public CBaseEntity
{
public:
void Spawn( void );
int m_iBaseNode;
int m_iDraw;
8 years ago
int m_nVisited;
7 years ago
int m_aFrom[128];
int m_aTo[128];
int m_iHull;
int m_afNodeType;
Vector m_vecColor;
void FindNodeConnections( int iNode );
void AddNode( int iFrom, int iTo );
void EXPORT DrawThink( void );
};
LINK_ENTITY_TO_CLASS( node_viewer, CNodeViewer )
LINK_ENTITY_TO_CLASS( node_viewer_human, CNodeViewer )
LINK_ENTITY_TO_CLASS( node_viewer_fly, CNodeViewer )
LINK_ENTITY_TO_CLASS( node_viewer_large, CNodeViewer )
9 years ago
8 years ago
void CNodeViewer::Spawn()
7 years ago
{
8 years ago
if( !WorldGraph.m_fGraphPresent || !WorldGraph.m_fGraphPointersSet )
{
// protect us in the case that the node graph isn't available or built
ALERT( at_console, "Graph not ready!\n" );
7 years ago
UTIL_Remove( this );
return;
}
8 years ago
if( FClassnameIs( pev, "node_viewer_fly" ) )
7 years ago
{
m_iHull = NODE_FLY_HULL;
m_afNodeType = bits_NODE_AIR;
m_vecColor = Vector( 160, 100, 255 );
}
8 years ago
else if( FClassnameIs( pev, "node_viewer_large" ) )
7 years ago
{
m_iHull = NODE_LARGE_HULL;
m_afNodeType = bits_NODE_LAND | bits_NODE_WATER;
m_vecColor = Vector( 100, 255, 160 );
}
else
{
m_iHull = NODE_HUMAN_HULL;
m_afNodeType = bits_NODE_LAND | bits_NODE_WATER;
m_vecColor = Vector( 255, 160, 100 );
}
8 years ago
m_iBaseNode = WorldGraph.FindNearestNode( pev->origin, m_afNodeType );
7 years ago
8 years ago
if( m_iBaseNode < 0 )
7 years ago
{
9 years ago
ALERT( at_console, "No nearby node\n" );
7 years ago
return;
}
m_nVisited = 0;
ALERT( at_aiconsole, "basenode %d\n", m_iBaseNode );
8 years ago
if( WorldGraph.m_cNodes < 128 )
7 years ago
{
8 years ago
for( int i = 0; i < WorldGraph.m_cNodes; i++ )
7 years ago
{
8 years ago
AddNode( i, WorldGraph.NextNodeInRoute( i, m_iBaseNode, m_iHull, 0 ) );
7 years ago
}
}
else
{
// do a depth traversal
FindNodeConnections( m_iBaseNode );
int start = 0;
int end;
8 years ago
do{
// end = m_nVisited;
7 years ago
// ALERT( at_console, "%d :", m_nVisited );
8 years ago
for( end = m_nVisited; start < end; start++ )
7 years ago
{
FindNodeConnections( m_aFrom[start] );
FindNodeConnections( m_aTo[start] );
}
8 years ago
} while( end != m_nVisited );
7 years ago
}
ALERT( at_aiconsole, "%d nodes\n", m_nVisited );
m_iDraw = 0;
9 years ago
SetThink( &CNodeViewer::DrawThink );
7 years ago
SetNextThink( 0 );
}
8 years ago
void CNodeViewer::FindNodeConnections( int iNode )
7 years ago
{
8 years ago
AddNode( iNode, WorldGraph.NextNodeInRoute( iNode, m_iBaseNode, m_iHull, 0 ) );
for( int i = 0; i < WorldGraph.m_pNodes[iNode].m_cNumLinks; i++ )
7 years ago
{
8 years ago
CLink *pToLink = &WorldGraph.NodeLink( iNode, i );
AddNode( pToLink->m_iDestNode, WorldGraph.NextNodeInRoute( pToLink->m_iDestNode, m_iBaseNode, m_iHull, 0 ) );
7 years ago
}
}
void CNodeViewer::AddNode( int iFrom, int iTo )
{
8 years ago
if( m_nVisited >= 128 )
7 years ago
{
return;
}
else
{
8 years ago
if( iFrom == iTo )
7 years ago
return;
8 years ago
for( int i = 0; i < m_nVisited; i++ )
7 years ago
{
8 years ago
if( m_aFrom[i] == iFrom && m_aTo[i] == iTo )
7 years ago
return;
8 years ago
if( m_aFrom[i] == iTo && m_aTo[i] == iFrom )
7 years ago
return;
}
m_aFrom[m_nVisited] = iFrom;
m_aTo[m_nVisited] = iTo;
m_nVisited++;
}
}
8 years ago
void CNodeViewer::DrawThink( void )
7 years ago
{
SetNextThink( 0 );
8 years ago
for( int i = 0; i < 10; i++ )
7 years ago
{
8 years ago
if( m_iDraw == m_nVisited )
7 years ago
{
UTIL_Remove( this );
return;
}
extern short g_sModelIndexLaser;
MESSAGE_BEGIN( MSG_BROADCAST, SVC_TEMPENTITY );
WRITE_BYTE( TE_BEAMPOINTS );
8 years ago
WRITE_COORD( WorldGraph.m_pNodes[m_aFrom[m_iDraw]].m_vecOrigin.x );
WRITE_COORD( WorldGraph.m_pNodes[m_aFrom[m_iDraw]].m_vecOrigin.y );
WRITE_COORD( WorldGraph.m_pNodes[m_aFrom[m_iDraw]].m_vecOrigin.z + NODE_HEIGHT );
7 years ago
8 years ago
WRITE_COORD( WorldGraph.m_pNodes[m_aTo[m_iDraw]].m_vecOrigin.x );
WRITE_COORD( WorldGraph.m_pNodes[m_aTo[m_iDraw]].m_vecOrigin.y );
WRITE_COORD( WorldGraph.m_pNodes[m_aTo[m_iDraw]].m_vecOrigin.z + NODE_HEIGHT );
7 years ago
WRITE_SHORT( g_sModelIndexLaser );
WRITE_BYTE( 0 ); // framerate
WRITE_BYTE( 0 ); // framerate
WRITE_BYTE( 250 ); // life
WRITE_BYTE( 40 ); // width
WRITE_BYTE( 0 ); // noise
WRITE_BYTE( m_vecColor.x ); // r, g, b
WRITE_BYTE( m_vecColor.y ); // r, g, b
WRITE_BYTE( m_vecColor.z ); // r, g, b
WRITE_BYTE( 128 ); // brightness
WRITE_BYTE( 0 ); // speed
MESSAGE_END();
m_iDraw++;
}
}