DMH-Online

Dungeon Masters Helper · Est. 2005

Npcs Scripts

NWN Script: ambient_animations.htm

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;
            }
        }
    }
}
⌂ All Scripts Next → 👁 Category List

All Npcs Scripts

📄 ambient_animations 📄 barmaid 📄 barmaid002 📄 barmaid003 📄 barmaid003a 📄 barmaid004 📄 bitch_slap 📄 blacksmith 📄 blacksmith_001 📄 blacksmith_002 📄 blacksmith_003 📄 bounty_hunter 📄 check 📄 check_pc_item 📄 check_pc_item_002 📄 cheer_pcs 📄 cook 📄 daily_activity 📄 day_merchant 📄 determine_armed 📄 drunk 📄 equip_dequip 📄 equip_dequip_002 📄 escort 📄 flee 📄 flee_001 📄 force_pc_sit 📄 gate_guard 📄 get_nearest 📄 guard_dog_002 📄 guard_patrol 📄 healer 📄 impersonation 📄 item_guardian 📄 item_guardian_002 📄 jail_cell 📄 janitor 📄 janitor_002 📄 living_statues 📄 misc_activity 📄 monolouge 📄 multi_converse 📄 night_watch 📄 npc_brawls 📄 npc_pick_pocket 📄 npc_speak_onperception 📄 npc_start_conv 📄 npc_start_conv_002 📄 npc_start_conv_003 📄 npc_start_conv_004 📄 npc_start_conv_005 📄 open_door 📄 polymorph 📄 prostitute 📄 protect_items 📄 protect_items_002 📄 random_speak 📄 random_speak_002 📄 random_speak_003 📄 random_speak_004 📄 random_speak_005 📄 random_walk2 📄 reinforcements 📄 reinforcements_002 📄 remember_conv 📄 rowdy 📄 sheath_weapons 📄 sit_and_talk 📄 sit_and_talk_002 📄 sit_in_nearest_chair 📄 smart_npc 📄 smart_sitting 📄 streetfight 📄 tag 📄 tag_001a 📄 teleport_away 📄 teleport_away_002 📄 teleport_away_003 📄 town_crier 📄 trivia_bot 📄 walk_after 📄 woodcutter 📄 worker 📄 worker_002 📄 workstation 📄 wrestling_game