// Party-related functions
// 2002-08-03, Jason Robinson

int ANY_MEMBER = 0;
int ALL_MEMBERS = 1;

void TakeItemFromParty(object oMember, string sItemTag)
{
    // Takes item with tag sItemTag from every member of
    // oMember's party
    object oPC = GetFirstFactionMember(oMember, TRUE);
    object oItem = GetFirstItemInInventory(oPC);
    while (GetIsObjectValid(oPC) == TRUE)
    {
        while (oItem != OBJECT_INVALID)
        {

            if (GetTag(oItem) == sItemTag)
            {
                DestroyObject(oItem);
            }
            oItem = GetNextItemInInventory(oPC);
        }
        oPC = GetNextFactionMember(oMember, TRUE);
    }
}

void GiveItemToEveryPartyMember(object oMember, string sItemResRef, int nStackSize = 1)
{
    // Gives an item to everyone in oMember's party
    //
    // oMember         PC in target party
    // sItemResRef     blueprint of item to give
    // nStackSize      How many of the items to give to each party member
    object oPartyMember = GetFirstFactionMember(oMember, TRUE);
    while (GetIsObjectValid(oPartyMember) == TRUE)
    {
        CreateItemOnObject(sItemResRef, oPartyMember, nStackSize);
        oPartyMember = GetNextFactionMember(oMember, TRUE);
    }
}

int GetNumberOfPartyMembers(object oMember)
{
    // Returns the number of PCs in oMember's party
    //
    // oMember         PC in target party
    int Counter = 0;
    object oPartyMember = GetFirstFactionMember(oMember, TRUE);
    while (GetIsObjectValid(oPartyMember) == TRUE)
    {
        Counter += 1;
        oPartyMember = GetNextFactionMember(oMember, TRUE);
    }
    return Counter;
}

void SetLocalIntOnParty(object oMember, string sVariableName, int nValue)
{
    // Sets sVariableName to nValue on every PC in oMember's party
    //
    // oMember         PC in target party
    // sVariableName   Name of variable to set
    // nValue          What to set the variable to
    object oPartyMember = GetFirstFactionMember(oMember, TRUE);
    while (GetIsObjectValid(oPartyMember) == TRUE)
    {
        SetLocalInt(oPartyMember, sVariableName, nValue);
        oPartyMember = GetNextFactionMember(oMember, TRUE);
    }
}

void SetLocalFloatOnParty(object oMember, string sVariableName, float fValue)
{
    // Sets sVariableName to fValue on every PC in oMember's party
    //
    // oMember         PC in target party
    // sVariableName   Name of variable to set
    // fValue          What to set the variable to
    object oPartyMember = GetFirstFactionMember(oMember, TRUE);
    while (GetIsObjectValid(oPartyMember) == TRUE)
    {
        SetLocalFloat(oPartyMember, sVariableName, fValue);
        oPartyMember = GetNextFactionMember(oMember, TRUE);
    }
}

void SetLocalStringOnParty(object oMember, string sVariableName, string sValue)
{
    // Sets sVariableName to sValue on every PC in oMember's party
    //
    // oMember         PC in target party
    // sVariableName   Name of variable to set
    // sValue          What to set the variable to
    object oPartyMember = GetFirstFactionMember(oMember, TRUE);
    while (GetIsObjectValid(oPartyMember) == TRUE)
    {
        SetLocalString(oPartyMember, sVariableName, sValue);
        oPartyMember = GetNextFactionMember(oMember, TRUE);
    }
}

