//::///////////////////////////////////////////////
//:: FileName re_mmorpghb.nss
//:: Copyright (c) 2001 Bioware Corp.
//:://////////////////////////////////////////////
/*
This script is used in the heartbeat of the
BESIE RES tool, part of the BESIE Random Encounter
package by Ray Miller.
*/
//:://////////////////////////////////////////////
//:: Created By: Ray Miller
//:: Created On: 9-2-02
//:://////////////////////////////////////////////

#include "re_rndenc"
void main()
{
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
//Set this parameter to FALSE if you don't want your MOBs
//to receive extra gold for loot.
//
int GiveGold = TRUE
//
///////////////////////////////////////////////////////////

    ;
    string sTemplate;
    object oCreature;
    object oAmIASpawn;
    object oArea = GetArea(OBJECT_SELF);
    int iTreasure;
    int iType;
    int bSpawnOne;
    int iCounterPC;
    int iCounterParty;
    int iCounterSpawn;
    int iSpawnRadius = GetFortitudeSavingThrow(OBJECT_SELF) * 5;
    int iEncounterLevel = GetReflexSavingThrow(OBJECT_SELF);
    int iMaxSpawns = GetWillSavingThrow(OBJECT_SELF);
    float fPCFactor;
    float fChanceOfSpawn;
    float fChallengeFactor;
    float fFactor;
    if(!iEncounterLevel)
        {
        oAmIASpawn = GetFirstObjectInArea(oArea);
        while(GetIsObjectValid(oAmIASpawn))
            {
            if(GetIsPC(oAmIASpawn))
                {
                iCounterPC++;
                if(GetFactionLeader(oAmIASpawn) == oAmIASpawn)
                    {
                    iCounterParty++;
                    }
                iEncounterLevel = iEncounterLevel + GetLevelByPosition(1, oAmIASpawn) + GetLevelByPosition(2, oAmIASpawn) + GetLevelByPosition(3, oAmIASpawn);
                if(GetIsObjectValid(GetHenchman(oAmIASpawn)))
                    {
                    iEncounterLevel = iEncounterLevel + GetLevelByPosition(1, GetHenchman(oAmIASpawn)) + GetLevelByPosition(2, GetHenchman(oAmIASpawn)) + GetLevelByPosition(3, GetHenchman(oAmIASpawn));
                    }
               }
            oAmIASpawn = GetNextObjectInArea(oArea);
            }
        if(iCounterParty < 1) iCounterParty = 1;
        iEncounterLevel = FloatToInt(IntToFloat(iEncounterLevel) / IntToFloat(iCounterParty));
        }
    if(iMaxSpawns < 0)iMaxSpawns = 1;
    if(GetStringLeft(GetTag(OBJECT_SELF), 3) == "re_")
        {
        sTemplate = GetTag(OBJECT_SELF);
        }
    else
        {
        sTemplate = "random";
        }
    oAmIASpawn = GetFirstObjectInArea(oArea);
    while(GetIsObjectValid(oAmIASpawn))
        {
        if(GetLocalInt(oAmIASpawn, "bRandomEncounter") && GetLocalObject(oAmIASpawn, "oRandomEncounterSpawner") == OBJECT_SELF)
            {
            iCounterSpawn++;
            if(!iEncounterLevel) SetLocalInt(oAmIASpawn, "iRandomEncounterLifeTime", 450);
            else SetLocalInt(oAmIASpawn, "iRandomEncounterLifeTime", 900);
            int iShouldIWalk = Random(100) + 1;
            if(GetLocalInt(oAmIASpawn, "bRandomWalking") && !GetIsInCombat(oAmIASpawn))
                {
                AssignCommand(oAmIASpawn, ClearAllActions());
                DeleteLocalInt(oAmIASpawn, "bRandomWalking");
                }
            if(Random(100) + 1 < GetMaxHitPoints(OBJECT_SELF) && !GetIsInCombat(oAmIASpawn))
                {
                AssignCommand(oAmIASpawn, ActionRandomWalk());
                SetLocalInt(oAmIASpawn, "bRandomWalking", TRUE);
                }
            }
        if(GetIsPC(oAmIASpawn)) bSpawnOne = TRUE;
        oAmIASpawn = GetNextObjectInArea(oArea);
        }
    if(bSpawnOne && iCounterSpawn < iMaxSpawns)
        {
        fPCFactor = IntToFloat(iMaxSpawns) / (IntToFloat(iMaxSpawns) - ((IntToFloat(iCounterPC) / (IntToFloat(iMaxSpawns) * 0.25)) * IntToFloat(iCounterSpawn)));
        fChanceOfSpawn = ((100.0 - ((IntToFloat(iCounterSpawn) / IntToFloat(iMaxSpawns)) * 100.0)) * fPCFactor);
        oCreature = RandomEncounter(fChanceOfSpawn, OBJECT_SELF, sTemplate, 0, 1, 1, iSpawnRadius, 360, 0, iEncounterLevel, 5);
        if(GiveGold)
            {
            fChallengeFactor = GetChallengeRating(oCreature) * 30.0;
            fFactor = IntToFloat(Random(5) + 2);
            iTreasure = FloatToInt(fChallengeFactor / fFactor);
            iType = GetRacialType(oCreature);
            CreateItemOnObject("NW_IT_GOLD001", oCreature, iTreasure);
            if(iType == RACIAL_TYPE_UNDEAD || iType == RACIAL_TYPE_ABERRATION)
            CreateItemOnObject("NW_IT_MSMLMISC21", oCreature, 1);
            else if(iType == RACIAL_TYPE_CONSTRUCT ||
                    iType == RACIAL_TYPE_ELEMENTAL ||
                    iType == RACIAL_TYPE_OUTSIDER)
            CreateItemOnObject("NW_IT_MSMLMISC11", oCreature, 1);
            else
            CreateItemOnObject("NW_IT_MMIDMISC05", oCreature, 1);
            }
        }
    CleanHouse();
}
