using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using Server;
using Server.Mobiles;
namespace Server.Items
{
public class ItemSets
{
private static Dictionary<Type, ItemSet> m_Registry = new Dictionary<Type, ItemSet>();
public static Dictionary<Type, ItemSet> Registry { get { return m_Registry; } }
private static Dictionary<Type, List<ItemSet>> m_PartRegistry = new Dictionary<Type, List<ItemSet>>();
public static Dictionary<Type, List<ItemSet>> PartRegistry { get { return m_PartRegistry; } }
public static void Initialize()
{
Type[] types = Assembly.GetAssembly(typeof(ItemSet)).GetTypes();
foreach (Type type in types)
{
if (type != null && type.IsSubclassOf(typeof(ItemSet)) && !type.IsAbstract)
{
ItemSet set = (ItemSet)type.GetConstructor(new Type[0]).Invoke(new object[0]);
if (set == null)
continue;
Register(set);
}
}
}
private static void Register(ItemSet set)
{
//Console.WriteLine("Registering: {0} - {1}", set.Name, set.TypeOf.Name);
if (!m_Registry.ContainsKey(set.TypeOf))
m_Registry.Add(set.TypeOf, set);
foreach (SetPart part in set.Parts)
{
//Console.WriteLine("Registering Part: {0} - {1}", part.Name, part.TypeOf.Name);
if (!m_PartRegistry.ContainsKey(part.TypeOf))
m_PartRegistry.Add(part.TypeOf, new List<ItemSet>());
if (m_PartRegistry[part.TypeOf] == null)
m_PartRegistry[part.TypeOf] = new List<ItemSet>();
m_PartRegistry[part.TypeOf].Add(set);
}
}
public static void Invalidate(Mobile owner)
{
foreach (Item part in owner.Items)
{
if (part == null || part.Deleted)
continue;
if (IsEquipped(part))
CheckPartAdded(owner, part);
else
CheckPartRemoved(owner, part);
}
}
public static bool IsEquipped(Item item)
{
if (item.Parent != null)
{
object parent = item.Parent;
if (parent is Mobile)
{
Mobile parent_m = (Mobile)parent;
if (parent_m.Items.Contains(this))
return true;
}
}
return false;
}
public static void Activate(Mobile owner)
{
foreach (Item part in owner.Items)
{
if (part == null || part.Deleted)
continue;
List<ItemSet> sets = ItemSets.Find(part);
if (sets == null)
return;
foreach (ItemSet set in sets)
{
if (set == null)
continue;
int partCount = set.Invalidate(owner, false);
set.Activate(owner, partCount);
}
}
}
public static void Deactivate(Mobile owner)
{
foreach (Item part in owner.Items)
{
if (part == null || part.Deleted)
continue;
List<ItemSet> sets = ItemSets.Find(part);
if (sets == null)
return;
foreach (ItemSet set in sets)
{
if (set == null)
continue;
set.Deactivate(owner, 0);
}
}
}
public static void CheckPartAdded(Mobile owner, Item part)
{
//Console.WriteLine("Equip Part: {0} - {1}", part.Name, part.GetType().Name);
List<ItemSet> sets = ItemSets.Find(part);
if (sets == null)
return;
foreach (ItemSet set in sets)
{
if (set == null)
continue;
//Console.WriteLine("Found Set: {0} - {1}", set.Name, set.TypeOf.Name);
set.PartAdded(owner, part);
}
}
public static void CheckPartRemoved(Mobile owner, Item part)
{
//Console.WriteLine("Remove Part: {0} - {1}", part.Name, part.GetType().Name);
List<ItemSet> sets = ItemSets.Find(part);
if (sets == null)
return;
foreach (ItemSet set in sets)
{
if (set == null)
continue;
//Console.WriteLine("Found Set: {0} - {1}", set.Name, set.TypeOf.Name);
set.PartRemoved(owner, part);
}
}
public static List<ItemSet> Find(Item part)
{
Type partType = part.GetType();
//Console.WriteLine("Finding Sets From Part: {0} - {1}", part.Name, partType.Name);
if (m_PartRegistry.ContainsKey(partType))
{
if (m_PartRegistry[partType] == null)
m_PartRegistry[partType] = new List<ItemSet>();
return m_PartRegistry[partType];
}
return new List<ItemSet>();
}
public static void EquipTo(Mobile owner, Type setType)
{
try
{
ItemSet set = ItemSets.GetInstance(setType);
if (set == null)
return;
for (int i = 0; i < set.Parts.Length; i++)
{
SetPart part = set.Parts[i];
if (part == null || !part.Valid)
continue;
Item item = part.TypeOf.GetConstructor(new Type[0]).Invoke(new object[0]) as Item;
owner.AddItem(item);
}
set.Invalidate(owner, true);
}
catch { }
}
public static Item GetRandomPart(Type setType)
{
try
{
ItemSet set = ItemSets.GetInstance(setType);
if (set == null)
return null;
SetPart part = set.Parts[Utility.Random(set.Parts.Length)];
while (!part.Valid)
{
part = set.Parts[Utility.Random(set.Parts.Length)];
}
Item item = part.TypeOf.GetConstructor(new Type[0]).Invoke(new object[0]) as Item;
if (item != null && !item.Deleted)
return item;
}
catch { }
return null;
}
public static Type GetRandomPartType(Type setType)
{
ItemSet set = ItemSets.GetInstance(setType);
SetPart part = set.Parts[Utility.Random(set.Parts.Length)];
while (!part.Valid)
{
part = set.Parts[Utility.Random(set.Parts.Length)];
}
return part.TypeOf;
}
public static ItemSet GetInstance(Type setType)
{
if (m_Registry.ContainsKey(setType))
return m_Registry[setType];
return null;
}
}
}