forked from PerpetuumOnline/PerpetuumServer
-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathNpc.cs
More file actions
341 lines (277 loc) · 11 KB
/
Npc.cs
File metadata and controls
341 lines (277 loc) · 11 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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
using Perpetuum.Data;
using Perpetuum.EntityFramework;
using Perpetuum.Log;
using Perpetuum.Players;
using Perpetuum.Services.Looting;
using Perpetuum.Services.MissionEngine;
using Perpetuum.Services.MissionEngine.MissionTargets;
using Perpetuum.Units;
using Perpetuum.Zones.Eggs;
using Perpetuum.Zones.Intrusion;
using Perpetuum.Zones.LandMines;
using Perpetuum.Zones.RemoteControl;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Numerics;
using System.Threading.Tasks;
namespace Perpetuum.Zones.NpcSystem
{
public class Npc : SmartCreature, ITaggable
{
private readonly TagHelper tagHelper;
public Npc(TagHelper tagHelper)
{
this.tagHelper = tagHelper;
}
public NpcSpecialType SpecialType { get; set; }
public int EP { get; private set; }
public ILootGenerator LootGenerator { get; set; }
public void Tag(Player tagger, TimeSpan duration)
{
tagHelper.DoTagging(this, tagger, duration);
}
[CanBeNull]
public Player GetTagger()
{
return TagHelper.GetTagger(this);
}
public override void AcceptVisitor(IEntityVisitor visitor)
{
if (!TryAcceptVisitor(this, visitor))
{
base.AcceptVisitor(visitor);
}
}
public override IDictionary<string, object> GetDebugInfo()
{
IDictionary<string, object> info = base.GetDebugInfo();
double homeDistance = HomePosition.TotalDistance2D(CurrentPosition);
info.Add("homePositionX", HomePosition.intX);
info.Add("homePositionY", HomePosition.intY);
info.Add("homeRange", HomeRange);
info.Add("homeDistance", homeDistance);
info.Add("coreMax", CoreMax);
info.Add("coreCurrent", Core);
info.Add("bestCombatRange", BestActionRange);
StateMachines.IState currentAI = AI.Current;
if (currentAI != null)
{
info.Add("fsm", currentAI.GetType().Name);
}
info.Add("threat", ThreatManager.ToDebugString());
Group?.AddDebugInfoToDictionary(info);
info.Add("ismission", GetMissionGuid() != Guid.Empty);
return info;
}
protected override void OnDead(Unit killer)
{
IZone zone = Zone;
Player tagger = GetTagger();
Debug.Assert(zone != null, "zone != null");
BossInfo?.OnDeath(this, killer);
_ = HandleNpcDeadAsync(zone, killer, tagger)
.ContinueWith((t) => base.OnDead(killer))
.LogExceptions();
}
public override bool IsWalkable(Vector2 position)
{
return Zone.IsWalkableForNpc((int)position.X, (int)position.Y, Slope);
}
public override bool IsWalkable(Position position)
{
return Zone.IsWalkableForNpc((int)position.X, (int)position.Y, Slope);
}
public override bool IsWalkable(int x, int y)
{
return Zone.IsWalkableForNpc(x, y, Slope);
}
protected override void OnEnterZone(IZone zone, ZoneEnterType enterType)
{
SetEP(zone);
base.OnEnterZone(zone, enterType);
}
private void SetEP(IZone zone)
{
if (zone.Configuration.Type == ZoneType.Training)
{
EP = 0;
return;
}
int ep = NpcEp.GetEpForNpc(this);
if (zone.Configuration.IsBeta)
{
ep *= 2;
}
EP = ep;
Logger.DebugInfo($"Ep4Npc:{ep} def:{Definition} {ED.Name}");
}
public override string InfoString
{
get
{
string infoString = $"Npc:{ED.Name}:{Eid}";
IZone zone = Zone;
if (zone != null)
{
infoString += " z:" + zone.Id;
}
if (Group != null)
{
infoString += " g:" + Group.Name;
}
return infoString;
}
}
public override bool IsHostile(Player player)
{
return ED.Options.Faction != Faction.Syndicate;
}
internal override bool IsHostile(AreaBomb bomb)
{
return true;
}
internal override bool IsHostile(SentryTurret turret)
{
return true;
}
internal override bool IsHostile(IndustrialTurret turret)
{
return true;
}
internal override bool IsHostile(IndustrialDrone turret)
{
return true;
}
internal override bool IsHostile(CombatDrone drone)
{
return true;
}
protected override bool IsHostileFor(Unit unit)
{
return unit.IsHostile(this);
}
internal override bool IsHostile(Npc npc)
{
return (ED.Options.Faction == Faction.Syndicate && npc.ED.Options.Faction != Faction.Syndicate) ||
(ED.Options.Faction != Faction.Syndicate && npc.ED.Options.Faction == Faction.Syndicate);
}
internal override bool IsHostile(SAP sap)
{
return ED.Options.Faction == Faction.Cultist;
}
protected override void UpdateUnitVisibility(Unit target)
{
if (target is RemoteControlledCreature ||
target is LandMine ||
target is SAP ||
(target is Npc npc && npc.ED.Options.Faction != ED.Options.Faction))
{
UpdateVisibility(target);
}
}
private Task HandleNpcDeadAsync(IZone zone, Unit killer, Player tagger)
{
return Task.Run(() => HandleNpcDead(zone, killer, tagger));
}
private void HandleNpcDead([NotNull] IZone zone, Unit killer, Player tagger)
{
Logger.DebugInfo($" >>>> NPC died. Killer unitName:{killer.Name} o:{killer.Owner} Tagger botname:{tagger?.Name} o:{killer.Owner} characterId:{tagger?.Character.Id}");
using (System.Transactions.TransactionScope scope = Db.CreateTransaction())
{
if (BossInfo?.IsLootSplit ?? false)
{
List<Player> participants = new List<Player>();
participants = ThreatManager.Hostiles.Select(x => zone.ToPlayerOrGetOwnerPlayer(x.Unit)).ToList();
if (participants.Count > 0)
{
ISplittableLootGenerator splitLooter = new SplittableLootGenerator(LootGenerator);
List<ILootGenerator> lootGenerators = splitLooter.GetGenerators(participants.Count);
for (int i = 0; i < participants.Count; i++)
{
_ = LootContainer.Create()
.SetOwner(participants[i])
.AddLoot(lootGenerators[i])
.BuildAndAddToZone(zone, participants[i].CurrentPosition);
}
}
}
else
{
_ = LootContainer.Create().SetOwner(tagger).AddLoot(LootGenerator).BuildAndAddToZone(zone, CurrentPosition);
}
Player killerPlayer = zone.ToPlayerOrGetOwnerPlayer(killer);
if (GetMissionGuid() != Guid.Empty)
{
Logger.DebugInfo(" >>>> NPC is mission related.");
SearchForMissionOwnerAndSubmitKill(zone, killer);
}
else
{
Logger.DebugInfo(" >>>> independent NPC.");
if (killerPlayer != null)
{
EnqueueKill(killerPlayer, killer);
}
}
if (EP > 0)
{
List<Unit> awardedPlayers = new List<Unit>();
foreach (ThreatManaging.Hostile hostile in ThreatManager.Hostiles.Where(x => x.Unit is Player))
{
Unit playerUnit = hostile.Unit;
Player hostilePlayer = zone.ToPlayerOrGetOwnerPlayer(playerUnit);
_ = (hostilePlayer?.Character.AddExtensionPointsBoostAndLog(EpForActivityType.Npc, EP));
awardedPlayers.Add(playerUnit);
}
PseudoThreatManager.AwardPseudoThreats(awardedPlayers, zone, EP);
}
scope.Complete();
}
}
/// <summary>
/// This occurs when aoe kills the npc.
/// Background task that searches for the related missionguid and sumbits the kill for that specific player
/// </summary>
private void SearchForMissionOwnerAndSubmitKill(IZone zone, Unit killerUnit)
{
Guid missionGuid = GetMissionGuid();
Accounting.Characters.Character missionOwner = MissionHelper.FindMissionOwnerByGuid(missionGuid);
Player missionOwnerPlayer = zone.GetPlayer(missionOwner);
if (missionOwnerPlayer == null)
{
Dictionary<string, object> info = new Dictionary<string, object>
{
{k.characterID, missionOwner.Id},
{k.guid, missionGuid.ToString()},
{k.type, MissionTargetType.kill_definition},
{k.definition, ED.Definition},
{k.increase ,1},
{k.zoneID, zone.Id},
{k.position, killerUnit.CurrentPosition},
};
if (killerUnit is Player killerPlayer && killerPlayer.Character.Id != missionOwner.Id)
{
info[k.assistingCharacterID] = killerPlayer.Character.Id;
}
_ = Task.Run(() =>
{
MissionHelper.MissionProcessor.NpcGotKilledInAway(missionOwner, missionGuid, info);
});
return;
}
EnqueueKill(missionOwnerPlayer, killerUnit);
}
private void EnqueueKill(Player missionOwnerPlayer, Unit killerUnit)
{
Player eventSourcePlayer = missionOwnerPlayer;
if (killerUnit is Player killerPlayer && !killerPlayer.Equals(missionOwnerPlayer))
{
eventSourcePlayer = killerPlayer;
}
Logger.DebugInfo($" >>>> EventSource: botName:{eventSourcePlayer.Name} o:{eventSourcePlayer.Owner} characterId:{eventSourcePlayer.Character.Id} MissionOwner: botName:{missionOwnerPlayer.Name} o:{missionOwnerPlayer.Owner} characterId:{missionOwnerPlayer.Character.Id}");
missionOwnerPlayer.MissionHandler.EnqueueMissionEventInfoLocally(new KillEventInfo(eventSourcePlayer, this, CurrentPosition));
}
}
}