/// <summary> /// Attempts to use this item on a creature in the battlelist. /// </summary> /// <param name="c">The creature to use this item on.</param> public void UseOnBattleList(Objects.Creature c) { if (c == null) { return; } this.Client.Packets.UseItemOnBattleList(this.ToItemLocation(), c); }
/// <summary> /// Attempts to use this item on a creature or player. /// </summary> /// <param name="c">The creature or player to use this item on.</param> public void UseOnCreature(Objects.Creature c) { if (c == null) { return; } this.UseOnLocation(c.Location); }
/// <summary> /// Attempts to use an item on the battlelist. Useful for shooting runes on creatures. /// </summary> /// <param name="itemLocation">The item's location.</param> /// <param name="c">The creature to use the item on..</param> public void UseItemOnBattleList(Objects.ItemLocation itemLocation, Objects.Creature c) { if (itemLocation == null || c == null) { return; } this.FunctionUseItemOnBattleList.Call(itemLocation.WorldLocation.X, itemLocation.WorldLocation.Y, itemLocation.WorldLocation.Z, itemLocation.ItemID, itemLocation.StackIndex, c.ID); if (this.ItemUsedOnBattleList != null) { this.ItemUsedOnBattleList(itemLocation, c); } }
/// <summary> /// Attempts to attack a creature, player or NPC. /// </summary> /// <param name="c">The Objects.Creature object that represents a creature, player or NPC.</param> public void Attack(Objects.Creature c) { if (c == null) { return; } uint targetID = c.ID; if (this.Client.Player.Target == targetID) { targetID = 0; } if (targetID != 0 && this.Client.Player.Z != c.Z) { return; } this.Attack(targetID); }
/// <summary> /// Use an item on a creature. /// </summary> /// <param name="onCreature"></param> /// <returns></returns> public bool Use(Objects.Creature onCreature) { return(Packets.Outgoing.ItemUseOnPacket.Send(client, location.ToLocation(), (ushort)id, location.ToBytes()[4], onCreature.Location, 0x63, 0)); }
private void HandleServer() { try { this.TcpClient = new TcpClient(this.ServerIP, this.ServerPort); this.IsConnected = true; if (this.Connected != null) { this.Connected(); } Objects.Item item; Objects.Packet p = new Objects.Packet(); p.AddByte((byte)PacketType.PlayerInfo); p.AddString(this.Client.Player.Name); p.AddLength(); p.Send(this.TcpClient); while (true) { p = Objects.Packet.GetNextPacket(this.TcpClient.GetStream()); if (p.Length == 0) { break; // disconnected } p.GetUInt16(); // length, not needed switch ((PacketType)p.GetByte()) { case PacketType.Combo: if (!this.Client.Player.Connected) { continue; } uint targetid = p.GetUInt32(); Objects.Creature c = this.Client.BattleList.GetAny(targetid); if (c == null || !this.Client.Player.Location.IsOnScreen(c.Location)) { break; } switch (this.Action) { case ActionType.Say: if (string.IsNullOrEmpty(this.ActionEx)) { break; } this.Client.Packets.Say(this.ActionEx); break; case ActionType.CustomRune: ushort runeid = 0; if (!ushort.TryParse(this.ActionEx, out runeid)) { break; } item = this.Client.Inventory.GetItem(runeid); if (item != null) { item.UseOnCreature(c); } break; case ActionType.SuddenDeath: item = this.Client.Inventory.GetItem(this.Client.ItemList.Runes.SuddenDeath); if (item != null) { item.UseOnCreature(c); } break; case ActionType.HeavyMagicMissile: item = this.Client.Inventory.GetItem(this.Client.ItemList.Runes.HeavyMagicMissile); if (item != null) { item.UseOnCreature(c); } break; case ActionType.Explosion: item = this.Client.Inventory.GetItem(this.Client.ItemList.Runes.Explosion); if (item != null) { item.UseOnCreature(c); } break; case ActionType.Paralyze: item = this.Client.Inventory.GetItem(this.Client.ItemList.Runes.Paralyze); if (item != null) { item.UseOnCreature(c); } break; } break; case PacketType.Ping: p = new Objects.Packet(); p.AddByte((byte)PacketType.Ping); p.AddLength(); p.Send(this.TcpClient); break; case PacketType.PlayerInfo: p = new Objects.Packet(); p.AddByte((byte)PacketType.PlayerInfo); p.AddString(this.Client.Player.Name); p.AddLength(); p.Send(this.TcpClient); break; } } } catch { } finally { if (this.TcpClient != null) { this.TcpClient.Close(); } this.IsConnected = false; if (this.Disconnected != null) { this.Disconnected(); } } }
/// <summary> /// Attempts to get the best setting for this target. Also sets CurrentSettingIndex. Returns null if unsuccessful. /// </summary> /// <param name="creaturesOnScreen">A collection of creatures visible on screen.</param> /// <param name="playersOnScreen">A collection of players visible on screen.</param> /// <param name="tileCollection">A collection of tiles visible on screen.</param> /// <param name="setCreature">Whether to set this target's Creature.</param> /// <returns></returns> public Target.Setting GetBestSetting(IEnumerable <Objects.Creature> creaturesOnScreen, IEnumerable <Objects.Creature> playersOnScreen, Map.TileCollection tileCollection, bool setCreature) { // check if there are any settings to use bool found = false; foreach (Target.Setting s in this.GetSettings()) { if (s.UseThisSetting) { found = true; break; } } if (!found) { return(null); } // set up the player's tile and other variables Map.Tile playerTile = tileCollection.GetTile(count: this.Parent.Client.Player.ID); if (playerTile == null) { return(null); } List <Objects.Creature> creatures = new List <Objects.Creature>(), players = new List <Objects.Creature>(); foreach (Objects.Creature c in creaturesOnScreen) { if (c.Name.ToLower() == this.Name.ToLower()) { creatures.Add(c); } } foreach (Objects.Creature p in playersOnScreen.ToArray()) { if (p.ID != this.Parent.Client.Player.ID) { players.Add(p); } } // calculate best setting int bestCount = 0, bestIndex = 0, index = 0; Target.Setting bestSetting = null; Objects.Creature bestCreature = null; foreach (Target.Setting setting in this.GetSettings()) { if (!setting.UseThisSetting) { continue; } int count = 0, bestCreatureDistance = setting.Range + 1; Objects.Creature tempCreature = null; foreach (Objects.Creature c in creatures) { if (!c.IsVisible) { continue; } Map.Tile creatureTile = tileCollection.GetTile(count: c.ID); if (creatureTile == null) { continue; } if (!playerTile.WorldLocation.IsOnScreen(creatureTile.WorldLocation)) { continue; } if (this.Parent.CurrentSettings.FriendlyMode && players.Count > 0 && !c.HasAttackedMeRecently(4000)) { continue; } if (setting.MustBeShootable && !c.IsShootable(tileCollection)) { continue; } var pfNodes = c.GetTilesToCreature(tileCollection, this.Parent.PathFinder) .ToList <Objects.PathFinder.Node>(); if (setting.MustBeReachable && pfNodes.Count == 0) { continue; } if ((pfNodes.Count > 0 ? pfNodes.Count : playerTile.WorldLocation.DistanceTo(creatureTile.WorldLocation)) > setting.Range) { continue; } count++; if (setCreature) { int distance = pfNodes.Count > 0 ? pfNodes.Count : (int)playerTile.WorldLocation.DistanceTo(creatureTile.WorldLocation); if (distance < bestCreatureDistance) { bestCreatureDistance = distance; tempCreature = c; } } } if (count == 0 || count < setting.Count) { continue; } if (count > bestCount) { bestCount = count; bestSetting = setting; bestIndex = index; bestCreature = tempCreature; } index++; } this.CurrentSettingIndex = bestSetting != null ? bestIndex : -1; if (bestSetting != null && bestCreature != null) { this.Creature = bestCreature; } return(bestSetting); }
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); } } } }