context: using System;
  using System.Collections.Generic;
  using Server.Items;
  using Server.Guilds;
  using Server.Multis;
  using Server.Mobiles;
  using Server.Engines.PartySystem;
  using Server.Factions;
  
  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.AccessLevel > AccessLevel.Player || target.AccessLevel > AccessLevel.Player )
  				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
  			
  
  			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( 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.AccessLevel > AccessLevel.Player || target.AccessLevel > AccessLevel.Player )
  				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.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().AccessLevel == AccessLevel.Player ) )
  			{
  				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
  			}
  
  			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)) )
  				return false; // Cannot harm other controlled mobiles
  
  			if( target.Player )
  				return false; // Cannot harm other players
  
  			if( !(target is BaseCreature && ((BaseCreature)target).InitialInnocent) )
  			{
  				if( Notoriety.Compute( from, target ) == Notoriety.Innocent )
  					return false; // Cannot harm innocent mobiles
  			}
  
  			return true;
  		}
  
  		public static Guild GetGuildFor( Guild def, Mobile m )
  		{
  			Guild g = def;
  
  			BaseCreature c = m as BaseCreature;
  
  			if( c != null && c.Controlled && c.ControlMaster != null )
  			{
  				c.DisplayGuildTitle = false;
  
  				if( c.Map != Map.Internal && (Core.AOS || Guild.NewGuildSystem || c.ControlOrder == OrderType.Attack || c.ControlOrder == OrderType.Guard) )
  					g = (Guild)(c.Guild = c.ControlMaster.Guild);
  				else if( c.Map == Map.Internal || c.ControlMaster.Guild == null )
  
gt: 					g = (Guild)(c.Guild = null);