context: using System;
  using System.Collections.Generic;
  using Server.Engines.PartySystem;
  using Server.Engines.XmlSpawner2;
  using Server.Factions;
  using Server.Guilds;
  using Server.Items;
  using Server.Mobiles;
  using Server.Multis;
  
  namespace Server.Misc
  {
      public class NotorietyHandlers
      {
          public static void Initialize()
          {
              Notoriety.Hues[Notoriety.Innocent] = 0x59;
              Notoriety.Hues[Notoriety.Ally] = 0x3F;
              Notoriety.Hues[Notoriety.CanBeAttacked] = 0x3B2;
              Notoriety.Hues[Notoriety.Criminal] = 0x3B2;
              Notoriety.Hues[Notoriety.Enemy] = 0x90;
              Notoriety.Hues[Notoriety.Murderer] = 0x22;
              Notoriety.Hues[Notoriety.Invulnerable] = 0x35;
  
              Notoriety.Handler = new NotorietyHandler(MobileNotoriety);
  
              Mobile.AllowBeneficialHandler = new AllowBeneficialHandler(Mobile_AllowBeneficial);
              Mobile.AllowHarmfulHandler = new AllowHarmfulHandler(Mobile_AllowHarmful);
          }
  
          private enum GuildStatus
          {
              None,
              Peaceful,
              Waring
          }
  
          private static GuildStatus GetGuildStatus(Mobile m)
          {
              if (m.Guild == null)
                  return GuildStatus.None;
              else if (((Guild)m.Guild).Enemies.Count == 0 && m.Guild.Type == GuildType.Regular)
                  return GuildStatus.Peaceful;
  
              return GuildStatus.Waring;
          }
  
          private static bool CheckBeneficialStatus(GuildStatus from, GuildStatus target)
          {
              if (from == GuildStatus.Waring || target == GuildStatus.Waring)
                  return false;
  
              return true;
          }
  
          /*private static bool CheckHarmfulStatus( GuildStatus from, GuildStatus target )
          {
          if ( from == GuildStatus.Waring && target == GuildStatus.Waring )
          return true;
  
          return false;
          }*/
  
          public static bool Mobile_AllowBeneficial(Mobile from, Mobile target)
          {
              if (from == null || target == null || from.IsStaff() || target.IsStaff())
                  return true;
  
              #region Dueling
              PlayerMobile pmFrom = from as PlayerMobile;
              PlayerMobile pmTarg = target as PlayerMobile;
  
              if (pmFrom == null && from is BaseCreature)
              {
                  BaseCreature bcFrom = (BaseCreature)from;
  
                  if (bcFrom.Summoned)
                      pmFrom = bcFrom.SummonMaster as PlayerMobile;
              }
  
              if (pmTarg == null && target is BaseCreature)
              {
                  BaseCreature bcTarg = (BaseCreature)target;
  
                  if (bcTarg.Summoned)
                      pmTarg = bcTarg.SummonMaster as PlayerMobile;
              }
  
              if (pmFrom != null && pmTarg != null)
              {
                  if (pmFrom.DuelContext != pmTarg.DuelContext && ((pmFrom.DuelContext != null && pmFrom.DuelContext.Started) || (pmTarg.DuelContext != null && pmTarg.DuelContext.Started)))
                      return false;
  
                  if (pmFrom.DuelContext != null && pmFrom.DuelContext == pmTarg.DuelContext && ((pmFrom.DuelContext.StartedReadyCountdown && !pmFrom.DuelContext.Started) || pmFrom.DuelContext.Tied || pmFrom.DuelPlayer.Eliminated || pmTarg.DuelPlayer.Eliminated))
                      return false;
  
                  if (pmFrom.DuelPlayer != null && !pmFrom.DuelPlayer.Eliminated && pmFrom.DuelContext != null && pmFrom.DuelContext.IsSuddenDeath)
                      return false;
  
                  if (pmFrom.DuelContext != null && pmFrom.DuelContext == pmTarg.DuelContext && pmFrom.DuelContext.m_Tournament != null && pmFrom.DuelContext.m_Tournament.IsNotoRestricted && pmFrom.DuelPlayer != null && pmTarg.DuelPlayer != null && pmFrom.DuelPlayer.Participant != pmTarg.DuelPlayer.Participant)
                      return false;
  
                  if (pmFrom.DuelContext != null && pmFrom.DuelContext == pmTarg.DuelContext && pmFrom.DuelContext.Started)
                      return true;
              }
  
              if ((pmFrom != null && pmFrom.DuelContext != null && pmFrom.DuelContext.Started) || (pmTarg != null && pmTarg.DuelContext != null && pmTarg.DuelContext.Started))
                  return false;
  
              Engines.ConPVP.SafeZone sz = from.Region.GetRegion(typeof(Engines.ConPVP.SafeZone)) as Engines.ConPVP.SafeZone;
  
              if (sz != null /*&& sz.IsDisabled()*/)
                  return false;
  
              sz = target.Region.GetRegion(typeof(Engines.ConPVP.SafeZone)) as Engines.ConPVP.SafeZone;
  
              if (sz != null /*&& sz.IsDisabled()*/)
                  return false;
              #endregion
  
              Map map = from.Map;
  
              #region Factions
              Faction targetFaction = Faction.Find(target, true);
  
              if ((!Core.ML || map == Faction.Facet) && targetFaction != null)
              {
                  if (Faction.Find(from, true) != targetFaction)
                      return false;
              }
              #endregion
  			
              #region Mondain's Legacy
              if (target is Gregorio)
                  return false;
              #endregion
  
              if (map != null && (map.Rules & MapRules.BeneficialRestrictions) == 0)
                  return true; // In felucca, anything goes
  
              if (!from.Player)
                  return true; // NPCs have no restrictions
  
              if (target is BaseCreature && !((BaseCreature)target).Controlled)
                  return false; // Players cannot heal uncontrolled mobiles
  
              if (XmlPoints.AreInAnyGame(target))
                  return XmlPoints.AreTeamMembers(from, target);
  
              if (from is PlayerMobile && ((PlayerMobile)from).Young && target is BaseCreature &&
                  ((BaseCreature) target).Controlled)
                  return true;
  
              if (from is PlayerMobile && ((PlayerMobile)from).Young && (!(target is PlayerMobile) || !((PlayerMobile)target).Young))
                  return false; // Young players cannot perform beneficial actions towards older players
  
              Guild fromGuild = from.Guild as Guild;
              Guild targetGuild = target.Guild as Guild;
  
              if (fromGuild != null && targetGuild != null && (targetGuild == fromGuild || fromGuild.IsAlly(targetGuild)))
                  return true; // Guild members can be beneficial
  
              return CheckBeneficialStatus(GetGuildStatus(from), GetGuildStatus(target));
          }
  
          public static bool Mobile_AllowHarmful(Mobile from, Mobile target)
          {
              if (from == null || target == null || from.IsStaff() || target.IsStaff())
                  return true;
  
              #region Mondain's Legacy
              if (target is Gregorio)
              {
                  if (Gregorio.IsMurderer(from))
                      return true;
  				
                  from.SendLocalizedMessage(1075456); // You are not allowed to damage this NPC unless your on the Guilty Quest
                  return false;
              }
              #endregion
  			
              #region Dueling
              PlayerMobile pmFrom = from as PlayerMobile;
              PlayerMobile pmTarg = target as PlayerMobile;
  
              if (pmFrom == null && from is BaseCreature)
              {
                  BaseCreature bcFrom = (BaseCreature)from;
  
                  if (bcFrom.Summoned)
                      pmFrom = bcFrom.SummonMaster as PlayerMobile;
              }
  
              if (pmTarg == null && target is BaseCreature)
              {
                  BaseCreature bcTarg = (BaseCreature)target;
  
                  if (bcTarg.Summoned)
                      pmTarg = bcTarg.SummonMaster as PlayerMobile;
              }
  
              if (pmFrom != null && pmTarg != null)
              {
                  if (pmFrom.DuelContext != pmTarg.DuelContext && ((pmFrom.DuelContext != null && pmFrom.DuelContext.Started) || (pmTarg.DuelContext != null && pmTarg.DuelContext.Started)))
                      return false;
  
                  if (pmFrom.DuelContext != null && pmFrom.DuelContext == pmTarg.DuelContext && ((pmFrom.DuelContext.StartedReadyCountdown && !pmFrom.DuelContext.Started) || pmFrom.DuelContext.Tied || pmFrom.DuelPlayer.Eliminated || pmTarg.DuelPlayer.Eliminated))
                      return false;
  
                  if (pmFrom.DuelContext != null && pmFrom.DuelContext == pmTarg.DuelContext && pmFrom.DuelContext.m_Tournament != null && pmFrom.DuelContext.m_Tournament.IsNotoRestricted && pmFrom.DuelPlayer != null && pmTarg.DuelPlayer != null && pmFrom.DuelPlayer.Participant == pmTarg.DuelPlayer.Participant)
                      return false;
  
                  if (pmFrom.DuelContext != null && pmFrom.DuelContext == pmTarg.DuelContext && pmFrom.DuelContext.Started)
                      return true;
              }
  
              if ((pmFrom != null && pmFrom.DuelContext != null && pmFrom.DuelContext.Started) || (pmTarg != null && pmTarg.DuelContext != null && pmTarg.DuelContext.Started))
                  return false;
  
              Engines.ConPVP.SafeZone sz = from.Region.GetRegion(typeof(Engines.ConPVP.SafeZone)) as Engines.ConPVP.SafeZone;
  
              if (sz != null /*&& sz.IsDisabled()*/)
                  return false;
  
              sz = target.Region.GetRegion(typeof(Engines.ConPVP.SafeZone)) as Engines.ConPVP.SafeZone;
  
              if (sz != null /*&& sz.IsDisabled()*/)
                  return false;
              #endregion
  
              Map map = from.Map;
  
              if (map != null && (map.Rules & MapRules.HarmfulRestrictions) == 0)
                  return true; // In felucca, anything goes
  
              BaseCreature bc = from as BaseCreature;
  
              if (!from.Player && !(bc != null && bc.GetMaster() != null && bc.GetMaster().IsPlayer()))
              {
                  if (!CheckAggressor(from.Aggressors, target) && !CheckAggressed(from.Aggressed, target) && target is PlayerMobile && ((PlayerMobile)target).CheckYoungProtection(from))
                      return false;
  
                  return true; // Uncontrolled NPCs are only restricted by the young system
              }
  
              if (XmlPoints.AreChallengers(from, target))
                  return true;
  
              Guild fromGuild = GetGuildFor(from.Guild as Guild, from);
              Guild targetGuild = GetGuildFor(target.Guild as Guild, target);
  
              if (fromGuild != null && targetGuild != null && (fromGuild == targetGuild || fromGuild.IsAlly(targetGuild) || fromGuild.IsEnemy(targetGuild)))
                  return true; // Guild allies or enemies can be harmful
  
              if (target is BaseCreature && (((BaseCreature)target).Controlled || (((BaseCreature)target).Summoned && from != ((BaseCreature)target).SummonMaster)))
  
gt:                 return false; // Cannot harm other controlled mobiles