Author Sticky: Post Working Scripts Here
SDxEair Posted: Thursday, 18 July 10:16AM
Several Useful Functions, I will Add to this file as I go, it currently includes:

object GetFirstPCInArea(object oAreaTest)

object GetNextPCInArea(object oAreaTest)

location OffSetLocation(location lPlayer)

int GetAreaPCCount(object oArea)

object GetRandomPCInArea(object oArea)

All the info is in the script!

NWScript:
//Include File by Eair

//Email: indy37@earthlink.net

//7/18/02

//Note: currently working on a Random Encounter Generator that gets as close to

//the tables in the DMG as possible, it works at the moment but only impelmented

//Table 4-15: 1st-Level Dungeon Encounters

//If anyone creates monsters for the Tables that arn't in NWN(theres a lot)

//Pls Tell me.



//Gets the First PC in oArea

object GetFirstPCInArea(object oAreaTest)

{

    object oPCTestValid = GetFirstPC();

    while(GetArea(oPCTestValid)!=oAreaTest&&GetIsObjectValid(oPCTestValid))

        oPCTestValid = GetNextPC();

    return(oPCTestValid);

}

//Gets the Next PC in oArea

object GetNextPCInArea(object oAreaTest)

{

    object oPCTestValid = GetNextPC();

    while(GetArea(oPCTestValid)!=oAreaTest&&GetIsObjectValid(oPCTestValid))

        oPCTestValid = GetNextPC();

    return(oPCTestValid);

}

//OffSet's a location by 10 in one of 8 dirrections(random)

//And Points fFacing towards the original location

location OffSetLocation(location lPlayer)

{

  //Excerpt from...

//:://////////////////////////////////////////////////////

//:: PnP Random Encounters

//:://////////////////////////////////////////////////////

/*

    This Script Generates Old School Random Encounters

    like the ones from a random encounter table in a PnP

    module

*/

//:://////////////////////////////////////////////////////

//:: Created By: LuckDragon

//:://////////////////////////////////////////////////////

  int iDirection=d8();

  float fX;

  float fY;

  float fF;//Added by Eair

  switch(iDirection) {

    case 1:  // North

      fX=0.0; fY=10.0; fF=270.0;

      break;

    case 2:  // South

      fX=0.0; fY=-10.0; fF=90.0;

      break;

    case 3:  // East

      fX=10.0; fY=0.0; fF=180.0;

      break;

    case 4:  // West

      fX=-10.0; fY=0.0; fF=0.0;

      break;

    case 5:  // Northeast

      fX=10.0; fY=10.0; fF=225.0;

      break;

    case 6:  // Southeast

      fX=10.0; fY=-10.0; fF=135.0;

      break;

    case 7:  // Southwest

      fX=-10.0; fY=-10.0; fF=45.0;

      break;

    case 8:  // Northwest

      fX=-10.0; fY=10.0; fF=315.0;

      break;

  }

  object oTemp = GetAreaFromLocation(lPlayer);//Added by Eair

  vector vStart = GetPositionFromLocation(lPlayer);

  vector vOffset = Vector(fX,fY);

  vector vEndvector = vStart+vOffset;

  location lEndLocation = Location(oTemp,vEndvector,fF);//Added by Eair

  return(lEndLocation);

  //End of Excerpt!

}

//Gets the number of PC's in oArea

int GetAreaPCCount(object oArea)

{

    int iPCCount=0;

    object oPCTest = GetFirstPCInArea(oArea);

    while(GetIsObjectValid(oPCTest))

    {

        oPCTest = GetNextPCInArea(oArea);

        iPCCount++;

    }

    return(iPCCount);

}

//Gets a Random PC in oArea

//Will return OBJECT_INVALID if no PC's are in the Area

object GetRandomPCInArea(object oArea)

{

    int iRandomPCNumber = d100();/*This Assumes theres no more than 100 people

    in the Area, and there probly won't be in most servers. if you expect more,

    change d100() to d100(X)-X, it might not be exact... if you want better make

    your own random number generator*/

    while(iRandomPCNumber>GetAreaPCCount(oArea))

        iRandomPCNumber = d100();

    object oReturnPC = GetFirstPCInArea(oArea);

    int iCounter = 0;

    while(iCounter<iRandomPCNumber&&GetIsObjectValid(oReturnPC))

    {

        GetNextPCInArea(oArea);

        if(!GetIsObjectValid(oReturnPC))

            GetFirstPCInArea(oArea);

        iCounter++;

    }

    return(oReturnPC);

}