fBaseXtensions.Navigation.PlayerMover.UnstuckHandler C# (CSharp) Method

UnstuckHandler() public static method

public static UnstuckHandler ( System.Vector3 vMyCurrentPosition, System.Vector3 vOriginalDestination ) : System.Vector3
vMyCurrentPosition System.Vector3
vOriginalDestination System.Vector3
return System.Vector3
        public static Vector3 UnstuckHandler(Vector3 vMyCurrentPosition, Vector3 vOriginalDestination)
        {
            // Update the last time we generated a path
            timeStartedUnstuckMeasure = DateTime.Now;

            // If we got stuck on a 2nd/3rd/4th "chained" anti-stuck route, then return the old move to target to keep movement of some kind going
            if (iTimesReachedStuckPoint > 0)
            {
                vSafeMovementLocation = Vector3.Zero;
                iTimesReachedStuckPoint++;
                // Reset the path and allow a whole "New" unstuck generation next cycle
                iTimesReachedStuckPoint = 0;
                // And cancel unstucking for 9 seconds so DB can try to navigate
                iCancelUnstuckerForSeconds = (9 * iTotalAntiStuckAttempts);
                if (iCancelUnstuckerForSeconds < 20)
                    iCancelUnstuckerForSeconds = 20;
                timeCancelledUnstuckerFor = DateTime.Now;
                Navigator.Clear();
                Logger.DBLog.DebugFormat("[Funky] Clearing old route and trying new path find to: " + vOldMoveToTarget.ToString());
                Navigator.MoveTo(vOldMoveToTarget, "original destination", false);
                return vSafeMovementLocation;
            }

            // Only try an unstuck 10 times maximum in XXX period of time
            if (Vector3.Distance(vOriginalDestination, vMyCurrentPosition) >= 700f)
            {
                Logger.DBLog.InfoFormat("[Funky] You are " + Vector3.Distance(vOriginalDestination, vMyCurrentPosition).ToString(CultureInfo.InvariantCulture) + " distance away from your destination.");
                Logger.DBLog.InfoFormat("[Funky] This is too far for the unstucker, and is likely a sign of ending up in the wrong map zone.");
                Logger.DBLog.InfoFormat("Reloading current profile");
                ProfileManager.Load(ProfileManager.CurrentProfile.Path);

            }
            if (iTotalAntiStuckAttempts <= 8)
            {
                //Check cache for barricades..
                if (ObjectCache.Objects.OfType<CacheDestructable>().Any(CO => CO.RadiusDistance <= 12f))
                {
                    Logger.DBLog.InfoFormat("[Funky] Found nearby barricade, flagging barricade destruction!");
                    ShouldHandleObstacleObject = true;
                }

                Logger.DBLog.InfoFormat("[Funky] Your bot got stuck! Trying to unstuck (attempt #" + iTotalAntiStuckAttempts.ToString(CultureInfo.InvariantCulture) + " of 8 attempts)");
                Logger.DBLog.DebugFormat("(destination=" + vOriginalDestination.ToString() + ", which is " + Vector3.Distance(vOriginalDestination, vMyCurrentPosition).ToString(CultureInfo.InvariantCulture) + " distance away)");

                Logger.Write(LogLevel.Movement, "Stuck Flags: {0}", FunkyGame.Hero.Stuckflags.ToString());

                bool FoundRandomMovementLocation = FunkyGame.Navigation.AttemptFindSafeSpot(out vSafeMovementLocation, Vector3.Zero, PointCheckingFlags.RaycastWalkable);

                // Temporarily log stuff
                if (iTotalAntiStuckAttempts == 1 && FunkyBaseExtension.Settings.Debugging.LogStuckLocations)
                {

                    string outputPath = FolderPaths.LoggingFolderPath + @"\Stucks.log";

                    FileStream LogStream = File.Open(outputPath, FileMode.Append, FileAccess.Write, FileShare.Read);
                    using (StreamWriter LogWriter = new StreamWriter(LogStream))
                    {
                        LogWriter.WriteLine(DateTime.Now.ToString(CultureInfo.InvariantCulture) + ": Original Destination=" + vOldMoveToTarget.ToString() + ". Current player position when stuck=" + vMyCurrentPosition.ToString());
                        LogWriter.WriteLine("Profile Name=" + ProfileManager.CurrentProfile.Name);
                    }
                }

                if (iTotalAntiStuckAttempts == 2)
                {
                    //Navigator.Clear();
                    Logger.DBLog.InfoFormat("Using Navigator StuckHandler To Generate Vector3");
                    vSafeMovementLocation = Navigator.StuckHandler.GetUnstuckPos();
                    FoundRandomMovementLocation = true;
                }

                // Now count up our stuck attempt generations
                iTotalAntiStuckAttempts++;

                if (FoundRandomMovementLocation)
                {
                    return vSafeMovementLocation;
                }
                Navigator.Clear();
                Navigator.MoveTo(vOriginalDestination, "original destination", false);
                iCancelUnstuckerForSeconds = 40;
                timeCancelledUnstuckerFor = DateTime.Now;
                return Vector3.Zero;
            }

            iTimesReachedMaxUnstucks++;
            iTotalAntiStuckAttempts = 1;
            vSafeMovementLocation = Vector3.Zero;
            vOldPosition = Vector3.Zero;
            iTimesReachedStuckPoint = 0;
            timeLastRecordedPosition = DateTime.Today;
            timeStartedUnstuckMeasure = DateTime.Today;
            int iSafetyLoops = 0;

            if (iTimesReachedMaxUnstucks == 1)
            {
                Navigator.Clear();

                Logger.DBLog.InfoFormat("[Funky] Anti-stuck measures now attempting to kickstart DB's path-finder into action.");
                if (Vector3.Distance(vOriginalDestination, vMyCurrentPosition) >= 200f)
                {
                    iTimesReachedMaxUnstucks++;
                }
                else
                {

                    Navigator.MoveTo(vOriginalDestination, "original destination", false);
                    iCancelUnstuckerForSeconds = 40;
                    timeCancelledUnstuckerFor = DateTime.Now;
                    return vSafeMovementLocation;
                }
            }

            if (iTimesReachedMaxUnstucks == 2)
            {
                Logger.DBLog.InfoFormat("[Funky] Anti-stuck measures failed. Now attempting to reload current profile.");
                ExitGameBehavior.ShouldExitGame = true;
            }
            // Exit the game and reload the profile
            if (FunkyBaseExtension.Settings.Debugging.RestartGameOnLongStucks && DateTime.Now.Subtract(timeLastRestartedGame).TotalMinutes >= 15)
            {
                timeLastRestartedGame = DateTime.Now;
                ExitGameBehavior.ShouldExitGame = true;
            }
            else
            {
                Logger.DBLog.InfoFormat("[Funky] Unstucking measures failed. Now stopping Trinity unstucker for 12 minutes to inactivity timers to kick in or DB to auto-fix.");
                iCancelUnstuckerForSeconds = 720;
                timeCancelledUnstuckerFor = DateTime.Now;
                return vSafeMovementLocation;
            }
            return vSafeMovementLocation;
        }