Author Sticky: Post Working Scripts Here
Morz Posted: Thursday, 11 July 03:01PM
More D&D-like Wandering Monster Checks:

Here's an example of a script I wrote to sumulate wantering monster checks like you'd do in tabletop D&D modules. It's formatted as a user defined script called from a heartbeat script (I didn't include the heartbeat script here, but that's just a counter using a local variable to adjust how many rounds go by before making the encounter roll).

It rolls a d6 and on a 1 spawns a certain number of creatues 10 meters in a random direction from the first PC on the PC list, depending on a roll and the corresponding switch/case statement.

Good thing is if the creature would spawn in an invalid location, NWN automatically moves the spawnpoint to the nearest valid location. This means, usually, the encounter spawns out of the immediate visual range of the party, and might also spawn in some unconnected nearby room for the party to encounter later (or it to 'wander' into the party).

Make sure to uncomment the random animation commands on the onspawn script for these creatures and set thier perception range to long. That way they'll wander about and find the party easier.

With a script like this running, there won't be any 'safe spots' for a party which has cleared all the encounters in an area, without you having to fill the place with continuously encounter triggers.

NWScript:
void main()

{

  switch (GetUserDefinedEventNumber())

    {

    case 100:

         if(d6()==1)

                {

                int iDirection=d4();

                float fX;

                float fY;

                switch(iDirection)

                    {

                    case 1:

                        fX=0.0;

                        fY=10.0;

                        break;

                    case 2:

                        fX=10.0;

                        fY=0.0;

                        break;

                    case 3:

                        fX=0.0;

                        fY=-10.0;

                        break;

                    case 4:

                        fX=-10.0;

                        fY=0.0;

                        break;

                    }

                vector vStart = GetPosition(GetFirstPC());

                vector vOffset = Vector(fX,fY);

                vector vEndvector=vStart+vOffset;

                location lEnd=Location(GetArea(GetFirstPC()),vEndvector,GetFacing(GetFirstPC()));

                int iEncRoll = d6();

                int iEncNumber;

                int iEncCount = 1;

                switch(iEncRoll)

                    {

                   case 1:

                        iEncNumber=d4();

                        while (iEncCount<=iEncNumber)

                        {

                        CreateObject(OBJECT_TYPE_CREATURE,"creature_resref_here",lEnd);

                        iEncCount++;

                        };

                       break;

                   case 2:

                        iEncNumber=d4();

                        while (iEncCount<=iEncNumber)

                        {

                        CreateObject(OBJECT_TYPE_CREATURE,"creature_resref_here",lEnd);

                        iEncCount++;

                        };

                       break;

                   case 3:

                        iEncNumber=d2();

                        while (iEncCount<=iEncNumber)

                        {

                        CreateObject(OBJECT_TYPE_CREATURE,"creature_resref_here",lEnd);

                        iEncCount++;

                        };

                       break;

                   case 4:

                        iEncNumber=d2();

                        while (iEncCount<=iEncNumber)

                        {

                        CreateObject(OBJECT_TYPE_CREATURE,"creature_resref_here",lEnd);

                        iEncCount++;

                        };

                       break;

                      }

               }

          break;

   }

}