View Single Post
arthurdead
Senior Member
Join Date: Jul 2013
Old 12-01-2014 , 19:17   Re: Using Navigation Meshes to find path?
Reply With Quote #3

copy-paste (with slight changes) from sm_navmesh_buildpath

PHP Code:
    new Float:EntityPos[3];
    
GetEntPropVector(entityProp_Data"m_vecAbsOrigin"EntityPos);
    new 
target FindNearestPlayer(entity);
    if(
target != -1)
    {
        new 
Float:TargetPos[3];
        
GetClientAbsOrigin(targetTargetPos);
        new 
iClosestAreaIndex 0;
        new 
bool:bBuiltPath NavMesh_BuildPath(NavMesh_GetNearestArea(EntityPos), NavMesh_GetNearestArea(TargetPos), TargetPosNavMeshShortestPathCost_iClosestAreaIndex0.0);
        if(
bBuiltPath)
        {
            new 
iTempAreaIndex iClosestAreaIndex;
            new 
iParentAreaIndex NavMeshArea_GetParent(iTempAreaIndex);
            new 
iNavDirection;
            new 
Float:flHalfWidth;
            new 
Float:flCenterPortal[3];
            new 
Float:flClosestPoint[3];
            
hPositions[entity] = CreateArray(3);
            
PushArrayArray(hPositions[entity], TargetPos3);
            while(
iParentAreaIndex != -1)
            {
                new 
Float:flTempAreaCenter[3];
                new 
Float:flParentAreaCenter[3];
                
NavMeshArea_GetCenter(iTempAreaIndexflTempAreaCenter);
                
NavMeshArea_GetCenter(iParentAreaIndexflParentAreaCenter);
                
iNavDirection NavMeshArea_ComputeDirection(iTempAreaIndexflParentAreaCenter);
                
NavMeshArea_ComputePortal(iTempAreaIndexiParentAreaIndexiNavDirectionflCenterPortalflHalfWidth);
                
NavMeshArea_ComputeClosestPointInPortal(iTempAreaIndexiParentAreaIndexiNavDirectionflCenterPortalflClosestPoint);
                
flClosestPoint[2] = NavMeshArea_GetZ(iTempAreaIndexflClosestPoint);
                
PushArrayArray(hPositions[entity], flClosestPoint3);
                
iTempAreaIndex iParentAreaIndex;
                
iParentAreaIndex NavMeshArea_GetParent(iTempAreaIndex);
            }
            
PushArrayArray(hPositions[entity], EntityPos3);
            new 
Float:flFromPos[3];
            
GetArrayArray(hPositions[entity], GetArraySize(hPositions[entity])-2flFromPos3);
            
decl Float:vecDistance[3];
            for (new 
03j++)
            {
                
vecDistance[j] = flFromPos[j] - EntityPos[j];
            }
            new 
Float:angles[3];
            
GetVectorAngles(vecDistanceangles);
            
NormalizeVector(vecDistancevecDistance);
            
ScaleVector(vecDistance1000.0 GetTickInterval());
            
AddVectors(vecDistanceEntityPosvecDistance);
            
angles[0] = 0.0;
            
TeleportEntity(entityvecDistanceanglesNULL_VECTOR);
            for(new 
GetArraySize(hPositions[entity]) - 10i--)
            {
                
decl Float:flFromPos2[3], Float:flToPos[3];
                
GetArrayArray(hPositions[entity], iflFromPos23);
                
GetArrayArray(hPositions[entity], 1flToPos3);
                new 
laser PrecacheModel("materials/sprites/laserbeam.vmt");
                
TE_SetupBeamPoints(flFromPos2flToPoslaserlaser0300.15.05.050.0, {02550255}, 30);
                
TE_SendToAll();
            }
        }
    } 
arthurdead is offline