// Copyright (C) 2007 Id Software, Inc. // #pragma hdrstop #include "precompiled.h" #include "NAV_local.h" #include "../Game_local.h" // for print and error #include "NAVCallback_AvoidLocation.h" /* ============ idNAVCallback_AvoidLocation::idNAVCallback_AvoidLocation ============ */ idNAVCallback_AvoidLocation::idNAVCallback_AvoidLocation() { avoidLocation.Zero(); avoidDist = 0.0f; obstacles = NULL; numObstacles = 0; } /* ============ idNAVCallback_AvoidLocation::~idNAVCallback_AvoidLocation ============ */ idNAVCallback_AvoidLocation::~idNAVCallback_AvoidLocation() { } /* ============ idNAVCallback_AvoidLocation::SetAvoidLocation ============ */ void idNAVCallback_AvoidLocation::SetAvoidLocation( const idVec3& start, const idVec3& avoidLocation ) { this->avoidLocation = avoidLocation; this->avoidDist = ( avoidLocation - start ).Length(); } /* ============ idNAVCallback_AvoidLocation::SetObstacles ============ */ void idNAVCallback_AvoidLocation::SetObstacles( const idNAV* aas, const idNAVObstacle* obstacles, int numObstacles ) { this->obstacles = obstacles; this->numObstacles = numObstacles; for( int i = 0; i < numObstacles; i++ ) { obstacles[i].expAbsBounds[0] = obstacles[i].absBounds[0] - aas->GetSettings()->boundingBoxes[0][1]; obstacles[i].expAbsBounds[1] = obstacles[i].absBounds[1] - aas->GetSettings()->boundingBoxes[0][0]; } } /* ============ idNAVCallback_AvoidLocation::PathValid ============ */ bool idNAVCallback_AvoidLocation::PathValid( const idNAV* aas, const idVec3& start, const idVec3& end ) { // path may not go through any obstacles for( int i = 0; i < numObstacles; i++ ) { // if the movement vector intersects the expanded obstacle bounds if( obstacles[i].expAbsBounds.LineIntersection( start, end ) ) { return false; } } return true; } /* ============ idNAVCallback_AvoidLocation::AdditionalTravelTimeForPath ============ */ int idNAVCallback_AvoidLocation::AdditionalTravelTimeForPath( const idNAV* aas, const idVec3& start, const idVec3& end ) { if( avoidDist <= 0.0f ) { return 0; } // project avoidLocation origin onto movement vector idVec3 v1 = end - start; v1.Normalize(); idVec3 v2 = avoidLocation - start; idVec3 p = start + ( v2 * v1 ) * v1; // get the point on the path closest to the avoidLocation int i; for( i = 0; i < 3; i++ ) { if( ( p[i] > start[i] + 0.1f && p[i] > end[i] + 0.1f ) || ( p[i] < start[i] - 0.1f && p[i] < end[i] - 0.1f ) ) { break; } } float dist; if( i >= 3 ) { dist = ( avoidLocation - p ).Length(); } else { dist = ( avoidLocation - end ).Length(); } // avoid moving closer to the avoidLocation if( dist < avoidDist ) { return idMath::Ftoi( ( avoidDist - dist ) * 10.0f ); } return 0; }