/** * Activate the server-side path calculation for a given ped * * @param ped Ped which path should be calculated by the server */ public void AddPedMovementCalculcation(Ped ped, bool SetCurrentNavmashPositionsIndex = true) { if (ped.Freeze || ped.Dead || !ped.Wandering) { return; } if (SetCurrentNavmashPositionsIndex) { ped.CurrentNavmashPositionsIndex = GetNearestNavMeshOfPed(ped); } if (ped.CurrentNavmashPositionsIndex < 0 || ped.PathPositions.Count >= ped.CurrentNavmashPositionsIndex) { ped.Wandering = true; return; } ped.Position = ped.PathPositions[ped.CurrentNavmashPositionsIndex].Position; if (ped.PathPositions.Count < ped.CurrentNavmashPositionsIndex + 1) { ped.ContinueWandering(); ped.CurrentNavmashPositionsIndex = 0; } AddPedMovement( (int)Math.Ceiling(Vector3Utils.GetDistanceBetweenPos(ped.Position, ped.PathPositions[ped.CurrentNavmashPositionsIndex + 1].Position)), ped ); }
//Function to determine the nearest navMesh to the ped public static int GetNearestNavMeshOfPed(Ped Ped) { if (Ped.PathPositions.Count == 0) { return(-1); } int MinimumPos = -1; double MinimumDistance = 1000; int i = 0; foreach (IPathElement NavMesh in Ped.PathPositions) { double Distance = Vector3Utils.GetDistanceBetweenPos(Ped.Position, NavMesh.Position); if (MinimumDistance > Distance) { MinimumPos = i; MinimumDistance = Distance; } i++; } return(MinimumPos); }