Skip to content

Commit 922590d

Browse files
committed
Update condition applying events to use ConditionCollection
1 parent 599149d commit 922590d

File tree

1 file changed

+143
-46
lines changed

1 file changed

+143
-46
lines changed

src/MHServerEmu.Games/Events/EventManager.cs

+143-46
Original file line numberDiff line numberDiff line change
@@ -262,27 +262,38 @@ private void OnToTeleport(PlayerConnection playerConnection, Vector3 targetPos)
262262
Logger.Trace($"Teleporting to {targetPos}");
263263
}
264264

265-
private void OnStartTravel(PlayerConnection playerConnection, PrototypeId powerId)
265+
private bool OnStartTravel(PlayerConnection playerConnection, PrototypeId powerId)
266266
{
267-
var conditionSerializationFlags = ConditionSerializationFlags.NoCreatorId | ConditionSerializationFlags.NoUltimateCreatorId
268-
| ConditionSerializationFlags.NoConditionPrototypeRef | ConditionSerializationFlags.HasCreatorPowerIndex | ConditionSerializationFlags.HasOwnerAssetRef;
269-
270-
ulong avatarEntityId = playerConnection.Player.CurrentAvatar.Id;
267+
Avatar avatar = playerConnection.Player.CurrentAvatar;
271268

272269
switch (powerId)
273270
{
274271
case (PrototypeId)PowerPrototypes.Travel.GhostRiderRide:
272+
Condition ghostRiderRideCondition = avatar.ConditionCollection.GetCondition(666);
273+
if (ghostRiderRideCondition != null) return Logger.WarnReturn(false, "OnStartTravel(): ghostRiderRideCondition != null");
274+
275275
Logger.Trace($"EventStart GhostRiderRide");
276+
276277
// Player.Avatar.EvalOnCreate.AssignProp.ProcProp.Param1
277-
AddConditionArchive conditionArchive = new(avatarEntityId, 666, conditionSerializationFlags, powerId, TimeSpan.Zero); // TODO: generate and save Condition.Id
278-
conditionArchive.Condition.CreatorPowerIndex = 0;
278+
// Create and add a ride condition
279+
ghostRiderRideCondition = avatar.ConditionCollection.AllocateCondition();
280+
ghostRiderRideCondition.InitializeFromPowerMixinPrototype(666, powerId, 0, TimeSpan.Zero);
281+
avatar.ConditionCollection.AddCondition(ghostRiderRideCondition);
282+
283+
// Notify the client
284+
AddConditionArchive conditionArchive = new()
285+
{
286+
ReplicationPolicy = AOINetworkPolicyValues.DefaultPolicy,
287+
EntityId = avatar.Id,
288+
Condition = ghostRiderRideCondition
289+
};
279290

280291
playerConnection.SendMessage(NetMessageAddCondition.CreateBuilder()
281292
.SetArchiveData(conditionArchive.SerializeToByteString())
282293
.Build());
283294

284295
playerConnection.SendMessage(NetMessagePowerCollectionAssignPower.CreateBuilder()
285-
.SetEntityId(avatarEntityId)
296+
.SetEntityId(avatar.Id)
286297
.SetPowerProtoId((ulong)PowerPrototypes.GhostRider.RideBikeHotspotsEnd)
287298
.SetPowerRank(0)
288299
.SetCharacterLevel(60)
@@ -301,32 +312,57 @@ private void OnStartTravel(PlayerConnection playerConnection, PrototypeId powerI
301312
case (PrototypeId)PowerPrototypes.Travel.BladeRide:
302313
case (PrototypeId)PowerPrototypes.Travel.AntmanFlight:
303314
case (PrototypeId)PowerPrototypes.Travel.ThingFlight:
315+
Condition rideCondition = avatar.ConditionCollection.GetCondition(667);
316+
if (rideCondition != null) return Logger.WarnReturn(false, "OnStartTravel(): rideCondition != null");
317+
304318
Logger.Trace($"EventStart Ride");
305-
conditionArchive = new(avatarEntityId, 667, conditionSerializationFlags, powerId, TimeSpan.Zero);
306-
conditionArchive.Condition.CreatorPowerIndex = 0;
319+
320+
// Create and add a ride condition
321+
rideCondition = avatar.ConditionCollection.AllocateCondition();
322+
rideCondition.InitializeFromPowerMixinPrototype(667, powerId, 0, TimeSpan.Zero);
323+
avatar.ConditionCollection.AddCondition(rideCondition);
324+
325+
// Notify the client
326+
conditionArchive = new()
327+
{
328+
ReplicationPolicy = AOINetworkPolicyValues.DefaultPolicy,
329+
EntityId = avatar.Id,
330+
Condition = rideCondition
331+
};
332+
307333
playerConnection.SendMessage(NetMessageAddCondition.CreateBuilder()
308334
.SetArchiveData(conditionArchive.SerializeToByteString())
309335
.Build());
336+
310337
break;
311338
}
339+
340+
return true;
312341
}
313342

314343
private void OnEndTravel(PlayerConnection playerConnection, PrototypeId powerId)
315344
{
316-
ulong avatarEntityId = playerConnection.Player.CurrentAvatar.Id;
345+
Avatar avatar = playerConnection.Player.CurrentAvatar;
317346

318347
switch (powerId)
319348
{
320349
case (PrototypeId)PowerPrototypes.Travel.GhostRiderRide:
350+
if (avatar.ConditionCollection.GetCondition(666) == null) return;
351+
321352
Logger.Trace($"EventEnd GhostRiderRide");
322353

354+
// Remove the ride condition
355+
avatar.ConditionCollection.RemoveCondition(666);
356+
// TODO: Remove the power from the collection
357+
358+
// Notify the client
323359
playerConnection.SendMessage(NetMessageDeleteCondition.CreateBuilder()
324-
.SetIdEntity(avatarEntityId)
360+
.SetIdEntity(avatar.Id)
325361
.SetKey(666)
326362
.Build());
327363

328364
playerConnection.SendMessage(NetMessagePowerCollectionUnassignPower.CreateBuilder()
329-
.SetEntityId(avatarEntityId)
365+
.SetEntityId(avatar.Id)
330366
.SetPowerProtoId((ulong)PowerPrototypes.GhostRider.RideBikeHotspotsEnd)
331367
.Build());
332368

@@ -340,9 +376,16 @@ private void OnEndTravel(PlayerConnection playerConnection, PrototypeId powerId)
340376
case (PrototypeId)PowerPrototypes.Travel.BladeRide:
341377
case (PrototypeId)PowerPrototypes.Travel.AntmanFlight:
342378
case (PrototypeId)PowerPrototypes.Travel.ThingFlight:
379+
if (avatar.ConditionCollection.GetCondition(667) == null) return;
380+
343381
Logger.Trace($"EventEnd Ride");
382+
383+
// Remove the ride condition
384+
avatar.ConditionCollection.RemoveCondition(667);
385+
386+
// Notify the client
344387
playerConnection.SendMessage(NetMessageDeleteCondition.CreateBuilder()
345-
.SetIdEntity(avatarEntityId)
388+
.SetIdEntity(avatar.Id)
346389
.SetKey(667)
347390
.Build());
348391

@@ -443,23 +486,33 @@ private void OnEndThrowing(PlayerConnection playerConnection, PrototypeId powerI
443486

444487
private void OnDiamondFormActivate(PlayerConnection playerConnection)
445488
{
446-
var conditionSerializationFlags = ConditionSerializationFlags.NoCreatorId | ConditionSerializationFlags.NoUltimateCreatorId | ConditionSerializationFlags.NoConditionPrototypeRef
447-
| ConditionSerializationFlags.HasCreatorPowerIndex | ConditionSerializationFlags.HasOwnerAssetRef | ConditionSerializationFlags.OwnerAssetRefOverride;
489+
Avatar avatar = playerConnection.Player.CurrentAvatar;
448490

449-
var diamondFormCondition = (PrototypeId)PowerPrototypes.EmmaFrost.DiamondFormCondition;
450-
AddConditionArchive conditionArchive = new(playerConnection.Player.CurrentAvatar.Id, 111, conditionSerializationFlags, diamondFormCondition, TimeSpan.Zero);
491+
Condition diamondFormCondition = avatar.ConditionCollection.GetCondition(111);
492+
if (diamondFormCondition != null) return;
451493

452494
Logger.Trace($"Event Start EmmaDiamondForm");
453495

454-
PrototypeId emmaCostume = playerConnection.Player.CurrentAvatar.Properties[PropertyEnum.CostumeCurrent];
455-
496+
// Get the asset id for the current costume to set the correct owner asset id override
497+
PrototypeId emmaCostume = avatar.Properties[PropertyEnum.CostumeCurrent];
456498
// Invalid prototype id is the same as the default costume
457499
if (emmaCostume == PrototypeId.Invalid)
458-
emmaCostume = GameDatabase.GetPrototypeRefByName("Entity/Items/Costumes/Prototypes/EmmaFrost/Modern.prototype");
500+
emmaCostume = GameDatabase.GetPrototypeRefByName("Entity/Items/Costumes/Prototypes/EmmaFrost/Modern.prototype"); // MarvelPlayer_EmmaFrost_Modern
501+
502+
AssetId costumeAsset = emmaCostume.As<CostumePrototype>().CostumeUnrealClass;
503+
504+
// Create and add a condition for the diamond form
505+
diamondFormCondition = avatar.ConditionCollection.AllocateCondition();
506+
diamondFormCondition.InitializeFromPowerMixinPrototype(111, (PrototypeId)PowerPrototypes.EmmaFrost.DiamondFormCondition, 0, TimeSpan.Zero, true, costumeAsset);
507+
avatar.ConditionCollection.AddCondition(diamondFormCondition);
459508

460-
var asset = GameDatabase.GetPrototype<CostumePrototype>(emmaCostume).CostumeUnrealClass;
461-
conditionArchive.Condition.OwnerAssetRef = asset; // MarvelPlayer_EmmaFrost_Modern
462-
conditionArchive.Condition.CreatorPowerIndex = 0;
509+
// Notify the client
510+
AddConditionArchive conditionArchive = new()
511+
{
512+
ReplicationPolicy = AOINetworkPolicyValues.DefaultPolicy,
513+
EntityId = avatar.Id,
514+
Condition = diamondFormCondition
515+
};
463516

464517
playerConnection.SendMessage(NetMessageAddCondition.CreateBuilder()
465518
.SetArchiveData(conditionArchive.SerializeToByteString())
@@ -468,40 +521,62 @@ private void OnDiamondFormActivate(PlayerConnection playerConnection)
468521

469522
private void OnDiamondFormDeactivate(PlayerConnection playerConnection)
470523
{
524+
Avatar avatar = playerConnection.Player.CurrentAvatar;
525+
471526
// TODO: get DiamondFormCondition Condition Key
527+
if (avatar.ConditionCollection.GetCondition(111) == null) return;
528+
529+
Logger.Trace($"EventEnd EmmaDiamondForm");
530+
531+
// Remove the condition server-side
532+
avatar.ConditionCollection.RemoveCondition(111);
533+
534+
// Notify the client
472535
playerConnection.SendMessage(NetMessageDeleteCondition.CreateBuilder()
473536
.SetKey(111)
474-
.SetIdEntity(playerConnection.Player.CurrentAvatar.Id)
537+
.SetIdEntity(avatar.Id)
475538
.Build());
476-
477-
Logger.Trace($"EventEnd EmmaDiamondForm");
478539
}
479540

480-
private void OnStartMagikUltimate(PlayerConnection playerConnection, NetStructPoint3 position)
541+
private bool OnStartMagikUltimate(PlayerConnection playerConnection, NetStructPoint3 position)
481542
{
482-
ulong avatarEntityId = playerConnection.Player.CurrentAvatar.Id;
543+
Avatar avatar = playerConnection.Player.CurrentAvatar;
483544

484-
var conditionSerializationFlags = ConditionSerializationFlags.NoCreatorId | ConditionSerializationFlags.NoUltimateCreatorId | ConditionSerializationFlags.NoConditionPrototypeRef
485-
| ConditionSerializationFlags.HasCreatorPowerIndex | ConditionSerializationFlags.HasOwnerAssetRef | ConditionSerializationFlags.HasDuration;
545+
Condition magikUltimateCondition = avatar.ConditionCollection.GetCondition(777);
546+
if (magikUltimateCondition != null) return Logger.WarnReturn(false, "OnStartMagikUltimate(): magikUltimateCondition != null");
486547

487548
Logger.Trace($"EventStart Magik Ultimate");
488549

489-
AddConditionArchive conditionArchive = new(avatarEntityId, 777, conditionSerializationFlags, (PrototypeId)PowerPrototypes.Magik.Ultimate, TimeSpan.Zero);
490-
conditionArchive.Condition.Duration = TimeSpan.FromMilliseconds(20000);
491-
conditionArchive.Condition.CreatorPowerIndex = 0;
492-
493-
playerConnection.SendMessage(NetMessageAddCondition.CreateBuilder()
494-
.SetArchiveData(conditionArchive.SerializeToByteString())
495-
.Build());
550+
// Create and add a condition for the ultimate
551+
magikUltimateCondition = avatar.ConditionCollection.AllocateCondition();
552+
magikUltimateCondition.InitializeFromPowerMixinPrototype(777, (PrototypeId)PowerPrototypes.Magik.Ultimate, 0, TimeSpan.FromMilliseconds(20000));
553+
avatar.ConditionCollection.AddCondition(magikUltimateCondition);
496554

555+
/*
556+
// Create the arena entity
497557
WorldEntity arenaEntity = _game.EntityManager.CreateWorldEntityEmpty(
498558
playerConnection.AOI.Region.Id,
499559
(PrototypeId)PowerPrototypes.Magik.UltimateArea,
500560
new(position.X, position.Y, position.Z), new());
501561
502-
// we need to store this state in the avatar entity instead
562+
563+
// Save the entity id for the arena entity (we need to store this state in the avatar entity instead)
503564
playerConnection.MagikUltimateEntityId = arenaEntity.Id;
565+
*/
566+
567+
// Notify the client
568+
AddConditionArchive conditionArchive = new()
569+
{
570+
ReplicationPolicy = AOINetworkPolicyValues.DefaultPolicy,
571+
EntityId = avatar.Id,
572+
Condition = magikUltimateCondition
573+
};
504574

575+
playerConnection.SendMessage(NetMessageAddCondition.CreateBuilder()
576+
.SetArchiveData(conditionArchive.SerializeToByteString())
577+
.Build());
578+
579+
/*
505580
playerConnection.SendMessage(arenaEntity.ToNetMessageEntityCreate());
506581
507582
playerConnection.SendMessage(NetMessagePowerCollectionAssignPower.CreateBuilder()
@@ -514,31 +589,53 @@ private void OnStartMagikUltimate(PlayerConnection playerConnection, NetStructPo
514589
.SetItemVariation(1)
515590
.Build());
516591
517-
playerConnection.SendMessage(Property.ToNetMessageSetProperty(arenaEntity.Properties.ReplicationId, new(PropertyEnum.AttachedToEntityId), avatarEntityId));
592+
playerConnection.SendMessage(Property.ToNetMessageSetProperty(arenaEntity.Properties.ReplicationId, new(PropertyEnum.AttachedToEntityId), avatar.Id));
593+
*/
594+
595+
return true;
518596
}
519597

520-
private void OnEndMagikUltimate(PlayerConnection playerConnection)
598+
private bool OnEndMagikUltimate(PlayerConnection playerConnection)
521599
{
600+
// Make sure we still get Magik in case the player switched to another avatar
601+
Avatar avatar = playerConnection.Player.AvatarList.FirstOrDefault(avatar => avatar.PrototypeDataRef == (PrototypeId)AvatarPrototypeId.Magik);
602+
if (avatar == null) return Logger.WarnReturn(false, "OnEndMagikUltimate(): avatar == null");
603+
604+
Condition magikUltimateCondition = avatar.ConditionCollection.GetCondition(777);
605+
if (magikUltimateCondition == null) return Logger.WarnReturn(false, "OnEndMagikUltimate(): magikUltimateCondition == null");
606+
522607
Logger.Trace($"EventEnd Magik Ultimate");
523-
ulong avatarEntityId = playerConnection.Player.CurrentAvatar.Id;
608+
609+
// Remove the ultimate condition
610+
avatar.ConditionCollection.RemoveCondition(777);
611+
612+
/*
613+
// TODO: Removed the hotspot effect power from the arena's power collection
614+
615+
// Destroy the arena entity
524616
ulong arenaEntityId = playerConnection.MagikUltimateEntityId;
617+
var entity = _game.EntityManager.GetEntityById(arenaEntityId);
618+
entity?.Destroy();
619+
*/
525620

621+
// Notify the client
526622
playerConnection.SendMessage(NetMessageDeleteCondition.CreateBuilder()
527-
.SetIdEntity(avatarEntityId)
623+
.SetIdEntity(avatar.Id)
528624
.SetKey(777)
529625
.Build());
530626

627+
/*
531628
playerConnection.SendMessage(NetMessagePowerCollectionUnassignPower.CreateBuilder()
532629
.SetEntityId(arenaEntityId)
533630
.SetPowerProtoId((ulong)PowerPrototypes.Magik.UltimateHotspotEffect)
534631
.Build());
535632
536-
var entity = _game.EntityManager.GetEntityById(arenaEntityId);
537-
entity?.Destroy();
538-
539633
playerConnection.SendMessage(NetMessageEntityDestroy.CreateBuilder()
540634
.SetIdEntity(arenaEntityId)
541635
.Build());
636+
*/
637+
638+
return true;
542639
}
543640

544641
private void OnGetRegion(PlayerConnection playerConnection, Region region)

0 commit comments

Comments
 (0)