Skip to content

Commit

Permalink
Allow ProtectEffect from NPC (#488)
Browse files Browse the repository at this point in the history
  • Loading branch information
NetDwarf authored Nov 8, 2024
2 parents beb1d26 + e5f19ef commit 1495c93
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 67 deletions.
2 changes: 1 addition & 1 deletion GameServer/ai/brain/ControlledNpcBrain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ public override void CheckAbilities()
case Abilities.Protect:
{
if (GetPlayerOwner() is GamePlayer player)
new ProtectEffect().Start(player);
new ProtectEffect().Start(Body, player);
break;
}
case Abilities.ChargeAbility:
Expand Down
6 changes: 3 additions & 3 deletions GameServer/ai/brain/StandardMobBrain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -523,9 +523,9 @@ public virtual void AddToAggroList(GameLiving living, int aggroamount, bool Chec
if (protectAmount > 0)
{
aggroamount -= protectAmount;
protect.ProtectSource.Out.SendMessage(LanguageMgr.GetTranslation(protect.ProtectSource.Client.Account.Language, "AI.Brain.StandardMobBrain.YouProtDist", player.GetName(0, false),
Body.GetName(0, false, protect.ProtectSource.Client.Account.Language, Body)), eChatType.CT_System, eChatLoc.CL_SystemWindow);
//player.Out.SendMessage("You are protected by " + protect.ProtectSource.GetName(0, false) + " from " + Body.GetName(0, false) + ".", eChatType.CT_System, eChatLoc.CL_SystemWindow);
if (protect.ProtectSource is GamePlayer playerSource)
playerSource.Out.SendMessage(LanguageMgr.GetTranslation(playerSource.Client.Account.Language, "AI.Brain.StandardMobBrain.YouProtDist", player.GetName(0, false),
Body.GetName(0, false, playerSource.Client.Account.Language, Body)), eChatType.CT_System, eChatLoc.CL_SystemWindow);

lock ((m_aggroTable as ICollection).SyncRoot)
{
Expand Down
121 changes: 58 additions & 63 deletions GameServer/effects/ProtectEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,34 +32,16 @@ namespace DOL.GS.Effects
public class ProtectEffect : StaticEffect, IGameEffect
{
/// <summary>
/// The player protecting the target
/// The individual protecting the target
/// </summary>
GamePlayer m_protectSource;
public GameLiving ProtectSource { get; private set; }

/// <summary>
/// Gets the player protecting the target
/// The individual being protected
/// </summary>
public GamePlayer ProtectSource
{
get { return m_protectSource; }
set { m_protectSource = value; }
}

/// <summary>
/// Reference to gameplayer that is protecting this player
/// </summary>
GamePlayer m_protectTarget = null;

/// <summary>
/// Gets the protected player
/// </summary>
public GamePlayer ProtectTarget
{
get { return m_protectTarget; }
set { m_protectTarget = value; }
}
public GameLiving ProtectTarget { get; private set; }

private Group m_playerGroup;
private Group m_playerGroup = null;

/// <summary>
/// Creates a new protect effect
Expand All @@ -71,34 +53,37 @@ public ProtectEffect()
/// <summary>
/// Start the guarding on player
/// </summary>
public void Start(GamePlayer protectSource, GamePlayer protectTarget)
public void Start(GameLiving source, GameLiving target)
{
if (protectSource == null || protectTarget == null)
if (source == null || target == null)
return;

m_owner = protectSource;
m_playerGroup = protectSource.Group;
m_owner = source;
ProtectSource = source;
ProtectTarget = target;

if (m_playerGroup != protectTarget.Group)
return;

m_protectSource = protectSource;
m_protectTarget = protectTarget;

GameEventMgr.AddHandler(m_playerGroup, GroupEvent.MemberDisbanded, new DOLEventHandler(GroupDisbandCallback));
if (target.Group != null && target.Group == source.Group)
{
m_playerGroup = source.Group;
GameEventMgr.AddHandler(m_playerGroup, GroupEvent.MemberDisbanded, new DOLEventHandler(GroupDisbandCallback));
}

m_protectSource.EffectList.Add(this);
m_protectTarget.EffectList.Add(this);
source.EffectList.Add(this);
target.EffectList.Add(this);

if (!protectSource.IsWithinRadius(protectTarget, ProtectAbilityHandler.PROTECT_DISTANCE))
if (!source.IsWithinRadius(target, ProtectAbilityHandler.PROTECT_DISTANCE))
{
protectSource.Out.SendMessage(LanguageMgr.GetTranslation(protectSource.Client, "Effects.ProtectEffect.YouProtectingYBut", protectTarget.GetName(0, false)), eChatType.CT_System, eChatLoc.CL_SystemWindow);
protectTarget.Out.SendMessage(LanguageMgr.GetTranslation(protectTarget.Client, "Effects.ProtectEffect.XProtectingYouBut", protectSource.GetName(0, true)), eChatType.CT_System, eChatLoc.CL_SystemWindow);
if (source is GamePlayer playerSource)
playerSource.Out.SendMessage(LanguageMgr.GetTranslation(playerSource.Client, "Effects.ProtectEffect.YouProtectingYBut", target.GetName(0, false)), eChatType.CT_System, eChatLoc.CL_SystemWindow);
if (target is GamePlayer playerTarget)
playerTarget.Out.SendMessage(LanguageMgr.GetTranslation(playerTarget.Client, "Effects.ProtectEffect.XProtectingYouBut", source.GetName(0, true)), eChatType.CT_System, eChatLoc.CL_SystemWindow);
}
else
{
protectSource.Out.SendMessage(LanguageMgr.GetTranslation(protectSource.Client, "Effects.ProtectEffect.YouProtectingY", protectTarget.GetName(0, false)), eChatType.CT_System, eChatLoc.CL_SystemWindow);
protectTarget.Out.SendMessage(LanguageMgr.GetTranslation(protectTarget.Client, "Effects.ProtectEffect.XProtectingYou", protectSource.GetName(0, true)), eChatType.CT_System, eChatLoc.CL_SystemWindow);
if (source is GamePlayer playerSource)
playerSource.Out.SendMessage(LanguageMgr.GetTranslation(playerSource.Client, "Effects.ProtectEffect.YouProtectingY", target.GetName(0, false)), eChatType.CT_System, eChatLoc.CL_SystemWindow);
if (target is GamePlayer playerTarget)
playerTarget.Out.SendMessage(LanguageMgr.GetTranslation(playerTarget.Client, "Effects.ProtectEffect.XProtectingYou", source.GetName(0, true)), eChatType.CT_System, eChatLoc.CL_SystemWindow);
}
}

Expand All @@ -110,41 +95,43 @@ public void Start(GamePlayer protectSource, GamePlayer protectTarget)
/// <param name="args"></param>
protected void GroupDisbandCallback(DOLEvent e, object sender, EventArgs args)
{
MemberDisbandedEventArgs eArgs = args as MemberDisbandedEventArgs;
if (eArgs == null) return;
if (eArgs.Member == ProtectTarget || eArgs.Member == ProtectSource)
{
if (args is MemberDisbandedEventArgs eArgs && (eArgs.Member == ProtectTarget || eArgs.Member == ProtectSource))
Cancel(false);
}
}

/// <summary>
/// Called when effect must be canceled
/// </summary>
public override void Cancel(bool playerCancel)
{
GameEventMgr.RemoveHandler(m_playerGroup, GroupEvent.MemberDisbanded, new DOLEventHandler(GroupDisbandCallback));
if (m_playerGroup != null)
GameEventMgr.RemoveHandler(m_playerGroup, GroupEvent.MemberDisbanded, new DOLEventHandler(GroupDisbandCallback));

// intercept handling is done by the active part
m_protectSource.EffectList.Remove(this);
m_protectTarget.EffectList.Remove(this);

m_protectSource.Out.SendMessage(LanguageMgr.GetTranslation(m_protectSource.Client, "Effects.ProtectEffect.YouNoProtectY", m_protectTarget.GetName(0, false)), eChatType.CT_System, eChatLoc.CL_SystemWindow);
m_protectTarget.Out.SendMessage(LanguageMgr.GetTranslation(m_protectTarget.Client, "Effects.ProtectEffect.XNoProtectYou", m_protectSource.GetName(0, true)), eChatType.CT_System, eChatLoc.CL_SystemWindow);
ProtectSource.EffectList.Remove(this);
ProtectTarget.EffectList.Remove(this);

m_playerGroup = null;
if (ProtectSource is GamePlayer playerSource)
playerSource.Out.SendMessage(LanguageMgr.GetTranslation(playerSource.Client, "Effects.ProtectEffect.YouNoProtectY", ProtectTarget.GetName(0, false)), eChatType.CT_System, eChatLoc.CL_SystemWindow);
if (ProtectTarget is GamePlayer playerTarget)
playerTarget.Out.SendMessage(LanguageMgr.GetTranslation(playerTarget.Client, "Effects.ProtectEffect.XNoProtectYou", ProtectSource.GetName(0, true)), eChatType.CT_System, eChatLoc.CL_SystemWindow);
}

/// <summary>
/// Name of the effect
/// </summary>
public override string Name
{

get
{
if (m_protectSource != null && m_protectTarget != null)
return LanguageMgr.GetTranslation(((GamePlayer)Owner).Client, "Effects.ProtectEffect.ProtectByName", m_protectTarget.GetName(0, false), m_protectSource.GetName(0, false));
return LanguageMgr.GetTranslation(((GamePlayer)Owner).Client, "Effects.ProtectEffect.Name");
string language;
if (ProtectTarget is GamePlayer playerTarget)
language = playerTarget.Client.Account.Language;
else if (ProtectSource is GamePlayer playerSource)
language = playerSource.Client.Account.Language;
else
language = LanguageMgr.DefaultLanguage;
return LanguageMgr.GetTranslation(language, "Effects.ProtectEffect.ProtectByName", ProtectTarget.GetName(0, false), ProtectSource.GetName(0, false));
}
}

Expand All @@ -171,12 +158,20 @@ public override IList<string> DelveInfo
{
get
{
var delveInfoList = new List<string>(4);
delveInfoList.Add(LanguageMgr.GetTranslation(((GamePlayer)Owner).Client, "Effects.ProtectEffect.InfoEffect"));
delveInfoList.Add(" ");
delveInfoList.Add(LanguageMgr.GetTranslation(((GamePlayer)Owner).Client, "Effects.ProtectEffect.XProtectingY", ProtectSource.GetName(0, true), ProtectTarget.GetName(0, false)));

return delveInfoList;
string language;
if (ProtectTarget is GamePlayer playerTarget)
language = playerTarget.Client.Account.Language;
else if (ProtectSource is GamePlayer playerSource)
language = playerSource.Client.Account.Language;
else
language = LanguageMgr.DefaultLanguage;

return new List<string>(4)
{
LanguageMgr.GetTranslation(language, "Effects.ProtectEffect.InfoEffect"),
" ",
LanguageMgr.GetTranslation(language, "Effects.ProtectEffect.XProtectingY", ProtectSource.GetName(0, true), ProtectTarget.GetName(0, false))
};
}
}
}
Expand Down

0 comments on commit 1495c93

Please sign in to comment.