/*  This script is put in the OnEnter of a random forest encounter map.
    The map will have all the encounters below already in place where
    the PCs will arrive, but they will be inactive.  This script will
    randomly choose which encounter to activate.  For undead, the script
    checks to see if it is day with a GetIsDay() function, and if it is,
    will re-run the function to try to get a non-undead encounter.
    Theoretically, this could be an infinite loop, but with the number
    of encounters here, it won't happen.
*/

//------------RandomEncounter Function------------------------------------------
//Follows is the list of possible encounters or encounter types
//See the end on how to add an enounter
void RandomEncounter()
{//begin RandomEncounter function
object oEncounter;
object oPC=GetPCSpeaker();
object oWay=OBJECT_SELF;//added to fix "No enemies" issue
int iDieRoll2;
int iDieRoll;
//Randomizer
int iSeed=FloatToInt(100*log(IntToFloat(GetTimeMillisecond()+GetCalendarDay()+GetTimeHour())));
int i;
for (i=0;i!=iSeed;i++)
    {
    iDieRoll=Random(67);
    }  //This number should match iDieRoll below

//Roll random encounter or encounter type
iDieRoll=Random(67)+1;  //change this to match the max if you add encounters

if (iDieRoll==1)
    {oEncounter=GetNearestObjectByTag("Badger",oWay);
    SetEncounterActive(TRUE,oEncounter);
    return;}
else if ((iDieRoll>1)&&iDieRoll<7)
    {oEncounter=GetNearestObjectByTag("Bandit",oWay);
    SetEncounterActive(TRUE,oEncounter);
    return;}
else if ((iDieRoll>6)&&iDieRoll<10) //Encounter type beetle
    {iDieRoll2=d4();
    switch (iDieRoll2)
        {
        case 1:
            oEncounter=GetNearestObjectByTag("BombBeetle",oWay);
            SetEncounterActive(TRUE,oEncounter);
            break;
        case 2:
            oEncounter=GetNearestObjectByTag("FireBeetle",oWay);
            SetEncounterActive(TRUE,oEncounter);
            break;
        case 3:
            oEncounter=GetNearestObjectByTag("StagBeetle",oWay);
            SetEncounterActive(TRUE,oEncounter);
            break;
        case 4:
            oEncounter=GetNearestObjectByTag("StinkBeetle",oWay);
            SetEncounterActive(TRUE,oEncounter);
            break;
        default:
            oEncounter=GetNearestObjectByTag("BombBeetle",oWay);
            SetEncounterActive(TRUE,oEncounter);
            break;
        }
    return;
    }
else if (iDieRoll==10)
    {oEncounter=GetNearestObjectByTag("Boar",oWay);
    SetEncounterActive(TRUE,oEncounter);
    return;}
else if ((iDieRoll>10)&&(iDieRoll<13))
    {oEncounter=GetNearestObjectByTag("BrownBear",oWay);
    SetEncounterActive(TRUE,oEncounter);
    return;}
else if (iDieRoll==13)
    {oEncounter=GetNearestObjectByTag("Bugbear",oWay);
    SetEncounterActive(TRUE,oEncounter);
    return;}
else if ((iDieRoll>13)&&(iDieRoll<19))
    {oEncounter=GetNearestObjectByTag("Character",oWay);
    SetEncounterActive(TRUE,oEncounter);
    return;}
else if (iDieRoll==19)
    {oEncounter=GetNearestObjectByTag("Cougar",oWay);
    SetEncounterActive(TRUE,oEncounter);
    return;}
else if ((iDieRoll>19)&&(iDieRoll<22))
    {oEncounter=GetNearestObjectByTag("DireTiger",oWay);
    SetEncounterActive(TRUE,oEncounter);
    return;}
else if ((iDieRoll>21)&&(iDieRoll<25))
    {oEncounter=GetNearestObjectByTag("DireWolf",oWay);
    SetEncounterActive(TRUE,oEncounter);
    return;}
else if (iDieRoll==25)
    {oEncounter=GetNearestObjectByTag("Dog",oWay);
    SetEncounterActive(TRUE,oEncounter);
    return;}
else if (iDieRoll==26) //Encounter type giant
    {iDieRoll2=Random(90)+1;
    if ((iDieRoll2>0)&&(iDieRoll2<8))
        {oEncounter=GetNearestObjectByTag("Ettin",oWay);
        SetEncounterActive(TRUE,oEncounter);
        return;}
    else if ((iDieRoll2>7)&&(iDieRoll2<86))
        {oEncounter=GetNearestObjectByTag("HillGiant",oWay);
        SetEncounterActive(TRUE,oEncounter);
        return;}
    else if ((iDieRoll2>85)&&(iDieRoll2<91))
        {oEncounter=GetNearestObjectByTag("StoneGiant",oWay);
        SetEncounterActive(TRUE,oEncounter);
        return;}
    }
else if (iDieRoll==27)  //Encounter type dragon
    {iDieRoll2=Random(59)+1;
    if ((iDieRoll2>0)&&(iDieRoll2<17))
        {oEncounter=GetNearestObjectByTag("BlackDragon",oWay);
        SetEncounterActive(TRUE,oEncounter);
        return;}
    else if (iDieRoll2==17)
        {oEncounter=GetNearestObjectByTag("BlueDragon",oWay);
        SetEncounterActive(TRUE,oEncounter);
        return;}
    else if ((iDieRoll2>17)&&(iDieRoll2<58))
        {oEncounter=GetNearestObjectByTag("GreenDragon",oWay);
        SetEncounterActive(TRUE,oEncounter);
        return;}
    else if (iDieRoll2==58)
        {oEncounter=GetNearestObjectByTag("RedDragon",oWay);
        SetEncounterActive(TRUE,oEncounter);
        return;}
    else if (iDieRoll2==59)
        {oEncounter=GetNearestObjectByTag("WhiteDragon",oWay);
        SetEncounterActive(TRUE,oEncounter);
        return;}
    }
else if (iDieRoll==28)
    {oEncounter=GetNearestObjectByTag("GreyRender",oWay);
    SetEncounterActive(TRUE,oEncounter);
    return;}
else if ((iDieRoll>28)&&(iDieRoll<33)) //Encounter type humanoid
    {iDieRoll2=Random(80)+1;
    if ((iDieRoll2>0)&&(iDieRoll2<11))
        {oEncounter=GetNearestObjectByTag("Goblin",oWay);
        SetEncounterActive(TRUE,oEncounter);
        return;}
    else if ((iDieRoll2>10)&&(iDieRoll2<61))
        {oEncounter=GetNearestObjectByTag("Kobold",oWay);
        SetEncounterActive(TRUE,oEncounter);
        return;}
    else if ((iDieRoll2>60)&&(iDieRoll2<81))
        {oEncounter=GetNearestObjectByTag("Orc",oWay);
        SetEncounterActive(TRUE,oEncounter);
        return;}
    }
else if (iDieRoll==33)
    {oEncounter=GetNearestObjectByTag("Krenshar",oWay);
    SetEncounterActive(TRUE,oEncounter);
    return;}
else if (iDieRoll==34)
    {oEncounter=GetNearestObjectByTag("Lion",oWay);
    SetEncounterActive(TRUE,oEncounter);
    return;}
else if ((iDieRoll>34)&&(iDieRoll<37)) //Encounter type lycanthrope
    {iDieRoll2=d20();
    if ((iDieRoll2>0)&&(iDieRoll2<6))
        {oEncounter=GetNearestObjectByTag("Aranea",oWay);
        SetEncounterActive(TRUE,oEncounter);
        return;}
    else if ((iDieRoll2>5)&&(iDieRoll2<11))
        {oEncounter=GetNearestObjectByTag("Wererat",oWay);
        SetEncounterActive(TRUE,oEncounter);
        return;}
    else if ((iDieRoll2>10)&&(iDieRoll2<21))
        {oEncounter=GetNearestObjectByTag("Werewolf",oWay);
        SetEncounterActive(TRUE,oEncounter);
        return;}
    }
else if (iDieRoll==37)
    {oEncounter=GetNearestObjectByTag("Malar",oWay);
    SetEncounterActive(TRUE,oEncounter);
    return;}
else if ((iDieRoll>37)&&(iDieRoll<42))
    {oEncounter=GetNearestObjectByTag("Ogre",oWay);
    SetEncounterActive(TRUE,oEncounter);
    return;}
else if ((iDieRoll>41)&&(iDieRoll<45))
    {oEncounter=GetNearestObjectByTag("Spider",oWay);
    SetEncounterActive(TRUE,oEncounter);
    return;}
else if (iDieRoll==45)
    {oEncounter=GetNearestObjectByTag("Stirge",oWay);
    SetEncounterActive(TRUE,oEncounter);
    return;}
else if (iDieRoll==46)
    {oEncounter=GetNearestObjectByTag("Troll",oWay);
    SetEncounterActive(TRUE,oEncounter);
    return;}
else if ((iDieRoll>46)&&(iDieRoll<49)) //Encounter type undead
    {//Check to see if it is night and retry script if it is
    if (GetIsDay())
        {RandomEncounter();
        return;}
    iDieRoll2=Random(125+1);
    if ((iDieRoll2>0)&&(iDieRoll2<6))
        {oEncounter=GetNearestObjectByTag("Allip",oWay);
        SetEncounterActive(TRUE,oEncounter);
        return;}
    else if ((iDieRoll2>5)&&(iDieRoll2<10))
        {oEncounter=GetNearestObjectByTag("Bodak",oWay);
        SetEncounterActive(TRUE,oEncounter);
        return;}
    else if ((iDieRoll2>9)&&(iDieRoll2<20))
        {oEncounter=GetNearestObjectByTag("Curst",oWay);
        SetEncounterActive(TRUE,oEncounter);
        return;}
    else if ((iDieRoll2>19)&&(iDieRoll2<30))
        {oEncounter=GetNearestObjectByTag("Ghast",oWay);
        SetEncounterActive(TRUE,oEncounter);
        return;}
    else if ((iDieRoll2>29)&&(iDieRoll2<73))
        {oEncounter=GetNearestObjectByTag("Ghoul",oWay);
        SetEncounterActive(TRUE,oEncounter);
        return;}
    else if (iDieRoll2==73)
        {oEncounter=GetNearestObjectByTag("Lich",oWay);
        SetEncounterActive(TRUE,oEncounter);
        return;}
    else if ((iDieRoll2>73)&&(iDieRoll2<81))
        {oEncounter=GetNearestObjectByTag("Mohrg",oWay);
        SetEncounterActive(TRUE,oEncounter);
        return;}
    else if ((iDieRoll2>80)&&(iDieRoll2<95))
        {oEncounter=GetNearestObjectByTag("Shadow",oWay);
        SetEncounterActive(TRUE,oEncounter);
        return;}
    else if ((iDieRoll2>94)&&(iDieRoll2<105))
        {oEncounter=GetNearestObjectByTag("Spectre",oWay);
        SetEncounterActive(TRUE,oEncounter);
        return;}
    else if ((iDieRoll2>104)&&(iDieRoll2<115))
        {oEncounter=GetNearestObjectByTag("Vampire",oWay);
        SetEncounterActive(TRUE,oEncounter);
        return;}
    else if ((iDieRoll2>114)&&(iDieRoll2<122))
        {oEncounter=GetNearestObjectByTag("Wight",oWay);
        SetEncounterActive(TRUE,oEncounter);
        return;}
    else if ((iDieRoll2>121)&&(iDieRoll2<126))
        {oEncounter=GetNearestObjectByTag("Wraith",oWay);
        SetEncounterActive(TRUE,oEncounter);
        return;}
    }
else if (iDieRoll==49)
    {oEncounter=GetNearestObjectByTag("WilloWisp",oWay);
    SetEncounterActive(TRUE,oEncounter);
    return;}
else if ((iDieRoll>49)&&(iDieRoll<57))
    {oEncounter=GetNearestObjectByTag("Wolf",oWay);
    SetEncounterActive(TRUE,oEncounter);
    return;}
else if ((iDieRoll>56)&&(iDieRoll<60))
    {oEncounter=GetNearestObjectByTag("Worg",oWay);
    SetEncounterActive(TRUE,oEncounter);
    return;}
else if (iDieRoll==60) //Skeleton Re-animator
    {oEncounter=GetNearestObjectByTag("Skeleton",oWay);
    SetEncounterActive(TRUE,oEncounter);
    return;}
else if (iDieRoll==61) //Zombie Re-animator
    {oEncounter=GetNearestObjectByTag("Zombie",oWay);
    SetEncounterActive(TRUE,oEncounter);
    return;}
else if ((iDieRoll>61)&&(iDieRoll<67)) //Ettercap
    {oEncounter=GetNearestObjectByTag("Ettercap",oWay);
    SetEncounterActive(TRUE,oEncounter);
    return;}
else if (iDieRoll==67) //Rat
    {oEncounter=GetNearestObjectByTag("Rat",oWay);
    SetEncounterActive(TRUE,oEncounter);
    return;}
//add new encounters here by copying the "else if" above that matches what you
//want to do (i.e. ==, range, or sub-table) then change the iRoll statement
//and Randomizer back at the beginning
else FloatingTextStringOnCreature(IntToString(iDieRoll),oPC);//catch all debug - if it fires, you are missing that number in the else ifs above
}//end RandomEncounter function

void main()
{
RandomEncounter(); //choose and activate the random encounter
}