Exemple #1
0
            private void Run()
            {
                while (true)
                {
                    try
                    {
                        this.ResetEvent.Wait();
                        if (this.TargetExecutedBegin != null)
                        {
                            this.TargetExecutedBegin(this.CurrentTarget);
                        }

                        Objects.Client   client      = this.Parent.Client;
                        Objects.Creature oldCreature = null;

                        while (!this.Cancel)
                        {
                            this.ResetEventCacheUpdated.WaitOne();

                            if (this.Cancel)
                            {
                                break;
                            }

                            // get best setting
                            Target.Setting setting = this.CurrentTarget.GetBestSetting(this.CachedCreatures,
                                                                                       this.CachedPlayers, this.CachedTiles, true);
                            if (setting == null)
                            {
                                break;
                            }

                            // check if we should revert to older creature if sticky is on
                            // or if new creature is in no better position
                            if (oldCreature != null &&
                                oldCreature != this.CurrentTarget.Creature &&
                                oldCreature.IsVisible &&
                                client.Player.Location.IsOnScreen(oldCreature.Location))
                            {
                                if (this.Parent.CurrentSettings.StickToCreature)
                                {
                                    this.CurrentTarget.Creature = oldCreature;
                                }
                                else
                                {
                                    int oldDist = (int)client.Player.Location.DistanceTo(oldCreature.Location),
                                        newDist = this.CurrentTarget.Creature != null ?
                                                  (int)client.Player.Location.DistanceTo(this.CurrentTarget.Creature.Location) :
                                                  100;
                                    if (oldDist < newDist + 2)
                                    {
                                        this.CurrentTarget.Creature = oldCreature;
                                    }
                                }
                            }
                            oldCreature = this.CurrentTarget.Creature;

                            Objects.Location playerLoc = client.Player.Location,
                                             targetLoc = this.CurrentTarget.Creature.Location;

                            #region targeting checks and whatnot
                            // check if the creature is dead
                            if (this.CurrentTarget.Creature.IsDead)
                            {
                                break;
                            }

                            // check if creature is about to become a corpse
                            if (this.CurrentTarget.Creature.IsVisible &&
                                this.CurrentTarget.Creature.HealthPercent == 0)
                            {
                                continue;
                            }

                            // check if creature is still on screen, stop attacking and looping if so
                            if (!playerLoc.IsOnScreen(targetLoc))
                            {
                                this.CancelAttack();
                                break;
                            }

                            // check if creature if reachable and/or shootable
                            // stop attacking and break if settings says they must be
                            if ((setting.MustBeReachable && !this.CurrentTarget.Creature.IsReachable(this.CachedTiles, this.Parent.PathFinder)) ||
                                (setting.MustBeShootable && !this.CurrentTarget.Creature.IsShootable(this.CachedTiles)))
                            {
                                this.CancelAttack();
                                break;
                            }

                            // check if player is attacking the wrong target
                            if (client.Player.Target != this.CurrentTarget.Creature.ID)
                            {
                                this.CurrentTarget.Creature.Attack();
                                Thread.Sleep(100);
                            }

                            // set fight stance+mode and move accordingly
                            Map.Tile playerTile   = this.CachedTiles.GetTile(count: client.Player.ID),
                                     creatureTile = this.CachedTiles.GetTile(count: this.CurrentTarget.Creature.ID);
                            if (playerTile == null || creatureTile == null)
                            {
                                this.CancelAttack();
                                break;
                            }
                            switch (setting.FightStance)
                            {
                            case Enums.FightStance.Stand:
                                if (client.Player.FightStance != Enums.FightStance.Stand)
                                {
                                    client.Player.FightStance = Enums.FightStance.Stand;
                                }
                                break;

                            case Enums.FightStance.Follow:
                                if (client.Player.FightStance != Enums.FightStance.Follow)
                                {
                                    client.Player.FightStance = Enums.FightStance.Follow;
                                }
                                break;

                            case Enums.FightStance.FollowDiagonalOnly:
                            case Enums.FightStance.DistanceFollow:
                            case Enums.FightStance.DistanceWait:
                            case Enums.FightStance.FollowStrike:
                                if (client.Player.FightStance != Enums.FightStance.Stand)
                                {
                                    client.Player.FightStance = Enums.FightStance.Stand;
                                }
                                if (client.Player.IsWalking)
                                {
                                    break;
                                }
                                Objects.Location bestLoc = this.CurrentTarget.GetBestLocation(setting, this.CachedTiles, this.CachedCreatures);
                                if (bestLoc.IsValid() && client.Player.GoTo != bestLoc)
                                {
                                    client.Player.GoTo = bestLoc;
                                }
                                break;
                            }

                            // shoot rune or spell
                            if (client.Player.HealthPercent >= this.Parent.CurrentSettings.MinimumHealthToShoot &&
                                this.CurrentTarget.Creature.HealthPercent > 0)
                            {
                                ushort runeID = 0;
                                if ((runeID = setting.GetRuneID()) != 0)
                                {
                                    Objects.Item rune = client.Inventory.GetItem(runeID);
                                    if (rune != null)
                                    {
                                        if (!this.StopwatchExhaust.IsRunning ||
                                            this.StopwatchExhaust.ElapsedMilliseconds >= this.Parent.CurrentSettings.Exhaust)
                                        {
                                            if (setting.RuneIsAoE()) // todo: test this
                                            {
                                                Map.Tile aoeTile = this.Parent.GetAreaEffectTile(setting.GetAreaEffect(),
                                                                                                 this.CachedTiles, this.CurrentTarget.Creature);
                                                if (aoeTile != null)
                                                {
                                                    rune.UseOnTile(aoeTile);
                                                    this.StopwatchExhaust.Restart();
                                                }
                                            }
                                            else if (this.CurrentTarget.Creature.IsShootable(this.CachedTiles))
                                            {
                                                rune.UseOnBattleList(this.CurrentTarget.Creature);
                                                this.StopwatchExhaust.Restart();
                                            }
                                        }
                                    }
                                }
                                else if (!string.IsNullOrEmpty(setting.Spell))
                                {
                                    // todo: add aoe spells
                                    if (playerLoc.IsAdjacentNonDiagonalOnly(targetLoc))
                                    {
                                        Enums.Direction direction = Enums.Direction.Down;
                                        int             diffX     = playerLoc.X - targetLoc.X,
                                                        diffY = playerLoc.Y - targetLoc.Y;
                                        if (diffX > 0)
                                        {
                                            direction = Enums.Direction.Left;
                                        }
                                        else if (diffX < 0)
                                        {
                                            direction = Enums.Direction.Right;
                                        }
                                        else if (diffY > 0)
                                        {
                                            direction = Enums.Direction.Up;
                                        }
                                        else if (diffY < 0)
                                        {
                                            direction = Enums.Direction.Down;
                                        }
                                        if (client.Player.Direction != direction)
                                        {
                                            client.Packets.Turn(direction);
                                            for (int i = 0; i < 6; i++)
                                            {
                                                Thread.Sleep(50);
                                                if ((Enums.Direction)client.Player.Direction == direction)
                                                {
                                                    break;
                                                }
                                            }
                                        }
                                        if (client.Player.Direction == direction &&
                                            (!this.StopwatchExhaust.IsRunning || this.StopwatchExhaust.ElapsedMilliseconds > this.Parent.CurrentSettings.Exhaust))
                                        {
                                            client.Packets.Say(setting.Spell);
                                            this.StopwatchExhaust.Restart();
                                        }
                                    }
                                }
                            }
                            #endregion
                        }

                        if (this.TargetExecutedEnd != null)
                        {
                            this.TargetExecutedEnd(this.CurrentTarget);
                        }
                        this.ResetEvent.Reset();
                        this.Cancel = false;
                    }
                    catch (Exception ex)
                    {
                        if (this.ErrorOccurred != null)
                        {
                            this.ErrorOccurred(ex);
                        }
                    }
                }
            }