-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtargeting.csx
275 lines (232 loc) · 8.64 KB
/
targeting.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#load "colors.csx"
#load "Specs.csx"
#load "common.csx"
#load "pets.csx"
using System;
using System.Collections.Generic;
using System.Linq;
using Infusion.Packets;
public delegate IEnumerable<Mobile> TargetingMode();
public static class TargetingModes
{
public static TargetingMode Pvm = () =>
UO.Mobiles.MaxDistance(20)
// considering just murderers (red karma) and mobiles that are
// not player's pets/summons - player can change name of her/his
// pets/summons.
.Where(i => i.Id != UO.Me.PlayerId && i.Notoriety == Notoriety.Murderer
&& !i.CanRename && !Targeting.Ignored.Contains(i. Id) && (Targeting.IgnoredSpec == null || !Targeting.IgnoredSpec.Matches(i)))
.OrderByDistance();
public static TargetingMode Pvp = () =>
UO.Mobiles.MaxDistance(20)
.Matching(Specs.Player)
.Where(i => i.Id != UO.Me.PlayerId && (i.Notoriety == GetEnemyNotoriety()
|| ((i.Notoriety == Notoriety.Grey || i.Notoriety == Notoriety.Criminal) && Specs.Player.Matches(i)))
&& !Targeting.Ignored.Contains(i. Id) && (Targeting.IgnoredSpec == null || !Targeting.IgnoredSpec.Matches(i)))
.OrderByDistance();
private static Notoriety GetEnemyNotoriety()
{
if (UO.Me.Notoriety == Notoriety.Innocent)
return Notoriety.Murderer;
return Notoriety.Innocent;
}
public static TargetingMode PvpFriend = () =>
UO.Mobiles.MaxDistance(20)
.Matching(Specs.Player)
.Where(i => i.Id != UO.Me.PlayerId && !Targeting.Ignored.Any(x => x.Id == i.Id && !Specs.Player.Matches(x))
&& (Targeting.IgnoredSpec == null || !Targeting.IgnoredSpec.Matches(i)))
.OrderByDistance();
}
public static class Targeting
{
private static Stack<ObjectId> alreadyTargeted = new Stack<ObjectId>();
public static ObjectId? SelectedTarget { get; private set; }
public static ScriptTrace Trace { get; } = UO.Trace.Create();
public static ObjectId? CurrentTarget { get; private set; }
public static TargetingMode Mode { get; set; } = TargetingModes.Pvm;
public static IMobileLookup Ignored { get; set; } = new CompositeMobileLookup(Pets.MyPets, Party.Members);
public static MobileSpec IgnoredSpec { get; set; } = Specs.MistrovskySatan;
public static event Action<ObjectId> TargetingLast;
public static event Action<ObjectId> AttackingLast;
private static IEnumerable<Mobile> GetTargets()
{
if (Mode != null)
return Mode();
else
{
return UO.Mobiles.MaxDistance(20)
// considering just murderers (red karma) and mobiles that are
// not player's pets/summons - player can change name of her/his
// pets/summons.
.Where(i => i.Notoriety == Notoriety.Murderer && !i.CanRename && !Ignored.Contains(i. Id)
&& (IgnoredSpec == null || !IgnoredSpec.Matches(i)))
.OrderByDistance();
}
}
public static void TargetNext()
{
PrintAlreadyTargeted();
var potentialTargets = GetTargets();
if (!potentialTargets.Any())
{
UO.ClientPrint("No potential target", "targeting", UO.Me);
return;
}
var target = potentialTargets.FirstOrDefault(i =>
// excluding mobiles already target
!alreadyTargeted.Contains(i.Id) &&
(!SelectedTarget.HasValue || SelectedTarget.Value != i.Id));
if (target != null)
{
SelectTarget(target);
alreadyTargeted.Push(target.Id);
}
else
{
if (potentialTargets.Count() == 1 && SelectedTarget == potentialTargets.First().Id)
SelectTarget(potentialTargets.First());
else
{
// start the targeting from begining
// by clearing already targeted mobiles
alreadyTargeted.Clear();
// and trying to target next again
TargetNext();
}
}
}
public static void PrintAlreadyTargeted()
{
if (Trace.Enabled)
{
Trace.Log(alreadyTargeted
.Select(x => x.ToString())
.Aggregate("Targeted: ", (l, r) => l + ", " + r));
}
}
public static void TargetPrev()
{
Mobile target = null;
PrintAlreadyTargeted();
if (alreadyTargeted.Any())
{
if (SelectedTarget.HasValue && SelectedTarget.Value == alreadyTargeted.Peek())
alreadyTargeted.Pop();
while (alreadyTargeted.Any() && (target = UO.Mobiles[alreadyTargeted.Pop()]) == null)
;
}
if (target == null)
{
Mobile lastTarget = null;
var potentialTargets = GetTargets().ToArray();
if (!potentialTargets.Any())
{
UO.ClientPrint("No potential previous target");
return;
}
foreach (var t in potentialTargets)
{
alreadyTargeted.Push(t.Id);
lastTarget = t;
}
if (alreadyTargeted.Count() == 1)
SelectTarget(potentialTargets.First());
else
TargetPrev();
}
else
{
SelectTarget(target);
}
}
private static void SelectTarget(Mobile target)
{
if (string.IsNullOrEmpty(target.Name))
{
UO.ClientPrint("******", "targeting", target.Id, target.Type, SpeechType.Speech, Colors.Green, log: false);
UO.Click(target);
}
else
UO.ClientPrint($"*** {target.Name} ***", "targeting", target.Id, target.Type, SpeechType.Speech, Colors.Green, log: false);
UO.Client.AllowAttack(target.Id);
UO.ClientPrint($"Target distance: {target.GetDistance(UO.Me.Location)}", log: false);
SelectedTarget = target.Id;
}
public static void AttackLast()
{
if (!SelectedTarget.HasValue)
{
UO.ClientPrint("No selected target", "targeting", UO.Me);
return;
}
var target = UO.Mobiles[SelectedTarget.Value];
if (target == null)
{
UO.ClientPrint("Cannot see target", "targeting", UO.Me);
return;
}
UO.Attack(target);
AttackingLast?.Invoke(target.Id);
alreadyTargeted.Clear();
alreadyTargeted.Push(target.Id);
}
public static void TargetLast()
{
if (!SelectedTarget.HasValue)
{
UO.ClientPrint("No selected target", "targeting", UO.Me);
return;
}
var target = UO.Mobiles[SelectedTarget.Value];
if (target == null)
{
UO.ClientPrint("Cannot see target", "targeting", UO.Me);
return;
}
if (CurrentTarget != SelectedTarget)
{
// When we are targeting a target for the first time:
CurrentTarget = SelectedTarget;
// Restart targeting by clearing list of already targeted
// items. We have to add current target, so ',targetnext'
// skips current target and really moves to the next one.
alreadyTargeted.Clear();
alreadyTargeted.Push(target.Id);
}
TargetingLast?.Invoke(target.Id);
UO.Target(target);
}
public static void SelectTarget()
{
var mobile = UO.AskForMobile();
if (mobile == null)
{
UO.Log("Targeting canceled");
return;
}
SelectedTarget = mobile.Id;
CurrentTarget = mobile.Id;
alreadyTargeted.Clear();
alreadyTargeted.Push(mobile.Id);
}
}
UO.RegisterCommand("target-select", Targeting.SelectTarget);
UO.RegisterCommand("target-next", Targeting.TargetNext);
UO.RegisterCommand("target-prev", Targeting.TargetPrev);
UO.RegisterCommand("target-last", Targeting.TargetLast);
UO.RegisterCommand("target-attack", Targeting.AttackLast);
UO.RegisterCommand("target-pvm", () =>
{
Targeting.Mode = TargetingModes.Pvm;
UO.ClientPrint("Pvm targeting");
});
UO.RegisterCommand("target-pvp", () =>
{
Targeting.Mode = TargetingModes.Pvp;
UO.ClientPrint("Pvp targeting");
});
UO.RegisterCommand("target-pvpfriend", () =>
{
Targeting.Mode = TargetingModes.PvpFriend;
UO.ClientPrint("Pvp with friends targeting");
});