Author Sticky: Post Working Scripts Here
Edret Posted: Wednesday, 03 July 09:32PM
Script Name: Standard NPC Behavior v1.0

What it does: Provides a modular approach to applying standard behavior to your NPC's. Have this script called by your NPC's heartbeat event, apply the appropriate waypoint, and your NPC will act accordingly. Distinguishes between day and night behaviors. The beauty of this script is that you can use it on all but your most complex NPC's to do a wide variety of behaviors. One script for a number of different actions.

Notes: Future versions will add more behaviors such as worshiping, closing objects, etc. The three actions implemented here, however, will be plenty to make convincingly real NPC's.
Although extensively tested, this is a complex script and may have a couple bugs (although it should work fine in most standard situations). Then again, so does NWN so no one's perfect. If you find any bugs, reply to this message and I'll get right no it.

Here is the script:

NWScript:
//:: Standard NPC Behavior v1.0

//:: sc_std_bhvr

/*

    Intended to be called by the heartbeat event.

    This script will have the character perform

    certain actions depending on what waypoints

    are present.  Furthermore, this script

    distinguishes between night and not night.

    To have an NPC perform a particular behavior

    action, create a waypoint with the proper tag

    to indicate the location this action will

    occur.  The tag of the waypoint determines

    what action is to be performed.  The tag

    format is as follows:

    NPC_Tag + "_WP" + "_D" or "_N" + Behavior

    where NPC_Tag is the tag of the NPC and

    Behavior is the particular behavior action

    the NPC will perform.  Below is a list of

    the behavior actions supported by this script

    as of this version.

    NOTE:  Sleep effects may not work properly

    during beginning of dawn and dusk.

    Behavior actions listed by priority:

   Behavior Action     -   What to put in tag

    Sleep              -    _SLEEP

    Random Walk        -    _RANDWALK

    Walk Waypoints*    -    _WAYPTWALK

    * Does not denote location but rather that NPC

      has a post or patrol.

*/

//:: Created By:  Stephen Cannon

//:: Created On:  June 1, 2002

#include "NW_I0_GENERIC"

void main()

{

    string sNPCTag = GetTag(OBJECT_SELF);

    object oWaypoint;

    effect sleepEffect = EffectSleep();

    // Ensure not in conversation or fighting.

    if(!IsInConversation(OBJECT_SELF) && !GetIsFighting(OBJECT_SELF))

    {

        // Day actions

        if(!GetIsNight())

        {

            // Sleep at location designated by waypoint until night.  Location

            // is 3 meter circumference around waypoint.

            if((oWaypoint = GetWaypointByTag(sNPCTag + "_WP" + "_D" + "_SLEEP")) != OBJECT_INVALID)

            {

                ClearAllActions();

                if(GetDistanceToObject(oWaypoint) > -1.0f && GetDistanceToObject(oWaypoint) < 3.0f)

                {

                    ApplyEffectToObject(DURATION_TYPE_TEMPORARY, sleepEffect, OBJECT_SELF, 60.0f);

                    SetLocalInt(OBJECT_SELF, "NATURAL_SLEEPING", 1);

                }

                else

                {

                    ActionMoveToObject(oWaypoint);

                }

                return;

            }

            // Random Walk within 5 meters of tag.

            if((oWaypoint = GetWaypointByTag(sNPCTag + "_WP" + "_D" + "_RANDWALK")) != OBJECT_INVALID)

            {

                if(GetDistanceToObject(oWaypoint) > -1.0f && GetDistanceToObject(oWaypoint) < 5.0f)

                {

                    ActionRandomWalk();

                }

                else

                {

                    ClearAllActions();

                    ActionMoveToObject(oWaypoint);

                }

                return;

            }

            // Standard walk waypoints or post action.

            if((oWaypoint = GetWaypointByTag(sNPCTag + "_WP+" + "_D" + "_WAYPTWALK")) != OBJECT_INVALID)

            {

                ClearAllActions();

                WalkWayPoints();

                return;

            }

        }

        // Night actions

        else

        {

            // Sleep at location designated by waypoint until dawn.  Location is

            // 3 meter circumference around waypoint.

            if((oWaypoint = GetWaypointByTag(sNPCTag + "_WP" + "_N" + "_SLEEP")) != OBJECT_INVALID)

            {

                ClearAllActions();

                if(GetDistanceToObject(oWaypoint) > -1.0f && GetDistanceToObject(oWaypoint) < 3.0f)

                {

                    ApplyEffectToObject(DURATION_TYPE_TEMPORARY, sleepEffect, OBJECT_SELF, 60.0f);

                    SetLocalInt(OBJECT_SELF, "NATURAL_SLEEPING", 1);

                }

                else

                {

                    ActionMoveToObject(oWaypoint);

                }

                return;

            }

            // Random Walk within 5 meters of tag.

            if((oWaypoint = GetWaypointByTag(sNPCTag + "_WP" + "_N" + "_RANDWALK")) != OBJECT_INVALID)

            {

                if(GetDistanceToObject(oWaypoint) > -1.0f && GetDistanceToObject(oWaypoint) < 5.0f)

                {

                    ActionRandomWalk();

                }

                else

                {

                    ClearAllActions();

                    ActionMoveToObject(oWaypoint);

                }

                return;

            }

            // Standard walk waypoints or post action.

            if((oWaypoint = GetWaypointByTag(sNPCTag + "_WP+" + "_N" + "_WAYPTWALK")) != OBJECT_INVALID)

            {

                ClearAllActions();

                WalkWayPoints();

                return;

            }

        }

    }

}