Author Sticky: Post Working Scripts Here
weecc Posted: Thursday, 04 July 03:48AM
Name: Yet Another Wandering Script
What it does: YAWS takes a bunch of NPCs and makes them wander randomly between some predefined waypoints.
How to use: Create some NPCs and give them all the same "NPC1" tag. In the script's unmodified state, create 8 waypoints, naming them "way_1", "way_2", ... "way_8". Put the script in the area's OnHeartbeat event.
Notes: You can change the number of waypoints you want them to wander among, see the comments for details on how to do this. You can also combine this script with triggered areas to create some pretty complex flocking behaviour. All comments and feedback -> weecc@roots.com.my

Here's the script:

NWScript:
//***********************************

//      Random walk script

//      (c) weecc 2002

//***********************************

//This script goes into the area's OnHeartbeat event

//You need to create a bunch of NPCs, all bearing the same "NPC1" tag

//as well as a number of waypoints whose tags are in the format

//of "way_1", "way_2", etc. Make sure that the number of waypoints that you

//use corresponds with the number in the random function under the

//wander() subroutine. You can keep adding NPCs to this script as long as they

//all have the same "NPC1" tag. I tried this with about 20 NPCs, and they

//seemed to behave properly.

//All comments -> weecc@roots.com.my

void wander (int count)

{

    //This script randomly chooses one out of eight waypoints and assigns

    //it as a destination for a NPC to walk to

    int s;

    for (s=0; s<=count; ++s)

        {

        object oNPCw                    = GetObjectByTag("NPC1", s);

        //Change the number inside the random function to reflect the number

        //of waypoints that you are using. For example, if I had only five

        //waypoints, it would read Random (5)

        int i                           = Random(8);

        string si                       = IntToString(i);

        object destinationlocation      = GetObjectByTag("way_" + si);

        //The random walk subroutine

        AssignCommand(oNPCw, ActionMoveToObject(destinationlocation, FALSE));

        }

}

void main()

{

    int NPCcount = 0;

    int i;

    object oNPC1 = GetObjectByTag("NPC1");

    //Get the count of NPCs with the Tag NPC1 and store it in NPCcount

    {

    for (i=0; GetIsObjectValid(GetObjectByTag("NPC1",i))==TRUE; ++ i)

        NPCcount = NPCcount+1;

    }

    //Now that we've got the count of NPCs bearing the "NPC1" tag, we're

    //going to pass this value to the wander() subroutine, which will go

    //through all NPC1 tags and assigns a "walk-to" location for each

    wander(NPCcount);

}



NB: There is no scripting for the NPC itself. Everything is controlled by the heartbeat event. Just plonk down the NPC and it'll start to wander.