private async Task PressKeyAction(KeyAction item)
        {
            playerReader.LastUIErrorMessage = UI_ERROR.NONE;

            await PressKey(item.ConsoleKey, item.Log?item.Name + (item.AfterCastWaitNextSwing ? " and wait for next swing!" : "") : string.Empty, item.PressDuration);

            item.SetClicked();
        }
        public async Task <bool> CastIfReady(KeyAction item, int sleepBeforeCast = 0)
        {
            if (!CanRun(item))
            {
                return(false);
            }

            if (item.ConsoleKey == 0)
            {
                return(false);
            }

            if (!await SwitchToCorrectShapeShiftForm(item))
            {
                return(false);
            }

            if (this.playerReader.IsShooting)
            {
                await input.TapInteractKey("Stop casting shoot");

                await Task.Delay(1500); // wait for shooting to end
            }

            if (sleepBeforeCast > 0)
            {
                item.LogInformation($" Wait before {sleepBeforeCast}.");
                await Task.Delay(sleepBeforeCast);
            }

            await PressKey(item.ConsoleKey, item.Name, item.PressDuration);

            item.SetClicked();

            if (!item.HasCastBar)
            {
                var result = await WaitInterrupt(item.DelayAfterCast,
                                                 () => playerReader.PlayerBitValues.TargetIsDead || playerReader.TargetHealthPercentage < 5);

                item.LogInformation($" ... no castbar delay after cast {result.Item2}ms");
                await InteractOnUIError();
            }
            else
            {
                await playerReader.WaitForNUpdate(1);

                if (!this.playerReader.IsCasting && this.playerReader.HasTarget)
                {
                    await this.InteractOnUIError();

                    item.LogInformation($"Not casting, pressing it again");
                    await PressKey(item.ConsoleKey, item.Name, item.PressDuration);

                    await playerReader.WaitForNUpdate(1);

                    if (!this.playerReader.IsCasting && this.playerReader.HasTarget)
                    {
                        item.LogInformation($"Still not casting !");
                        await this.InteractOnUIError();

                        return(false);
                    }
                }

                item.LogInformation(" waiting for cast bar to end.");
                for (int i = 0; i < 15000; i += 100)
                {
                    if (!this.playerReader.IsCasting)
                    {
                        await playerReader.WaitForNUpdate(1);

                        break;
                    }

                    // wait after cast if the delay is different to the default value
                    if (item.DelayAfterCast != new KeyAction().DelayAfterCast)
                    {
                        item.LogInformation($" ... delay after cast {item.DelayAfterCast}");

                        if (item.DelayUntilCombat) // stop waiting if the mob is targetting me
                        {
                            var sw = new Stopwatch();
                            sw.Start();
                            while (sw.ElapsedMilliseconds < item.DelayAfterCast)
                            {
                                await Task.Delay(10);

                                if (this.playerReader.PlayerBitValues.TargetOfTargetIsPlayer)
                                {
                                    break;
                                }
                            }
                        }
                        else
                        {
                            var result = await WaitInterrupt(item.DelayAfterCast,
                                                             () => playerReader.PlayerBitValues.TargetIsDead || playerReader.TargetHealthPercentage < 5);

                            item.LogInformation($" ... castbar delay after cast {result.Item2}ms");
                        }
                    }

                    await playerReader.WaitForNUpdate(1);
                }
            }
            if (item.StepBackAfterCast > 0)
            {
                await this.input.KeyPress(ConsoleKey.DownArrow, item.StepBackAfterCast, $"Step back for {item.StepBackAfterCast}ms");
            }

            item.ConsumeCharge();
            return(true);
        }
