Don't click here unless you want to be banned.

LSL Wiki : LibraryFPPBrain1

HomePage :: PageIndex :: RecentChanges :: RecentlyCommented :: UserSettings :: You are ia360925.us.archive.org

Overview


The Brain script's function is to decide what to do and then tell the other scripts what to do. To start with, the basic bBrain script will do just one thing, pick a random location to move to. Later on we will add more functions.


Functions

Pick a random location within x meters of the pet's current location.
Tell the movement script where to go.
Wait for a link message from the movement script.
Handle being rezzed in a new location

User defined functions


There are two functions that are going to be used more than once, sending a "MOVETO" message and selecting a new location to move to. So we will put these into user defined functions.

move_to
The move_to function is going to take three parameters, target, speed and range, and send the data in a link message as discussed in the link message section.

move_to(vector new_target,float speed,float range)
{
    list command=["MOVETO",new_target,speed,range];
    llMessageLinked( LINK_SET, company_channel, llDumpList2String(command, "-=-"), "" );
}

select_new_location
The select_new_location function is going to take a float as a parameter and return a vector. The float, max_dist, is the maximum distance from the object's current location that the new location should be. The vector it returns is a random location no more than max_dist away from the current position. The new location will be no higher or lower than half of max_dist.
To keep things simple for this example, the max_dist will be used for the x and y axis separately. We will be improving this scripts later on.
You can put what you want in this function. Its function is to return the next location. You could put code here to randomly pick from a set of landmarks. Or find the nearest object called tree and move towards it.

vector select_new_location(float max_dist)
{
  vector current_pos = llGetPos();
  vector offset;
    //Create a vector to hold the offset in.
  offset.x = llFrand(2* max_dist) - max_dist);
    //Set the x value to a number between -max_dist and +max_dist
  offset.y = llFrand(2* max_dist) - max_dist);
    //Set the y value to a number between -max_dist and +max_dist
  offset.z = llFrand(max_dist) - (max_dist/2));
    //Set the z value to a number between -max_dist/2 and +max_dist/2
  return current_pos + offset;
    //Return the new location to move to.  This is the current position plus the offset we just created.
}

States and Events


This simple brain script only has one behaviour, and so we only need one state, the default state. Within the default state, three events are needed. The state_entry to start the pet off, the link_message event to 'listen' for the movement script saying that it has reached the location and the on_rez event to cater for when the pet is rezzed.


state_entry event

The state_entry event occurs when a script first enters a specific state. As this state_entry is in the default state, this is the first code that is triggered when the Brain script is first run. So what do we want to do when the script first runs? We want the brain script to pick a random location to move to and then tell the movement script to go there. With our two user defined functions this is a pretty simple bit of code.
state_entry

{
    vector new_target = select_new_location(patrol_range);
    move_to(new_target,speed,range);
}

Do we want the state_entry event to do anything else? Not at the moment. This will tell the movement script to start the object on the way to the new location. All the brain script has to do is wait for the movement script to tell it when it's there. Which is what the

Link_Message event

does. The linkmessage event is going to be triggered whenever a script is sent to the prim that this script is in. When that message is received this event is going to check to see if its from one of your scripts, by checking the company_channel, and then test to see if its an "ATPOS" message from the movement script. If it is what does the brains need to do? Pick a new random location to move to and tell the movement script.
link_message(integer sender,integer num,string str,key id)
{
    //Is this from a known script
    if(num == company_channel)
    {
        //It is!  Lets unpack the data
         list data = llParseStringKeepNulls( str, ["-=-"], [] );
         string Cmd = llList2String( data, 0 );
    //Is it an ATPOS message?
         if( Cmd == "ATPOS" )
        {
            //It is!  Pick a new location
             vector next_pos = select_new_position(patrol_range);
            //And tell the movement script to go there
             move_to_position(next_pos);
         }
    }
}
Once the pet is in motion again, all the brain script has to do is wait for the next ATPOS message.


on_rez event

One last situation we have to deal with. When the pet is rezzed, we have to stop whatever motion it had when we rezzed it and start it off again.

on_rez(integer param)
{
    vector new_target = select_new_location(patrol_range);
    move_to(new_target,speed,range);
}

Putting it all together


integer company_channel = 250;
float range = 0.5;
float speed = 1;
float patrol_range = 3;

vector select_new_location(float max_dist)
{
  vector current_pos = llGetPos();
  vector offset;
    //Create a vector to hold the offset in.
  offset.x = (llFrand(2* max_dist) - max_dist);
    //Set the x value to a number between -max_dist and +max_dist
  offset.y = (llFrand(2* max_dist) - max_dist);
    //Set the y value to a number between -max_dist and +max_dist
  offset.z = (llFrand(max_dist) - (max_dist/2));
    //Set the z value to a number between -max_dist/2 and +max_dist/2 (might need to change, it always seems to want to move down) (DM)
  return current_pos + offset;
    //Return the new location to move to.  This is the current position plus the offset we just created.
}
move_to(vector new_target,float speed,float range)
{
    list command=["MOVETO",new_target,speed,range];
    llMessageLinked( LINK_SET, company_channel, llDumpList2String(command, "-=-"), "" );
}
default
{
    state_entry()
    {
        vector new_target = select_new_location(patrol_range);
        move_to(new_target,speed,range);
    }
    link_message(integer sender,integer num,string str,key id)
    {
       //Is this from a known script
       if(num == company_channel)
        {
          //It is!  Lets unpack the data
          list data = llParseStringKeepNulls( str, ["-=-"], [] );
          string Cmd = llList2String( data, 0 );
          //Is it an ATPOS message?
          if( Cmd == "ATPOS" )
            {
            //It is!  Pick a new location
             vector next_pos = select_new_location(patrol_range);
            //And tell the movement script to go there
             move_to(next_pos, speed, range);
         }
    }
}
on_rez(integer param)
{
    vector new_target = select_new_location(patrol_range);
    move_to(new_target,speed,range);
}
}


Back to the Create a flying pet home page
Back to the Script Library
There is no comment on this page. [Display comments/form]