context: using System;
  using Server.Items;
  using Server.Mobiles;
  using Server.Targeting;
  
  namespace Server.Engines.Craft
  {
      public interface IRepairable
      {
          CraftSystem RepairSystem { get; }
      }
  
      public class Repair
      {
          public Repair()
          {
          }
  
          public static void Do(Mobile from, CraftSystem craftSystem, BaseTool tool)
          {
              from.Target = new InternalTarget(craftSystem, tool);
              from.SendLocalizedMessage(1044276); // Target an item to repair.
          }
  
          public static void Do(Mobile from, CraftSystem craftSystem, RepairDeed deed)
          {
              from.Target = new InternalTarget(craftSystem, deed);
              from.SendLocalizedMessage(1044276); // Target an item to repair.
          }
  
          private class InternalTarget : Target
          {
              private readonly CraftSystem m_CraftSystem;
              private readonly BaseTool m_Tool;
              private readonly RepairDeed m_Deed;
  
              public InternalTarget(CraftSystem craftSystem, BaseTool tool)
                  : base(10, false, TargetFlags.None)
              {
                  this.m_CraftSystem = craftSystem;
                  this.m_Tool = tool;
              }
  
              public InternalTarget(CraftSystem craftSystem, RepairDeed deed)
                  : base(2, false, TargetFlags.None)
              {
                  this.m_CraftSystem = craftSystem;
                  this.m_Deed = deed;
              }
  
              private static void EndMobileRepair(object state)
              {
                  ((Mobile)state).EndAction(typeof(IRepairableMobile));
              }
  
              private int GetWeakenChance(Mobile mob, SkillName skill, int curHits, int maxHits)
              {
                  // 40% - (1% per hp lost) - (1% per 10 craft skill)
                  return (40 + (maxHits - curHits)) - (int)(((this.m_Deed != null) ? this.m_Deed.SkillLevel : mob.Skills[skill].Value) / 10);
              }
  
              private bool CheckWeaken(Mobile mob, SkillName skill, int curHits, int maxHits)
              {
                  return (this.GetWeakenChance(mob, skill, curHits, maxHits) > Utility.Random(100));
              }
  
              private int GetRepairDifficulty(int curHits, int maxHits)
              {
                  return (((maxHits - curHits) * 1250) / Math.Max(maxHits, 1)) - 250;
              }
  
              private bool CheckRepairDifficulty(Mobile mob, SkillName skill, int curHits, int maxHits)
              {
                  double difficulty = this.GetRepairDifficulty(curHits, maxHits) * 0.1;
  
                  if (this.m_Deed != null)
                  {
                      double value = this.m_Deed.SkillLevel;
                      double minSkill = difficulty - 25.0;
                      double maxSkill = difficulty + 25;
  
                      if (value < minSkill)
                          return false; // Too difficult
                      else if (value >= maxSkill)
                          return true; // No challenge
  
                      double chance = (value - minSkill) / (maxSkill - minSkill);
  
                      return (chance >= Utility.RandomDouble());
                  }
                  else
                  {
                      return mob.CheckSkill(skill, difficulty - 25.0, difficulty + 25.0);
                  }
              }
  
              private bool CheckDeed(Mobile from)
              {
                  if (this.m_Deed != null)
                  {
                      return this.m_Deed.Check(from);
                  }
  
                  return true;
              }
  
              private bool IsSpecialClothing(BaseClothing clothing)
              {
                  if (clothing is IRepairable && ((IRepairable)clothing).RepairSystem == m_CraftSystem)
                      return true;
  
                  // Clothing repairable but not craftable
                  if (this.m_CraftSystem is DefTailoring)
                  {
                      return (clothing is BearMask) ||
                             (clothing is DeerMask) ||
                             (clothing is TheMostKnowledgePerson) ||
                             (clothing is TheRobeOfBritanniaAri) ||
                             (clothing is EmbroideredOakLeafCloak);
                  }
  
                  return false;
              }
  
              private bool IsSpecialWeapon(BaseWeapon weapon)
              {
                  if (weapon is IRepairable && ((IRepairable)weapon).RepairSystem == m_CraftSystem)
                      return true;
  
                  // Weapons repairable but not craftable
                  if (this.m_CraftSystem is DefTinkering)
                  {
                      return (weapon is Cleaver) ||
                             (weapon is Hatchet) ||
                             (weapon is Pickaxe) ||
                             (weapon is ButcherKnife) ||
                             (weapon is SkinningKnife);
                  }
                  else if (this.m_CraftSystem is DefCarpentry)
                  {
                      return (weapon is Club) ||
                             (weapon is BlackStaff) ||
                             (weapon is MagicWand)
                      #region Temporary
                          // TODO: Make these items craftable
                             ||
                             (weapon is WildStaff);
                      #endregion
                  }
                  else if (this.m_CraftSystem is DefBlacksmithy)
                  {
                      return (weapon is Pitchfork)
                      #region Temporary
                          // TODO: Make these items craftable
                             ||
                             (weapon is RadiantScimitar) ||
                             (weapon is WarCleaver) ||
                             (weapon is ElvenSpellblade) ||
                             (weapon is AssassinSpike) ||
                             (weapon is Leafblade) ||
                             (weapon is RuneBlade) ||
                             (weapon is ElvenMachete) ||
                             (weapon is OrnateAxe) ||
                             (weapon is DiamondMace);
                      #endregion
                  }
                  #region Temporary
                  // TODO: Make these items craftable
                  else if (this.m_CraftSystem is DefBowFletching)
                  {
                      return (weapon is ElvenCompositeLongbow) ||
                             (weapon is MagicalShortbow);
                  }
                  #endregion
  
                  return false;
              }
  
              private bool IsSpecialJewel(BaseJewel jewel)
              {
                  return jewel is SilverBracelet || jewel is SilverRing;
              }
  
              private bool IsSpecialArmor(BaseArmor armor)
              {
                  // Armor repairable but not craftable
                  #region Temporary
                  // TODO: Make these items craftable
                  if (this.m_CraftSystem is DefTailoring)
                  {
                      return (armor is LeafTonlet) ||
                             (armor is LeafArms) ||
                             (armor is LeafChest) ||
                             (armor is LeafGloves) ||
                             (armor is LeafGorget) ||
                             (armor is LeafLegs) ||
                             (armor is HideChest) ||
                             (armor is HideGloves) ||
                             (armor is HideGorget) ||
                             (armor is HidePants) ||
                             (armor is HidePauldrons);
                  }
                  else if (this.m_CraftSystem is DefCarpentry)
                  {
                      return (armor is WingedHelm) ||
                             (armor is RavenHelm) ||
                             (armor is VultureHelm) ||
                             (armor is WoodlandArms) ||
                             (armor is WoodlandChest) ||
                             (armor is WoodlandGloves) ||
                             (armor is WoodlandGorget) ||
                             (armor is WoodlandLegs);
                  }
                  else if (this.m_CraftSystem is DefBlacksmithy)
                  {
                      return (armor is Circlet) ||
                             (armor is RoyalCirclet) ||
                             (armor is GemmedCirclet);
                  }
                  else if (this.m_CraftSystem is DefTinkering)
                  {
                      return (armor is Glasses) ||
                             (armor is ElvenGlasses) ||
                             (armor is GargishGlasses);
                  }
                  #endregion
  
                  return false;
              }
  
              protected override void OnTarget(Mobile from, object targeted)
              {
                  bool usingDeed = (this.m_Deed != null);
                  bool toDelete = false;
                  int number;
  
                  if (this.m_CraftSystem is DefTinkering && targeted is IRepairableMobile)
                  {
                      if (TryRepairMobile(from, (IRepairableMobile)targeted, usingDeed, out toDelete))
                      {
                          number = 1044279; // You repair the item.
                      }
                      else
                      {
                          number = 500426; // You can't repair that.
                      }
                  }
                  else if (targeted is Item)
                  {
                      if (from.InRange(((Item)targeted).GetWorldLocation(), 2))
                      {
                          if (!this.CheckDeed(from))
                              return;
  
                          if (!AllowsRepair(targeted, m_CraftSystem))
                          {
                              from.SendLocalizedMessage(500426); // You can't repair that.
                              return;
                          }
  
                          if (this.m_CraftSystem.CanCraft(from, this.m_Tool, targeted.GetType()) == 1044267)
                          {
                              number = 1044282; // You must be near a forge and and anvil to repair items. * Yes, there are two and's *
                          }
                          else if (!usingDeed && m_CraftSystem is DefTinkering && targeted is BrokenAutomatonHead)
                          {
                              if (((BrokenAutomatonHead)targeted).TryRepair(from))
  
gt:                                 number = 1044279; // You repair the item.