-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathHealthBar.cs
More file actions
171 lines (149 loc) · 5.05 KB
/
HealthBar.cs
File metadata and controls
171 lines (149 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Threading;
using ExileCore2.PoEMemory.Components;
using ExileCore2.PoEMemory.MemoryObjects;
using ExileCore2.Shared.Cache;
using ExileCore2.Shared.Enums;
using RectangleF = ExileCore2.Shared.RectangleF;
using Vector2 = System.Numerics.Vector2;
namespace HealthBars;
public class HealthBar
{
private static long _idCounter;
private readonly Stopwatch _dpsStopwatch = Stopwatch.StartNew();
private bool _isHostile;
private readonly CachedValue<float> _distanceCache;
public HealthBar(Entity entity, HealthBarsSettings settings)
{
Entity = entity;
AllSettings = settings;
_distanceCache = new TimeCache<float>(() => entity.DistancePlayer, 200);
Update();
}
public void CheckUpdate()
{
var entityIsHostile = Entity.IsHostile;
if (_isHostile != entityIsHostile)
{
_isHostile = entityIsHostile;
Update();
}
if (Settings.ShowDps)
{
DpsRefresh();
}
}
public bool Skip { get; set; } = false;
public Vector2 LastPosition { get; set; }
private HealthBarsSettings AllSettings { get; }
public long StableId { get; } = Interlocked.Increment(ref _idCounter);
public UnitSettings Settings => Type switch
{
CreatureType.Player when Entity.Equals(Entity.Player) => AllSettings.Self,
CreatureType.Player => AllSettings.Players,
CreatureType.Minion => AllSettings.Minions,
CreatureType.Normal => AllSettings.NormalEnemy,
CreatureType.Magic => AllSettings.MagicEnemy,
CreatureType.Rare => AllSettings.RareEnemy,
CreatureType.Unique => AllSettings.UniqueEnemy,
};
public RectangleF DisplayArea { get; set; }
public float Distance => _distanceCache.Value;
public Entity Entity { get; }
public CreatureType Type { get; private set; }
public Life Life => Entity.GetComponent<Life>();
public float HpPercent => Life.HPPercentage;
public float EsPercent => Life.ESPercentage;
public float ManaPercent => Life.MPPercentage;
public float EhpPercent => CurrentEhp / (float)MaxEhp;
public int CurrentEhp => Life.CurHP + Life.CurES + (Settings.ManaProtectsLife ? Life.CurMana : 0);
public int MaxEhp => Life.MaxHP + Life.MaxES + (Settings.ManaProtectsLife ? Life.MaxMana : 0);
public readonly Queue<(DateTime Time, int Value)> EhpHistory = new Queue<(DateTime, int)>();
public Color Color
{
get
{
if (IsHidden(Entity))
return Color.LightGray;
if (ShouldDrawCullingStrikeIndicator())
return Settings.CullableColor;
return Settings.LifeColor;
}
}
private static bool IsHidden(Entity entity)
{
try
{
return entity.IsHidden;
}
catch
{
return false;
}
}
private void Update()
{
Type = GetEntityType();
}
private CreatureType GetEntityType()
{
if (Entity.HasComponent<Player>())
{
return CreatureType.Player;
}
if (Entity.HasComponent<Monster>())
{
var objectMagicProperties = Entity.GetComponent<ObjectMagicProperties>();
if (Entity.IsHostile)
{
return objectMagicProperties?.Rarity switch
{
MonsterRarity.White => CreatureType.Normal,
MonsterRarity.Magic => CreatureType.Magic,
MonsterRarity.Rare => CreatureType.Rare,
MonsterRarity.Unique => CreatureType.Unique,
_ => CreatureType.Minion
};
}
return CreatureType.Minion;
}
return CreatureType.Minion;
}
private void DpsRefresh()
{
if (_dpsStopwatch.ElapsedMilliseconds >= 200)
{
var hp = CurrentEhp;
if (hp == MaxEhp && EhpHistory.TryPeek(out var entry) && hp == entry.Value)
{
EhpHistory.Clear();
}
else
{
while (EhpHistory.TryPeek(out entry) &&
DateTime.UtcNow - entry.Time > TimeSpan.FromMilliseconds(AllSettings.DpsEstimateDuration))
{
EhpHistory.Dequeue();
}
}
EhpHistory.Enqueue((DateTime.UtcNow, hp));
}
}
internal bool ShouldDrawCullingStrikeIndicator()
{
if (!AllSettings.HasCullingStrike)
return false;
float cullingMultiplier = 1.0f + (AllSettings.CullingThreshold / 100f);
return Type switch
{
CreatureType.Normal => HpPercent <= 0.30f * cullingMultiplier,
CreatureType.Magic => HpPercent <= 0.20f * cullingMultiplier,
CreatureType.Rare => HpPercent <= 0.10f * cullingMultiplier,
CreatureType.Unique => HpPercent <= 0.05f * cullingMultiplier,
_ => false
};
}
}