/
hpnotify.csx
181 lines (144 loc) · 5.91 KB
/
hpnotify.csx
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
172
173
174
175
176
177
178
179
180
181
#load "colors.csx"
#load "targeting.csx"
#load "graphicseffects.csx"
using System;
using System.Collections.Generic;
using Infusion;
using Infusion.LegacyApi;
public static class HitPointNotifier
{
public static ScriptTrace Trace = UO.Trace.Create();
public static IPrintHitPointNotification Mode = HitPointNotificationModes.Standard;
public static void Run()
{
var journal = UO.CreateEventJournal();
journal.When<CurrentHealthUpdatedEvent>(OnHealthUpdated)
.Incomming();
}
public static void Enable()
{
UO.CommandHandler.Invoke("hpnotify");
}
public static void Disable()
{
UO.CommandHandler.Terminate("hpnotify");
}
public static void Toggle()
{
if (UO.CommandHandler.IsCommandRunning("hpnotify-toggle"))
Disable();
else
Enable();
}
private static void OnHealthUpdated(CurrentHealthUpdatedEvent args)
{
if (IsFirstStatusBarUpdate(args))
return;
// It seems like Sphere adds HP disregardig maximum HP. Then Sphere sends another
// update packet which adjust HP to maximum value.
if (IsHealingOverMaximumUpdate(args))
return;
var delta = args.UpdatedMobile.CurrentHealth - args.OldHealth;
Mode.Print(delta, args.UpdatedMobile);
}
private static bool IsHealingOverMaximumUpdate(CurrentHealthUpdatedEvent args) =>
args.UpdatedMobile.Id == UO.Me.PlayerId && args.UpdatedMobile.CurrentHealth > args.UpdatedMobile.MaxHealth;
private static bool IsFirstStatusBarUpdate(CurrentHealthUpdatedEvent args) =>
args.OldHealth == 0 && args.UpdatedMobile.CurrentHealth == args.UpdatedMobile.MaxHealth;
}
public interface IPrintHitPointNotification
{
void Print(int delta, Mobile updatedMobile);
}
public class AboveAllMobilesNotificationPrinter : IPrintHitPointNotification
{
public Color EnemyColor { get; set; } = Colors.Green;
public Color FriendColor { get; set; } = Colors.Blue;
public Color MyColor { get; set; } = Colors.LightBlue;
public Color PetsColor { get; set; } = Colors.Blue;
public void Print(int delta, Mobile mobile)
{
var deltaText = (delta > 0) ? "+" + delta.ToString() : delta.ToString();
Color textColor;
if (mobile.Id == UO.Me.PlayerId)
{
textColor = MyColor;
}
else if (mobile.CanRename && Pets.MyPets.Contains(mobile.Id))
{
textColor = PetsColor;
}
else if (mobile.Notoriety == Notoriety.Friend)
{
textColor = FriendColor;
}
else
{
textColor = EnemyColor;
}
var currentHealthText = mobile.Id == UO.Me.PlayerId ?
mobile.CurrentHealth.ToString() :
$"{mobile.CurrentHealth} %";
UO.ClientPrint($"{deltaText}/{currentHealthText}", "hpnotify",
mobile.Id, mobile.Type, SpeechType.Speech, textColor, log: false);
}
}
public class StandardPrinter : IPrintHitPointNotification
{
private Dictionary<ObjectId, string> prefixes = new Dictionary<ObjectId, string>();
public void AddPrefix(ObjectId id, string prefix)
{
prefixes[id] = prefix;
}
public bool PlayerHealEnabled { get; set; } = false;
public bool OthersHealEnabled { get; set; } = false;
public Color PlayerHealColor { get; set; } = Colors.None;
public Color PlayerHarmColor { get; set; } = Colors.Red;
public GraphicsEffect PlayerHarmEffect { get; set; } = GraphicsEffect.EnergyTwister;
public Color OthersHealColor { get; set; } = Colors.None;
public Color OthersHarmColor { get; set; } = Colors.None;
public bool MeAndTargetOnly { get; set; } = true;
public void Print(int delta, Mobile mobile)
{
if (UO.Me.PlayerId == mobile.Id && delta < 0 && mobile.CurrentHealth >= mobile.MaxHealth)
{
HitPointNotifier.Trace.Log($"Correction detected: delta {delta}; current {mobile.CurrentHealth}; max {mobile.MaxHealth}");
return;
}
if (delta > 0 && UO.Me.PlayerId == mobile.Id && !PlayerHealEnabled)
return;
if (delta > 0 && UO.Me.PlayerId != mobile.Id && !OthersHealEnabled)
return;
if (UO.Me.PlayerId == mobile.Id && PlayerHarmEffect != null)
PlayerHarmEffect.PlayOverPlayer();
var deltaText = (delta > 0) ? "+" + delta.ToString() : delta.ToString();
var textColor = mobile.Id == UO.Me.PlayerId ?
((delta > 0) ? PlayerHealColor : PlayerHarmColor) :
((delta > 0) ? OthersHealColor : OthersHarmColor);
if (MeAndTargetOnly && mobile.Id != UO.Me.PlayerId
&& Targeting.SelectedTarget.HasValue && mobile.Id != Targeting.SelectedTarget.Value)
{
return;
}
var currentHealthText = mobile.Id == UO.Me.PlayerId ?
mobile.CurrentHealth.ToString() :
$"{mobile.CurrentHealth} %";
string text;
if (prefixes.TryGetValue(mobile.Id, out string prefix))
text = $"{prefix}: {deltaText}/{currentHealthText}";
else
text = $"{deltaText}/{currentHealthText}";
UO.ClientPrint(text, mobile.Name ?? "hpnotify", mobile.Id, mobile.Type, SpeechType.Speech, textColor, log: true);
}
}
public static class HitPointNotificationModes
{
public static AboveAllMobilesNotificationPrinter AboveAllMobiles { get; } =
new AboveAllMobilesNotificationPrinter();
public static StandardPrinter Standard { get; } =
new StandardPrinter();
}
UO.RegisterBackgroundCommand("hpnotify", HitPointNotifier.Run);
UO.RegisterCommand("hpnotify-toggle", HitPointNotifier.Toggle);
UO.RegisterCommand("hpnotify-enable", HitPointNotifier.Enable);
UO.RegisterCommand("hpnotify-disable", HitPointNotifier.Disable)