int TestPartyLocalInt(object oMember, string sVariableName, int nValueToTestFor, int nAnyOrAllFlag=0)
{
    // Returns TRUE or FALSE depending on whether sVariableName equals
    // nValueToTestFor on either all members of oMember's party or any
    // member of oMember's party
    //
    // oMember          PC in target party
    // sVariableName    Name of variable to check
    // nValueToTestFor  Check to see if variable is set to this value
    // nAnyOrAllFlag    If ALL_MEMBERS (1), then function returns TRUE only if every
    //                      party member has sVariableName set to nValueToTestFor;
    //                      If ANY_MEMBER (0), then function returns TRUE if any
    //                      party member has sVariableName set to nValueToTestFor
    int nReturnValue = FALSE;
    object oPartyMember = GetFirstFactionMember(oMember, TRUE);
    while (GetIsObjectValid(oPartyMember) == TRUE)
    {
        if (GetLocalInt(oPartyMember, sVariableName) == nValueToTestFor)
        {
            if (nAnyOrAllFlag == ANY_MEMBER)
            {
                nReturnValue = TRUE;
                return nReturnValue;
            }
            else
            {
                // nAnyOrAllFlag == ALL_MEMBERS
                nReturnValue = TRUE;
            }
        }
        else
        {
            if (nAnyOrAllFlag == ALL_MEMBERS)
            {
                nReturnValue = FALSE;
                return nReturnValue;
            }
        }
        oPartyMember = GetNextFactionMember(oMember, TRUE);
    }
    return nReturnValue;
}

int TestPartyLocalFloat(object oMember, string sVariableName, float fValueToTestFor, int nAnyOrAllFlag)
{
    // Returns TRUE or FALSE depending on whether sVariableName equals
    // fValueToTestFor on either all members of oMember's party or any
    // member of oMember's party
    //
    // oMember          PC in target party
    // sVariableName    Name of variable to check
    // fValueToTestFor  Check to see if variable is set to this value
    // nAnyOrAllFlag    If ALL_MEMBERS (1), then function returns TRUE only if every
    //                      party member has sVariableName set to fValueToTestFor;
    //                      If ANY_MEMBER (0), then function returns TRUE if any
    //                      party member has sVariableName set to fValueToTestFor
    int nReturnValue = FALSE;
    object oPartyMember = GetFirstFactionMember(oMember, TRUE);
    while (GetIsObjectValid(oPartyMember) == TRUE)
    {
        if (GetLocalFloat(oPartyMember, sVariableName) == fValueToTestFor)
        {
            if (nAnyOrAllFlag == ANY_MEMBER)
            {
                nReturnValue = TRUE;
                return nReturnValue;
            }
            else
            {
                // nAnyOrAllFlag == ALL_MEMBERS
                nReturnValue = TRUE;
            }
        }
        else
        {
            if (nAnyOrAllFlag == ALL_MEMBERS)
            {
                nReturnValue = FALSE;
                return nReturnValue;
            }
        }
        oPartyMember = GetNextFactionMember(oMember, TRUE);
    }
    return nReturnValue;
}

int TestPartyLocalString(object oMember, string sVariableName, string sValueToTestFor, int nAnyOrAllFlag)
{
    // Returns TRUE or FALSE depending on whether sVariableName equals
    // sValueToTestFor on either all members of oMember's party or any
    // member of oMember's party
    //
    // oMember          PC in target party
    // sVariableName    Name of variable to check
    // sValueToTestFor  Check to see if variable is set to this value
    // nAnyOrAllFlag    If ALL_MEMBERS (1), then function returns TRUE only if every
    //                      party member has sVariableName set to sValueToTestFor;
    //                      If ANY_MEMBER (0), then function returns TRUE if any
    //                      party member has sVariableName set to sValueToTestFor
    int nReturnValue = FALSE;
    object oPartyMember = GetFirstFactionMember(oMember, TRUE);
    while (GetIsObjectValid(oPartyMember) == TRUE)
    {
        if (GetLocalString(oPartyMember, sVariableName) == sValueToTestFor)
        {
            if (nAnyOrAllFlag == ANY_MEMBER)
            {
                nReturnValue = TRUE;
                return nReturnValue;
            }
            else
            {
                // nAnyOrAllFlag == ALL_MEMBERS
                nReturnValue = TRUE;
            }
        }
        else
        {
            if (nAnyOrAllFlag == ALL_MEMBERS)
            {
                nReturnValue = FALSE;
                return nReturnValue;
            }
        }
        oPartyMember = GetNextFactionMember(oMember, TRUE);
    }
    return nReturnValue;
}
