Author Sticky: Post Working Scripts Here
Afloyd Posted: Monday, 29 July 01:21PM
Re-posting this here per request:


NWScript:
// Custom Encounter script by Allan Floyd

// Written 7/28/02 afloyd@houston.rr.com

//

void SpawnCreatures(int Mode, string ResRef, string SpawnWP,int Count, int NumWP);

void main()

{

//** Figures the total levels in the party

    int PCLevels = 0;   //what is the total level of all players

    int NumPCs = 0;     //how many players are here?

    int CurrPCLvl = 0;  //track the current PC to determine highest level

    string sPCLevels;

    string sNum;

    object oPC = GetFirstPC();

    while (oPC!=OBJECT_INVALID)

    {

      CurrPCLvl =  GetLevelByPosition(1,oPC) + GetLevelByPosition(2,oPC) + GetLevelByPosition(3,oPC);

      PCLevels += CurrPCLvl;

      oPC=GetNextPC();

      NumPCs++;

      sPCLevels = IntToString(PCLevels);

      sNum = IntToString(NumPCs);

    }

//** Replace the variables below with the resrefs and CR or HD of the monsters in the spawn

//** Replace Difficulty with the multiplier you want for the encounter (2.0 will spawn twice the Party Levels)

    string MonOne = "crhalf"  ; float MLOne = 0.5;  //resref and CR/HD spawn1

    string MonTwo = "crone"   ; float MLTwo = 0.5;  //resref and CR/HD spawn2

    string MonThree = "crtwo" ; float MLThree = 0.5;//resref and CR/HD spawn3

    string MonFour = "crfour" ; float MLFour = 0.5; //resref and CR/HD spawn4

    float Difficulty = 10.0;                        //Difficulty Multiplier

    int SOne;int STwo;int SThree;int SFour;         //Stores end # of each spawn

    float TL = IntToFloat(PCLevels);                //convert Levels for math

    float Temp;                                     //work variable

    TL = TL * Difficulty;                           //apply multiplier

//** This part tries to spawn about the same number of HD in each group

//** Can be replaced with whatever you want your spawn determination to be

    if (MLFour <= (TL / 4.0))                       //if MonFour is not too big...

    {

        Temp = (TL / 4.0) / MLFour;                 //figures how many of MonFour it would take to

        SFour = FloatToInt(Temp);                   //make 1/4 of the encounter, set SFour = # to

        TL = TL - (SFour * MLFour);                 //spawn, decrement TL that many levels

    }

    if (MLThree <= (TL / 3.0))                      //if MonThree not too big...

    {

        Temp = (TL / 3.0) / MLThree;                //how many MonThree are 1/3 of TL

        SThree = FloatToInt(Temp);                  //set SThree

        TL = TL - (SThree * MLThree);               //decrement TL

    }

    if (MLTwo <= (TL / 2.0))                        //if MonTwo not too big...

    {

        Temp = (TL / 2.0) / MLTwo;                  //how many MonTwo are 1/2 of TL

        STwo = FloatToInt(Temp);                    //set STwo

        TL = TL - (STwo * MLTwo);                   //decrement TL

    }

    Temp = TL / MLOne;                              //divide TL by the HD of MonOne

    SOne = FloatToInt(Temp);                        //set SOne

//** This part spawns the creatures, as determined above

    SpawnCreatures(0, MonOne, "SP_01", SOne, 4);    //Spawn SOne MonOne at WP Tag in the middle

    SpawnCreatures(1, MonTwo, "SP", STwo, 4);       //Spawn MonTwo

    SpawnCreatures(2, MonThree, "SP", SThree, 4);   //Spawn MonThree

    SpawnCreatures(3, MonFour, "SP", SFour, 4);     //Spawn MonFour

}

void SpawnCreatures (int Mode, string ResRef, string SpawnWP, int Count, int NumWP)