Ejemplo n.º 3
0
        public async Task <bool> CastIfReady(KeyAction item, GoapGoal source, int sleepBeforeCast = 0)
        {
            if (!CanRun(item))
            {
                return(false);
            }

            if (item.ConsoleKey == 0)
            {
                return(false);
            }

            if (!await SwitchToCorrectShapeShiftForm(item))
            {
                return(false);
            }

            if (this.playerReader.IsShooting)
            {
                await TapInteractKey("Stop casting shoot");

                await Task.Delay(1500); // wait for shooting to end
            }

            if (sleepBeforeCast > 0)
            {
                item.LogInformation($" Wait before {sleepBeforeCast}.");
                await Task.Delay(sleepBeforeCast);
            }

            await PressKey(item.ConsoleKey, item.Name, item.PressDuration);

            item.SetClicked();

            if (!item.HasCastBar)
            {
                item.LogInformation($" ... delay after cast {item.DelayAfterCast}");
                await Task.Delay(item.DelayAfterCast);
            }
            else
            {
                await Task.Delay(300);

                if (!this.playerReader.IsCasting && this.playerReader.HasTarget)
                {
                    await this.InteractOnUIError();

                    item.LogInformation($"Not casting, pressing it again");
                    await PressKey(item.ConsoleKey, item.Name, item.PressDuration);

                    await Task.Delay(300);

                    if (!this.playerReader.IsCasting && this.playerReader.HasTarget)
                    {
                        item.LogInformation($"Still not casting !");
                        await this.InteractOnUIError();

                        return(false);
                    }
                }

                item.LogInformation(" waiting for cast bar to end.");
                for (int i = 0; i < 15000; i += 100)
                {
                    if (!this.playerReader.IsCasting)
                    {
                        await Task.Delay(100);

                        break;
                    }

                    // wait after cast if the delay is different to the default value
                    if (item.DelayAfterCast != new KeyAction().DelayAfterCast)
                    {
                        item.LogInformation($" ... delay after cast {item.DelayAfterCast}");

                        if (item.DelayUntilCombat) // stop waiting if the mob is targetting me
                        {
                            var sw = new Stopwatch();
                            sw.Start();
                            while (sw.ElapsedMilliseconds < item.DelayAfterCast)
                            {
                                await Task.Delay(10);

                                if (this.playerReader.PlayerBitValues.TargetOfTargetIsPlayer)
                                {
                                    break;
                                }
                            }
                        }
                        else
                        {
                            await Task.Delay(item.DelayAfterCast);
                        }
                    }

                    if (source.GetType() == typeof(PullTargetGoal) &&
                        this.playerReader.PlayerBitValues.PlayerInCombat &&
                        !this.playerReader.PlayerBitValues.TargetOfTargetIsPlayer &&
                        this.playerReader.IsCasting &&
                        this.playerReader.TargetHealthPercentage > 99
                        )
                    {
                        if (!this.playerReader.TargetIsFrostbitten)
                        {
                            await this.wowProcess.KeyPress(ConsoleKey.UpArrow, 200, "Stop cast as picked up an add, my mob is not targetting me.");

                            await wowProcess.KeyPress(ConsoleKey.F3, 400); // clear target

                            break;
                        }
                    }

                    await Task.Delay(100);
                }
            }
            if (item.StepBackAfterCast > 0)
            {
                await this.wowProcess.KeyPress(ConsoleKey.DownArrow, item.StepBackAfterCast * 1000, "Step back hero");
            }
            return(true);
        }
        private async Task ReactToLastCastingEvent(KeyAction item, string source)
        {
            switch ((UI_ERROR)playerReader.CastEvent.Value)
            {
            case UI_ERROR.NONE:
                break;

            case UI_ERROR.ERR_SPELL_FAILED_INTERRUPTED:
                item.SetClicked();
                break;

            case UI_ERROR.CAST_START:
                break;

            case UI_ERROR.CAST_SUCCESS:
                break;

            case UI_ERROR.ERR_SPELL_COOLDOWN:
                logger.LogInformation($"{source} React to {UI_ERROR.ERR_SPELL_COOLDOWN} -- wait until its ready");
                bool before = addonReader.UsableAction.Is(item);
                await wait.While(() => before != addonReader.UsableAction.Is(item));

                break;

            case UI_ERROR.ERR_SPELL_FAILED_STUNNED:
                int debuffCount = playerReader.AuraCount.PlayerDebuff;
                if (debuffCount != 0)
                {
                    logger.LogInformation($"{source} -- React to {UI_ERROR.ERR_SPELL_FAILED_STUNNED} -- Wait till losing debuff!");
                    await wait.While(() => debuffCount == playerReader.AuraCount.PlayerDebuff);
                }
                else
                {
                    logger.LogInformation($"{source} -- Didn't know how to react {UI_ERROR.ERR_SPELL_FAILED_STUNNED} when PlayerDebuffCount: {debuffCount}");
                }

                break;

            case UI_ERROR.ERR_SPELL_OUT_OF_RANGE:
                if (playerReader.Class == PlayerClassEnum.Hunter && playerReader.IsInMeleeRange)
                {
                    logger.LogInformation($"{source} -- As a Hunter didn't know how to react {UI_ERROR.ERR_SPELL_OUT_OF_RANGE}");
                    return;
                }

                float minRange = playerReader.MinRange;
                if (playerReader.Bits.PlayerInCombat && playerReader.HasTarget && !playerReader.IsTargetCasting)
                {
                    await wait.Update(2);

                    if (playerReader.TargetTarget == TargetTargetEnum.TargetIsTargettingMe)
                    {
                        logger.LogInformation($"{source} -- React to {UI_ERROR.ERR_SPELL_OUT_OF_RANGE} -- Just wait for the target to get in range.");

                        (bool inputNotHappened, double inputElapsedMs) = await wait.InterruptTask(MaxWaitCastTimeMs,
                                                                                                  () => minRange != playerReader.MinRange || playerReader.IsTargetCasting
                                                                                                  );
                    }
                }
                else
                {
                    double beforeDirection = playerReader.Direction;
                    await input.TapInteractKey("");

                    await input.TapStopAttack();

                    await stopMoving.Stop();

                    await wait.Update(1);

                    if (beforeDirection != playerReader.Direction)
                    {
                        await input.TapInteractKey("");

                        (bool inputNotHappened, double inputElapsedMs) = await wait.InterruptTask(MaxWaitCastTimeMs,
                                                                                                  () => minRange != playerReader.MinRange);

                        logger.LogInformation($"{source} -- React to {UI_ERROR.ERR_SPELL_OUT_OF_RANGE} -- Approached target {minRange}->{playerReader.MinRange}");
                    }
                    else
                    {
                        logger.LogInformation($"{source} -- React to {UI_ERROR.ERR_SPELL_OUT_OF_RANGE} -- Start moving forward");
                        input.SetKeyState(ConsoleKey.UpArrow, true, false, "");
                    }
                }

                break;

            case UI_ERROR.ERR_BADATTACKFACING:
                if (playerReader.IsInMeleeRange)
                {
                    logger.LogInformation($"{source} -- React to {UI_ERROR.ERR_BADATTACKFACING} -- Interact!");
                    await input.TapInteractKey("");
                }
                else
                {
                    logger.LogInformation($"{source} -- React to {UI_ERROR.ERR_BADATTACKFACING} -- Turning 180!");

                    double desiredDirection = playerReader.Direction + Math.PI;
                    desiredDirection = desiredDirection > Math.PI * 2 ? desiredDirection - (Math.PI * 2) : desiredDirection;
                    await direction.SetDirection(desiredDirection, new WowPoint(0, 0), "");

                    await wait.Update(1);
                }

                break;

            case UI_ERROR.SPELL_FAILED_MOVING:
                logger.LogInformation($"{source} -- React to {UI_ERROR.SPELL_FAILED_MOVING} -- Stop moving!");
                await stopMoving.Stop();

                await wait.Update(1);

                break;

            case UI_ERROR.ERR_SPELL_FAILED_ANOTHER_IN_PROGRESS:
                logger.LogInformation($"{source} -- React to {UI_ERROR.ERR_SPELL_FAILED_ANOTHER_IN_PROGRESS} -- Wait till casting!");
                await wait.While(() => playerReader.IsCasting);

                break;

            case UI_ERROR.ERR_BADATTACKPOS:
                if (playerReader.Bits.IsAutoRepeatSpellOn_AutoAttack)
                {
                    logger.LogInformation($"{source} -- React to {UI_ERROR.ERR_BADATTACKPOS} -- Interact!");
                    await input.TapInteractKey("");

                    await stopMoving.Stop();

                    await wait.Update(1);
                }
                else
                {
                    logger.LogInformation($"{source} -- Didn't know how to React to {(UI_ERROR)playerReader.CastEvent.Value}");
                }

                break;

            default:
                logger.LogInformation($"{source} -- Didn't know how to React to {(UI_ERROR)playerReader.CastEvent.Value}");

                break;
                //case UI_ERROR.ERR_SPELL_FAILED_S:
                //case UI_ERROR.ERR_BADATTACKPOS:
                //case UI_ERROR.ERR_SPELL_OUT_OF_RANGE:
                //case UI_ERROR.ERR_AUTOFOLLOW_TOO_FAR:
                //    this.playerReader.LastUIErrorMessage = UI_ERROR.NONE;
                //    break;
            }
        }