Exemple #1
0
        private async Task TurnAndReadActualDirection(double desiredDirection, ConsoleKey key)
        {
            // Press Right
            wowProcess.SetKeyState(key, true, true, "PlayerDirection");

            var startTime = DateTime.Now;

            // Wait until we are going the right direction
            while ((DateTime.Now - startTime).TotalSeconds < 10)
            {
                if ((DateTime.Now - startTime).TotalSeconds > 10)
                {
                    await Task.Delay(1);
                }
                System.Threading.Thread.Sleep(1);
                var actualDirection = playerReader.Direction;

                bool closeEnoughToDesiredDirection = Math.Abs(actualDirection - desiredDirection) < 0.01;

                if (closeEnoughToDesiredDirection)
                {
                    logger.LogInformation("Close enough, stopping turn");
                    wowProcess.SetKeyState(key, false, true, "PlayerDirection");
                    break;
                }

                bool goingTheWrongWay = GetDirectionKeyToPress(desiredDirection) != key;
                if (goingTheWrongWay)
                {
                    logger.LogInformation("GOING THE WRONG WAY! Stop turn");
                    wowProcess.SetKeyState(key, false, true, "PlayerDirection");
                    break;
                }
            }
        }
        public async Task Unstick()
        {
            await wowProcess.KeyPress(ConsoleKey.Spacebar, 500);

            logger.LogInformation($"Stuck for {actionDurationSeconds}s, last tried to unstick {unstickSeconds}s ago. Unstick seconds={unstickSeconds}.");

            if (actionDurationSeconds > 240)
            {
                // stuck for 4 minutes
                logger.LogInformation("Stuck for 4 minutes");
                SendActionEvent(new ActionEventArgs(GoapKey.abort, true));
                await Task.Delay(120000);
            }

            if (unstickSeconds > 5)
            {
                int strafeDuration = (int)(1000 + (((double)actionDurationSeconds * 1000) / 12));

                if (strafeDuration > 20000)
                {
                    strafeDuration = 20000;
                }

                if (actionDurationSeconds > 20)
                {
                    // back up a bit, added "remove" move forward
                    wowProcess.SetKeyState(ConsoleKey.DownArrow, true, false, "StuckDetector_back_up");
                    wowProcess.SetKeyState(ConsoleKey.UpArrow, false, false, "StuckDetector");
                    await Task.Delay(strafeDuration);

                    wowProcess.SetKeyState(ConsoleKey.DownArrow, false, false, "StuckDetector");
                }
                this.stopMoving?.Stop();

                // stuck for 20 seconds
                var r = random.Next(0, 100);
                if (r < 50)
                {
                    logger.LogInformation($"Trying to unstick by strafing left for {strafeDuration}ms");
                    wowProcess.SetKeyState(ConsoleKey.A, true, false, "StuckDetector");
                    await Task.Delay(strafeDuration);

                    wowProcess.SetKeyState(ConsoleKey.A, false, false, "StuckDetector");
                }
                else
                {
                    logger.LogInformation($"Trying to unstick by strafing right for {strafeDuration}ms");
                    wowProcess.SetKeyState(ConsoleKey.D, true, false, "StuckDetector");
                    await Task.Delay(strafeDuration);

                    wowProcess.SetKeyState(ConsoleKey.D, false, false, "StuckDetector");
                }

                await wowProcess.TapStopKey();

                wowProcess.SetKeyState(ConsoleKey.UpArrow, true, false, "StuckDetector");

                var heading = DirectionCalculator.CalculateHeading(this.playerReader.PlayerLocation, targetLocation);
                await playerDirection.SetDirection(heading, targetLocation, "Move to next point");

                LastUnstickAttemptTimer.Reset();
                LastUnstickAttemptTimer.Start();
            }
            else
            {
                await wowProcess.KeyPress(ConsoleKey.Spacebar, 500);
            }
        }
        public async Task Unstick()
        {
            await wowProcess.KeyPress(ConsoleKey.Spacebar, 500);

            logger.LogInformation($"Stuck for {actionDurationSeconds}s, last tried to unstick {unstickSeconds}s ago. Unstick seconds={unstickSeconds}.");

            if (actionDurationSeconds > 240)
            {
                // stuck for 4 minutes
                logger.LogInformation("Stuck for 4 minutes");
                SendActionEvent(new ActionEventArgs(GoapKey.abort, true));
                await Task.Delay(120000);
            }

            if (unstickSeconds > 2)
            {
                int actionDuration = (int)(1000 + (((double)actionDurationSeconds * 1000) / 8));

                if (actionDuration > 20000)
                {
                    actionDuration = 20000;
                }

                if (actionDurationSeconds > 10)
                {
                    // back up a bit, added "remove" move forward
                    logger.LogInformation($"Trying to unstick by backing up for {actionDuration}ms");
                    wowProcess.SetKeyState(ConsoleKey.DownArrow, true, false, "StuckDetector_back_up");
                    wowProcess.SetKeyState(ConsoleKey.UpArrow, false, false, "StuckDetector");
                    await Task.Delay(actionDuration);

                    wowProcess.SetKeyState(ConsoleKey.DownArrow, false, false, "StuckDetector");
                }
                this.stopMoving?.Stop();

                // Turn
                var r            = random.Next(0, 2);
                var key          = r == 0 ? ConsoleKey.A : ConsoleKey.D;
                var turnDuration = random.Next(0, 800) + 200;
                logger.LogInformation($"Trying to unstick by turning for {turnDuration}ms");
                wowProcess.SetKeyState(key, true, false, "StuckDetector");
                await Task.Delay(turnDuration);

                wowProcess.SetKeyState(key, false, false, "StuckDetector");

                // Move forward
                var strafeDuration = random.Next(0, 2000) + actionDurationSeconds;
                logger.LogInformation($"Trying to unstick by moving forward after turning for {strafeDuration}ms");
                wowProcess.SetKeyState(ConsoleKey.UpArrow, true, false, "StuckDetector");
                await Task.Delay(strafeDuration);

                await wowProcess.KeyPress(ConsoleKey.Spacebar, 500);

                var heading = DirectionCalculator.CalculateHeading(this.playerReader.PlayerLocation, targetLocation);
                await playerDirection.SetDirection(heading, targetLocation, "Move to next point");

                LastUnstickAttemptTimer.Reset();
                LastUnstickAttemptTimer.Start();
            }
            else
            {
                await wowProcess.KeyPress(ConsoleKey.Spacebar, 500);
            }
        }