{

    if (Mode == 0)                                  //Mode 0 spawns the whole group

    {                                               //at the given waypoint.  NumWP

        int curCnt = 0;                             //NumWP is not used in this mode

        object WP = GetWaypointByTag(SpawnWP);

        location Loc = GetLocation(WP);

        for (curCnt = 0; curCnt < Count; curCnt++)

        {

            CreateObject(OBJECT_TYPE_CREATURE, ResRef, Loc);

        }

    }

    if (Mode == 1)                                  //Mode 1 spreads the group as

    {                                               //evenly as possible over NumWP

        int Spread = 0;                             //waypoints. They need to be named

        int curCnt = 0;                             //XXX_01 thru XXX_0(NumWP)

        string WPT;

        object WP;

        location Loc;

        for (curCnt = 0; curCnt < Count; curCnt++)

        {

            Spread++;

            if (Spread > NumWP)

            {

                Spread = 1;

            }

            WPT = SpawnWP + "_0" + (IntToString(Spread));

            WP = GetWaypointByTag(WPT);

            Loc = GetLocation(WP);

            CreateObject(OBJECT_TYPE_CREATURE, ResRef, Loc);

        }

    }

    if (Mode == 2)                                  //Mode 2 spreads the group randomly

    {                                               //over NumWP waypoints.

        int Spread;                                 //Each creature has an equal chance

        int curCnt = 0;                             //to go to each WP

        string WPT;                                 //WPs named as in Mode 1

        object WP;

        location Loc;

        for (curCnt = 0; curCnt < Count; curCnt++)

        {

            Spread = (Random(NumWP) + 1);

            WPT = SpawnWP + "_0" + (IntToString(Spread));

            WP = GetWaypointByTag(WPT);

            Loc = GetLocation(WP);

            CreateObject(OBJECT_TYPE_CREATURE, ResRef, Loc);

        }

    }

    if (Mode == 3)                                  //Mode 3 spawns the group at one of

    {                                               //the specified waypoints, determined

        int Spread = (Random(NumWP) + 1);           //randomly

        int curCnt = 0;                             //WPs named as in Mode 1 & 2

        string WPT = SpawnWP + "_0" + (IntToString(Spread));

        object WP = GetWaypointByTag(WPT);

        location Loc = GetLocation(WP);

        for (curCnt = 0; curCnt < Count; curCnt++)

        {

            CreateObject(OBJECT_TYPE_CREATURE, ResRef, Loc);

        }

    }

}



I tried to document it pretty well, but if anyone has any questions, feel free to drop me a message.

What this script allows you to do, is set up a pool of up to four monsters to draw from. You have to specify their CR or HD, whichever way you want to go.

The script runs through and gets the party's level total, multiplies by a difficulty variable, generates a group based on the result.

It tries to spread the HD evenly among the four monster types, ie: if it needs to generate 16 HD worth of monsters, it will make four groups, each with 4HD of monsters. Any remainder (say one of the groups above was a 3HD monster) gets applied to the next lower group...

The creatures are then spawned. SpawnCreatures() can have four modes:
Mode 0 - Spawn all the creatures at a given waypoint
Mode 1 - Spawn the creatures and spread them out as evenly as possible over up to 9 wayppoints, named XXX_01 through XXX_09.
Mode 2 - Similar to Mode 1, but spread the monsters at random
Mode 3 - Similar to Mode 0, but a range of WPs is given as in Mode 1 & 2, the whole group spawns at this waypoint.

Well, I enjoyed making this script, and it is just what I wanted for my encounters to be. If you don't like it, change it to taste, it is pretty well commented. Hope someone finds it useful.

_________________
Ranger Allan
-Lost? Of course I'm not lost! I'm just not sure where we are...

ADDENDUM TO MY EARLIER POST:

Add this line after the SpawnCreature() commands, so that it is the last command in main():

DestroyObject(OBJECT_SELF);

Or it will re-trigger for every entry... Population explosion!