Skip to content

Commit

Permalink
=Fixed picking dead monsters
Browse files Browse the repository at this point in the history
+ClosestToMouse option
  • Loading branch information
vadash committed Feb 17, 2021
1 parent 2c0efba commit 736e771
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 37 deletions.
51 changes: 37 additions & 14 deletions EssenceDrainContagion.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using ExileCore;
Expand All @@ -19,7 +20,8 @@ public class EssenceDrainContagion : BaseSettingsPlugin<EssenceDrainContagionSet
private Vector2 _oldMousePos;
private HashSet<string> _ignoredMonsters;
private Coroutine _mainCoroutine;
private Tuple<float, Entity> _bestTarget;
private Tuple<float, Entity> _currentTarget;
private Stopwatch _lastTargetSwap = new Stopwatch();

private readonly string[] _ignoredBuffs = {
"capture_monster_captured",
Expand Down Expand Up @@ -69,7 +71,18 @@ private IEnumerator MainCoroutine()
{
try
{
if (!ValidTarget(_bestTarget?.Item2)) _bestTarget = ScanValidMonsters()?.FirstOrDefault();
if (_currentTarget == null ||
!ValidTarget(_currentTarget?.Item2))
{
_currentTarget = ScanValidMonsters()?.FirstOrDefault();
_lastTargetSwap.Restart();
}
else if (_lastTargetSwap.ElapsedMilliseconds > 100)
{
var best = ScanValidMonsters()?.FirstOrDefault();
if (best?.Item1 > 1.2f * _currentTarget?.Item1) _currentTarget = best;
_lastTargetSwap.Restart();
}
}
catch
{
Expand All @@ -92,7 +105,7 @@ private IEnumerator MainCoroutine()
_aiming = false;
}

yield return new WaitTime(25);
yield return new WaitTime(10);
}
// ReSharper disable once IteratorNeverReturns
}
Expand All @@ -104,11 +117,12 @@ private bool ValidTarget(Entity entity)
return entity != null &&
entity.IsValid &&
entity.IsAlive &&
entity.IsHostile &&
entity.HasComponent<Monster>() &&
entity.IsHostile &&
entity.HasComponent<Targetable>() &&
entity.GetComponent<Targetable>().isTargetable &&
entity.HasComponent<Life>() &&
entity.GetComponent<Life>().CurHP / (float) entity.GetComponent<Life>().MaxHP > 0.25f &&
entity.GetComponent<Life>().CurHP > 0 &&
entity.DistancePlayer < Settings.AimRangeGrid &&
GameController.Window.GetWindowRectangleTimeCache.Contains(
GameController.Game.IngameState.Camera.WorldToScreen(entity.Pos));
Expand All @@ -121,9 +135,9 @@ private bool ValidTarget(Entity entity)

public override void Render()
{
if (_bestTarget != null)
if (_currentTarget != null)
{
var position = GameController.Game.IngameState.Camera.WorldToScreen(_bestTarget.Item2.Pos);
var position = GameController.Game.IngameState.Camera.WorldToScreen(_currentTarget.Item2.Pos);
Graphics.DrawFrame(position, position.Translate(20, 20), Color.Chocolate, 3);
}
base.Render();
Expand All @@ -145,18 +159,17 @@ private void LoadIgnoredMonsters(string fileName)

private IEnumerator Attack()
{
if (_bestTarget == null) yield break;
var position = GameController.Game.IngameState.Camera.WorldToScreen(_bestTarget.Item2.Pos);
if (_currentTarget == null) yield break;
var position = GameController.Game.IngameState.Camera.WorldToScreen(_currentTarget.Item2.Pos);
Input.SetCursorPos(position);
yield return Input.KeyPress(_bestTarget.Item2.HasBuff("contagion", true) ? Settings.EssenceDrainKey.Value : Settings.ContagionKey.Value);
yield return Input.KeyPress(_currentTarget.Item2.HasBuff("contagion", true) ? Settings.EssenceDrainKey.Value : Settings.ContagionKey.Value);
}

private IEnumerable<Tuple<float, Entity>> ScanValidMonsters()
{
var queue =
from entity in GameController.Entities
from entity in GameController?.EntityListWrapper?.ValidEntitiesByType?[EntityType.Monster]
where ValidTarget(entity)
let stats = entity.GetComponent<Stats>()
where !Extensions.HaveStat(entity, GameStat.CannotDie) &&
!Extensions.HaveStat(entity, GameStat.CannotBeDamaged) &&
!Extensions.HaveStat(entity, GameStat.IgnoredByEnemyTargetSelection) &&
Expand All @@ -165,14 +178,24 @@ where ValidTarget(entity)
let weight = ComputeWeight(entity)
orderby weight descending
select new Tuple<float, Entity>(weight, entity);

return queue;
}

private float ComputeWeight(Entity entity)
{
var weight = 0;
weight -= GameController.Player.DistanceFrom(entity) / 10;
if (Settings.ClosestToMouse)
{
var p1 = Input.MousePosition;
var p2 = GameController.Game.IngameState.Camera.WorldToScreen(entity.Pos);
weight -= (int) (p1.Distance(p2) / 10f);
}
else
{
var p1 = GameController.Game.IngameState.Camera.WorldToScreen(GameController.Player.Pos);
var p2 = GameController.Game.IngameState.Camera.WorldToScreen(entity.Pos);
weight -= (int) (p1.Distance(p2) / 10f);
}

if (entity.GetComponent<Life>().HasBuff("contagion")) weight += Settings.HasContagionWeight;
if (entity.GetComponent<Life>().HasBuff("capture_monster_trapped")) weight += Settings.capture_monster_trapped;
Expand Down
1 change: 1 addition & 0 deletions EssenceDrainContagionSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class EssenceDrainContagionSettings : ISettings
public HotkeyNode AimKey { get; set; } = Keys.RButton;
public HotkeyNode ContagionKey { get; set; } = Keys.Q;
public HotkeyNode EssenceDrainKey { get; set; } = Keys.W;
public ToggleNode ClosestToMouse { get; set; } = new ToggleNode(true);
public RangeNode<int> AimRangeGrid { get; set; } = new RangeNode<int>(100, 40, 200);
public RangeNode<int> AimLoopDelay { get; set; } = new RangeNode<int>(124, 1, 200);
public ToggleNode RMousePos { get; set; } = new ToggleNode(false);
Expand Down
23 changes: 0 additions & 23 deletions Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ namespace EssenceDrainContagion
{
public static class Extensions
{
private const int PixelBorder = 3;

public static bool HasBuff(this Entity entity, string buff, bool contains = false)
{
return entity.HasComponent<Life>() &&
Expand All @@ -29,26 +27,5 @@ public static bool HaveStat(Entity entity, GameStat stat)
return false;
}
}

public static bool IsInside(this Vector2 position, RectangleF container)
{
return position.Y + PixelBorder < container.Bottom
&& position.Y - PixelBorder > container.Top
&& position.X + PixelBorder < container.Right
&& position.X - PixelBorder > container.Left;
}

public static int DistanceFrom(this Entity fromEntity, Entity toEntity)
{
var Object = toEntity.GetComponent<Render>();
try
{
return Convert.ToInt32(Vector3.Distance(fromEntity.Pos, Object.Pos));
}
catch
{
return Int32.MaxValue;
}
}
}
}

0 comments on commit 736e771

Please sign in to comment.