public static bool TRYCREATEGROUPPARTY(TriggerObject trigObject, XmlGroup grp, Type mobType, bool participantsOnly)
{
if (grp == null)
{
return false;
}
var mobsToParty = new List<Mobile>();
var accountsToAdd = new List<Account>();
if (participantsOnly)
{
if (grp.Participants.Count < 2)
{
return false;
}
accountsToAdd.AddRange(
grp.Participants.Cast<Mobile>().Where(mob => mob.Account is Account).Select(mob => (Account)mob.Account));
}
else
{
if (grp.Members.Count < 2)
{
return false;
}
accountsToAdd.AddRange(
grp.Members.Cast<Mobile>().Where(mob => mob.Account is Account).Select(mob => (Account)mob.Account));
}
foreach (NetState state in NetState.Instances.Where(ns => ns.Account is Account))
{
Account account = (Account)state.Account;
if (!accountsToAdd.Contains(account))
{
continue;
}
if (mobType != null)
{
if (IS(null, state.Mobile, mobType))
{
mobsToParty.Add(state.Mobile);
}
}
else
{
mobsToParty.Add(state.Mobile);
}
}
if (mobsToParty.Count < 2)
{
return false;
}
// we need to bring them all into one party, so grab the first party available
Party existingParty = null;
var mobsToAddToExistingParty = new List<Mobile>();
foreach (Mobile mobToParty in mobsToParty)
{
if (mobToParty.Party is Party)
{
if (existingParty == null)
{
existingParty = (Party)mobToParty.Party;
continue;
}
if (mobToParty.Party == existingParty)
{
continue; // already in the party
}
try // get them out of the party they were in
{
((Party)mobToParty.Party).Remove(mobToParty);
mobsToAddToExistingParty.Add(mobToParty);
}
catch
{ }
}
else
{
// not in a party yet
mobsToAddToExistingParty.Add(mobToParty);
}
}
if (existingParty == null)
{
// create a new party and add them in
Party newParty = new Party(mobsToParty[0]);
mobsToParty[0].Party = newParty;
for (int i = 1; i < mobsToParty.Count; i++)
{
newParty.Add(mobsToParty[i]);
}
}
else
{
// add the mobs that weren't in the party into the existing one
foreach (Mobile mob in mobsToAddToExistingParty)
{
existingParty.Add(mob);
}
}
return true;
}