context: using System;
  using System.Collections;
  
  namespace Server.Items
  {
      public abstract class BaseRunicTool : BaseTool
      {
          private static readonly SkillName[] m_PossibleBonusSkills = new SkillName[]
          {
              SkillName.Swords,
              SkillName.Fencing,
              SkillName.Macing,
              SkillName.Archery,
              SkillName.Wrestling,
              SkillName.Parry,
              SkillName.Tactics,
              SkillName.Anatomy,
              SkillName.Healing,
              SkillName.Magery,
              SkillName.Meditation,
              SkillName.EvalInt,
              SkillName.MagicResist,
              SkillName.AnimalTaming,
              SkillName.AnimalLore,
              SkillName.Veterinary,
              SkillName.Musicianship,
              SkillName.Provocation,
              SkillName.Discordance,
              SkillName.Peacemaking,
              SkillName.Chivalry,
              SkillName.Focus,
              SkillName.Necromancy,
              SkillName.Stealing,
              SkillName.Stealth,
              SkillName.SpiritSpeak,
              SkillName.Bushido,
              SkillName.Ninjitsu
          };
          private static readonly SkillName[] m_PossibleSpellbookSkills = new SkillName[]
          {
              SkillName.Magery,
              SkillName.Meditation,
              SkillName.EvalInt,
              SkillName.MagicResist
          };
          private static readonly BitArray m_Props = new BitArray(MaxProperties);
          private static readonly int[] m_Possible = new int[MaxProperties];
          private static bool m_IsRunicTool;
          private static int m_LuckChance;
          private const int MaxProperties = 32;
          private CraftResource m_Resource;
          public BaseRunicTool(CraftResource resource, int itemID)
              : base(itemID)
          {
              this.m_Resource = resource;
          }
  
          public BaseRunicTool(CraftResource resource, int uses, int itemID)
              : base(uses, itemID)
          {
              this.m_Resource = resource;
          }
  
          public BaseRunicTool(Serial serial)
              : base(serial)
          {
          }
  
          [CommandProperty(AccessLevel.GameMaster)]
          public CraftResource Resource
          {
              get
              {
                  return this.m_Resource;
              }
              set
              {
                  this.m_Resource = value;
                  this.Hue = CraftResources.GetHue(this.m_Resource);
                  this.InvalidateProperties();
              }
          }
          public static int GetUniqueRandom(int count)
          {
              int avail = 0;
  
              for (int i = 0; i < count; ++i)
              {
                  if (!m_Props[i])
                      m_Possible[avail++] = i;
              }
  
              if (avail == 0)
                  return -1;
  
              int v = m_Possible[Utility.Random(avail)];
  
              m_Props.Set(v, true);
  
              return v;
          }
  
          public static void ApplyAttributesTo(BaseWeapon weapon, int attributeCount, int min, int max)
          {
              ApplyAttributesTo(weapon, false, 0, attributeCount, min, max);
          }
  
          public static void ApplyAttributesTo(BaseWeapon weapon, bool isRunicTool, int luckChance, int attributeCount, int min, int max)
          {
              m_IsRunicTool = isRunicTool;
              m_LuckChance = luckChance;
  
              AosAttributes primary = weapon.Attributes;
              AosWeaponAttributes secondary = weapon.WeaponAttributes;
  
              m_Props.SetAll(false);
  
              if (weapon is BaseRanged)
              {
                  m_Props.Set(2, true); // ranged weapons cannot be ubws or mageweapon
              }
              else
              {
                  m_Props.Set(25, true); // Only bows can be Balanced
                  m_Props.Set(26, true); // Only bows have Velocity
              }
  
              for (int i = 0; i < attributeCount; ++i)
              {
                  int random = GetUniqueRandom(27);
  
                  if (random == -1)
                      break;
  
                  switch (random)
                  {
                      case 0:
                          {
                              switch (Utility.Random(5))
                              {
                                  case 0:
                                      ApplyAttribute(secondary, min, max, AosWeaponAttribute.HitPhysicalArea, 2, 50, 2);
                                      break;
                                  case 1:
                                      ApplyAttribute(secondary, min, max, AosWeaponAttribute.HitFireArea, 2, 50, 2);
                                      break;
                                  case 2:
                                      ApplyAttribute(secondary, min, max, AosWeaponAttribute.HitColdArea, 2, 50, 2);
                                      break;
                                  case 3:
                                      ApplyAttribute(secondary, min, max, AosWeaponAttribute.HitPoisonArea, 2, 50, 2);
                                      break;
                                  case 4:
                                      ApplyAttribute(secondary, min, max, AosWeaponAttribute.HitEnergyArea, 2, 50, 2);
                                      break;
                              }
  
                              break;
                          }
                      case 1:
                          {
                              switch (Utility.Random(4))
                              {
                                  case 0:
                                      ApplyAttribute(secondary, min, max, AosWeaponAttribute.HitMagicArrow, 2, 50, 2);
                                      break;
                                  case 1:
                                      ApplyAttribute(secondary, min, max, AosWeaponAttribute.HitHarm, 2, 50, 2);
                                      break;
                                  case 2:
                                      ApplyAttribute(secondary, min, max, AosWeaponAttribute.HitFireball, 2, 50, 2);
                                      break;
                                  case 3:
                                      ApplyAttribute(secondary, min, max, AosWeaponAttribute.HitLightning, 2, 50, 2);
                                      break;
                              }
  
                              break;
                          }
                      case 2:
                          {
                              switch (Utility.Random(2))
                              {
                                  case 0:
                                      ApplyAttribute(secondary, min, max, AosWeaponAttribute.UseBestSkill, 1, 1);
                                      break;
                                  case 1:
                                      ApplyAttribute(secondary, min, max, AosWeaponAttribute.MageWeapon, 1, 10);
                                      break;
                              }
  
                              break;
                          }
                      case 3:
                          ApplyAttribute(primary, min, max, AosAttribute.WeaponDamage, 1, 50);
                          break;
                      case 4:
                          ApplyAttribute(primary, min, max, AosAttribute.DefendChance, 1, 15);
                          break;
                      case 5:
                          ApplyAttribute(primary, min, max, AosAttribute.CastSpeed, 1, 1);
                          break;
                      case 6:
                          ApplyAttribute(primary, min, max, AosAttribute.AttackChance, 1, 15);
                          break;
                      case 7:
                          ApplyAttribute(primary, min, max, AosAttribute.Luck, 1, 100);
                          break;
                      case 8:
                          ApplyAttribute(primary, min, max, AosAttribute.WeaponSpeed, 5, 30, 5);
                          break;
                      case 9:
                          ApplyAttribute(primary, min, max, AosAttribute.SpellChanneling, 1, 1);
                          break;
                      case 10:
                          ApplyAttribute(secondary, min, max, AosWeaponAttribute.HitDispel, 2, 50, 2);
                          break;
                      case 11:
                          ApplyAttribute(secondary, min, max, AosWeaponAttribute.HitLeechHits, 2, 50, 2);
                          break;
                      case 12:
                          ApplyAttribute(secondary, min, max, AosWeaponAttribute.HitLowerAttack, 2, 50, 2);
                          break;
                      case 13:
                          ApplyAttribute(secondary, min, max, AosWeaponAttribute.HitLowerDefend, 2, 50, 2);
                          break;
                      case 14:
                          ApplyAttribute(secondary, min, max, AosWeaponAttribute.HitLeechMana, 2, 50, 2);
                          break;
                      case 15:
                          ApplyAttribute(secondary, min, max, AosWeaponAttribute.HitLeechStam, 2, 50, 2);
                          break;
                      case 16:
                          ApplyAttribute(secondary, min, max, AosWeaponAttribute.LowerStatReq, 10, 100, 10);
                          break;
                      case 17:
                          ApplyAttribute(secondary, min, max, AosWeaponAttribute.ResistPhysicalBonus, 1, 15);
                          break;
                      case 18:
                          ApplyAttribute(secondary, min, max, AosWeaponAttribute.ResistFireBonus, 1, 15);
                          break;
                      case 19:
                          ApplyAttribute(secondary, min, max, AosWeaponAttribute.ResistColdBonus, 1, 15);
                          break;
                      case 20:
                          ApplyAttribute(secondary, min, max, AosWeaponAttribute.ResistPoisonBonus, 1, 15);
                          break;
                      case 21:
                          ApplyAttribute(secondary, min, max, AosWeaponAttribute.ResistEnergyBonus, 1, 15);
                          break;
                      case 22:
                          ApplyAttribute(secondary, min, max, AosWeaponAttribute.DurabilityBonus, 10, 100, 10);
                          break;
                      case 23:
                          weapon.Slayer = GetRandomSlayer();
                          break;
                      case 24:
                          GetElementalDamages(weapon);
                          break;
                      case 25:
                          BaseRanged brb = weapon as BaseRanged;
                          brb.Balanced = true;
                          break;
                      case 26:
                          BaseRanged brv = weapon as BaseRanged;
                          brv.Velocity = (Utility.RandomMinMax(2, 50));
                          break;
                  }
              }
          }
  
          public static void GetElementalDamages(BaseWeapon weapon)
          {
              GetElementalDamages(weapon, true);
          }
  
          public static void GetElementalDamages(BaseWeapon weapon, bool randomizeOrder)
          {
              int fire, phys, cold, nrgy, pois, chaos, direct;
  
              weapon.GetDamageTypes(null, out phys, out fire, out cold, out pois, out nrgy, out chaos, out direct);
  
              int totalDamage = phys;
  
              AosElementAttribute[] attrs = new AosElementAttribute[]
              {
                  AosElementAttribute.Cold,
                  AosElementAttribute.Energy,
                  AosElementAttribute.Fire,
                  AosElementAttribute.Poison
              };
  
              if (randomizeOrder)
              {
                  for (int i = 0; i < attrs.Length; i++)
                  {
                      int rand = Utility.Random(attrs.Length);
                      AosElementAttribute temp = attrs[i];
  
                      attrs[i] = attrs[rand];
                      attrs[rand] = temp;
                  }
              }
  
              /*
              totalDamage = AssignElementalDamage( weapon, AosElementAttribute.Cold,		totalDamage );
              totalDamage = AssignElementalDamage( weapon, AosElementAttribute.Energy,	totalDamage );
              totalDamage = AssignElementalDamage( weapon, AosElementAttribute.Fire,		totalDamage );
              totalDamage = AssignElementalDamage( weapon, AosElementAttribute.Poison,	totalDamage );
  
              weapon.AosElementDamages[AosElementAttribute.Physical] = 100 - totalDamage;
              * */
  
              for (int i = 0; i < attrs.Length; i++)
                  totalDamage = AssignElementalDamage(weapon, attrs[i], totalDamage);
  
              //Order is Cold, Energy, Fire, Poison -> Physical left
              //Cannot be looped, AoselementAttribute is 'out of order'
  
              weapon.Hue = weapon.GetElementalDamageHue();
          }
  
          public static SlayerName GetRandomSlayer()
          {
              // TODO: Check random algorithm on OSI
              SlayerGroup[] groups = SlayerGroup.Groups;
  
              if (groups.Length == 0)
                  return SlayerName.None;
  
              SlayerGroup group = groups[Utility.Random(groups.Length - 1)]; //-1 To Exclude the Fey Slayer which appears ONLY on a certain artifact.
              SlayerEntry entry;
  
              if (group.Entries.Length == 0 || 10 > Utility.Random(100)) // 10% chance to do super slayer
              {
                  entry = group.Super;
              }
              else
              {
                  SlayerEntry[] entries = group.Entries;
                  entry = entries[Utility.Random(entries.Length)];
              }
  
              return entry.Name;
          }
  
          public static void ApplyAttributesTo(BaseArmor armor, int attributeCount, int min, int max)
          {
              ApplyAttributesTo(armor, false, 0, attributeCount, min, max);
          }
  
          public static void ApplyAttributesTo(BaseArmor armor, bool isRunicTool, int luckChance, int attributeCount, int min, int max)
          {
              m_IsRunicTool = isRunicTool;
              m_LuckChance = luckChance;
  
              AosAttributes primary = armor.Attributes;
              AosArmorAttributes secondary = armor.ArmorAttributes;
  
              m_Props.SetAll(false);
  
              bool isShield = (armor is BaseShield);
              int baseCount = (isShield ? 7 : 20);
              int baseOffset = (isShield ? 0 : 4);
  
              if (!isShield && armor.MeditationAllowance == ArmorMeditationAllowance.All)
                  m_Props.Set(3, true); // remove mage armor from possible properties
              if (armor.Resource >= CraftResource.RegularLeather && armor.Resource <= CraftResource.BarbedLeather)
              {
                  m_Props.Set(0, true); // remove lower requirements from possible properties for leather armor
                  m_Props.Set(2, true); // remove durability bonus from possible properties
              }
              if (armor.RequiredRace == Race.Elf)
                  m_Props.Set(7, true); // elves inherently have night sight and elf only armor doesn't get night sight as a mod
  
              for (int i = 0; i < attributeCount; ++i)
              {
                  int random = GetUniqueRandom(baseCount);
  
                  if (random == -1)
                      break;
  
                  random += baseOffset;
  
                  switch (random)
                  {
                      /* Begin Sheilds */
                      case 0:
                          ApplyAttribute(primary, min, max, AosAttribute.SpellChanneling, 1, 1);
                          break;
                      case 1:
                          ApplyAttribute(primary, min, max, AosAttribute.DefendChance, 1, 15);
                          break;
                      case 2:
                          if (Core.ML)
                          {
                              ApplyAttribute(primary, min, max, AosAttribute.ReflectPhysical, 1, 15);
                          }
                          else
                          {
                              ApplyAttribute(primary, min, max, AosAttribute.AttackChance, 1, 15);
                          }
                          break;
                      case 3:
                          ApplyAttribute(primary, min, max, AosAttribute.CastSpeed, 1, 1);
                          break;
                      /* Begin Armor */
                      case 4:
                          ApplyAttribute(secondary, min, max, AosArmorAttribute.LowerStatReq, 10, 100, 10);
                          break;
                      case 5:
                          ApplyAttribute(secondary, min, max, AosArmorAttribute.SelfRepair, 1, 5);
                          break;
                      case 6:
                          ApplyAttribute(secondary, min, max, AosArmorAttribute.DurabilityBonus, 10, 100, 10);
                          break;
                      /* End Shields */
                      case 7:
                          ApplyAttribute(secondary, min, max, AosArmorAttribute.MageArmor, 1, 1);
                          break;
                      case 8:
                          ApplyAttribute(primary, min, max, AosAttribute.RegenHits, 1, 2);
                          break;
                      case 9:
                          ApplyAttribute(primary, min, max, AosAttribute.RegenStam, 1, 3);
                          break;
                      case 10:
                          ApplyAttribute(primary, min, max, AosAttribute.RegenMana, 1, 2);
                          break;
                      case 11:
                          ApplyAttribute(primary, min, max, AosAttribute.NightSight, 1, 1);
                          break;
                      case 12:
                          ApplyAttribute(primary, min, max, AosAttribute.BonusHits, 1, 5);
                          break;
                      case 13:
                          ApplyAttribute(primary, min, max, AosAttribute.BonusStam, 1, 8);
                          break;
                      case 14:
                          ApplyAttribute(primary, min, max, AosAttribute.BonusMana, 1, 8);
                          break;
                      case 15:
                          ApplyAttribute(primary, min, max, AosAttribute.LowerManaCost, 1, 8);
                          break;
                      case 16:
                          ApplyAttribute(primary, min, max, AosAttribute.LowerRegCost, 1, 20);
                          break;
                      case 17:
                          ApplyAttribute(primary, min, max, AosAttribute.Luck, 1, 100);
                          break;
                      case 18:
                          ApplyAttribute(primary, min, max, AosAttribute.ReflectPhysical, 1, 15);
                          break;
                      case 19:
                          ApplyResistance(armor, min, max, ResistanceType.Physical, 1, 15);
                          break;
                      case 20:
                          ApplyResistance(armor, min, max, ResistanceType.Fire, 1, 15);
                          break;
                      case 21:
                          ApplyResistance(armor, min, max, ResistanceType.Cold, 1, 15);
                          break;
                      case 22:
                          ApplyResistance(armor, min, max, ResistanceType.Poison, 1, 15);
                          break;
                      case 23:
  
gt:                         ApplyResistance(armor, min, max, ResistanceType.Energy, 1, 15);