Public Types | Public Member Functions

BaseBehavior Class Reference

This reference page is linked to from the following overview topics: Character Studio Plug-ins, Plug-in Base Classes, Writing Crowd Behavior Plug-ins.


Search for all occurrences

Detailed Description

All behaviors must inherit from this base class.

#include <bhvr.h>

Inheritance diagram for BaseBehavior:
Inheritance graph
[legend]

List of all members.

Public Types

enum   BEHAVIOR_TYPE { FORCE = 0, CONSTRAINT, ORIENTATION }

Public Member Functions

  BaseBehavior ()
Identification Functions

These functions are used to uniquely define the behavior and to describe its type.

SClass_ID  SuperClassID ()
  Returns the super class id of the behavior.
virtual void  SetName (const MCHAR *newname)
  Sets the name of the behavior.
virtual const MCHAR *  GetName ()
  Gets the name of the behavior.
virtual BEHAVIOR_TYPE  BehaviorType ()
  Returns the behavior's type, either FORCE, CONSTRAINT, or ORIENTATION.
virtual BOOL  IsWeightable ()
  Queries whether or not the behavior may be modified by the behavior weight scalar.
virtual int  CanConvertToMaxScript ()
  Queries whether or not the behavior can convert itself to a script.
Initialization Functions

These functions allow a behavior to initialize itself at different times during a simulation.

They also allow a behavior to keep track of data associated with delegates assigned to it during a simulation.

virtual void  SetUpDelegates (INodeTab &participants)
  Sends the behavior a list of all delegates which will participate in the simulation.
virtual void  BehaviorStarted (TimeValue t, INode *node)
  Notifies the behavior that it should start to act upon a particular delegate.
virtual void  InitAtThisTime (TimeValue t)
  Initializes a behavior at the start of each simulation frame.
virtual void  InitBeforeSim (int FirstFrame, INode *n, int SimStart, int AssignIndex)
  Initializes the behavior at the start of a simulation.
Performance Functions

These functions are called for each delegate assigned to a behavior, each frame during a simulation.

Depending on the value returned from the BehaviorType function, only one of the following three functions must be implemented.

virtual int  Perform (INode *node, TimeValue t, int numsubsamples, BOOL DisplayHelpers, float BhvrWeight, PerformOut &out)
  This is the main function for FORCE behaviors.
virtual int  Constraint (INode *node, TimeValue t, int numsubsamples, BOOL DesGoalExists, BOOL DisplayHelpers, BOOL finalSet, ConstraintInOut &inOut)
  This is the main function for CONSTRAINT behaviors.
virtual int  Orient (const Point3 &vel, INode *node, TimeValue t, Quat &quat)
  This is the main function for ORIENTATION behaviors.
Display Functions

These functions provide an option to display an apparatus along with the behavior at all times - not just during solve.

The behavior can offer an option in its user interface to turn this on and off. Keep in mind this apparatus will be displayed as part of the crowd object, and so may enlarge the damaged rectangle significantly.

virtual int  Display (TimeValue t, ViewExp *vpt)
  Using this function, a behavior can display text or graphics inside the viewports.
virtual void  GetWorldBoundBox (TimeValue t, ViewExp *vpt, Box3 &box)
  Gets the world bounding box of what the behavior draws.

Member Enumeration Documentation


Constructor & Destructor Documentation

BaseBehavior ( ) [inline]
{}

Member Function Documentation

SClass_ID SuperClassID ( ) [inline, virtual]

Returns the super class id of the behavior.

The default implementation should be used, to make certain that the new behavior class is recognized by the Crowd system and appears in the list of available behaviors inside Crowd. Use ClassID to define your own class id specific to your behavior.

Returns:
the behavior super class ID

Reimplemented from ReferenceTarget.

{ return BEHAVIOR_SUPER_CLASS_ID; }
virtual void SetName ( const MCHAR *  newname ) [inline, virtual]

Sets the name of the behavior.

When the user changes the name of the behavior from the Crowd interface, this function is called. It should store the name in the behavior.

Parameters:
[in] newname - the new name of the behavior.
{}
virtual const MCHAR* GetName ( void  ) [inline, virtual]

Gets the name of the behavior.

Usually this name will be stored internally within a paramblock. The name usually starts out being the same as the behavior's class name. The name can then be changed from within the Crowd system, which sets it by calling this function.

Returns:
the behavior name
{return NULL;}
virtual BEHAVIOR_TYPE BehaviorType ( ) [inline, virtual]

Returns the behavior's type, either FORCE, CONSTRAINT, or ORIENTATION.

  • Force: Behaviors of this type force the delegates to move in a particular direction. Examples of force behaviors are Seek and Repel. Force behaviors work by returning a force vector in the direction that the behavior wants the delegate to go in, and in some cases the speed it should be traveling and the goal it is trying to reach.
  • Constraint: Behaviors of this type restrict the position and velocity of a delegate. An example of a constraint behavior is SurfaceFollow. Constraint behaviors set the velocity and sometimes may even change the delegate's position, in order to meet the constraint. The user can have only one active constraint behavior per delegate per frame.
  • Orientation: Behaviors of this type affect only the orientation of the delegates. An example of an orientation behavior is Orientation. These behaviors don't work with forces but instead return an orientation that the delegate should be at, represented by a quaternion. Any active orientation behavior will override the default orientation of the delegate. The velocity determines the default orientation. Like a constraint behavior, the user can have only one active orientation behavior per delegate per frame.
    Returns:
    one of three possible values: force, constraint, or orientation
{return FORCE;}
virtual BOOL IsWeightable ( ) [inline, virtual]

Queries whether or not the behavior may be modified by the behavior weight scalar.

For the user, this weight appears in the assignment dialog for each assignment. Usually FORCE types are weightable, while CONSTRAINT and ORIENTATION behaviors aren't.

Returns:
1 if the behavior is weightable, 0 if not.
                {if (BehaviorType() == BaseBehavior::CONSTRAINT)  return FALSE;
                 if (BehaviorType() == BaseBehavior::ORIENTATION) return FALSE;
                 return TRUE;}
virtual int CanConvertToMaxScript ( ) [inline, virtual]

Queries whether or not the behavior can convert itself to a script.

This function is for future versions of Crowd. It is not used now.

Returns:
true if the behavior can convert itself to maxScript, false if not
{return FALSE;}
virtual void SetUpDelegates ( INodeTab participants ) [inline, virtual]

Sends the behavior a list of all delegates which will participate in the simulation.

At the beginning of a simulation, this function is called for each behavior in the simulation. All active delegates assigned via an active assignment to any behavior or cognitive controller in the crowd system are included in the participants INodeTab. So this function tells the behavior the maximum number of delegates that could possibly perform this behavior during the simulation, and which delegates those are. With this information, the behavior can set up whatever data structures it requires to keep track of parameters on a per delegate basis. At the end of the simulation, this function is again called for each behavior, this time with an empty participants list, in order to allow the behavior to clear its data structures.

Parameters:
[in] participants - a tab of all of the delegates in the simulation. The delegates must be active, and they must be assigned, via an active assignment, to at least one behavior or cognitive controller.
{}; //called at beginning of simulation.
virtual void BehaviorStarted ( TimeValue  t,
INode node 
) [inline, virtual]

Notifies the behavior that it should start to act upon a particular delegate.

This function is called during a simulation, whenever a cognitive controller activates the behavior for a particular delegate. It allows a behavior to keep track of which delegates it is acting upon.

Parameters:
[in] t - the current simulation time.
[in] node - the delegate which started the behavior
{};
virtual void InitAtThisTime ( TimeValue  t ) [inline, virtual]

Initializes a behavior at the start of each simulation frame.

This function is called at the beginning of each frame of the simulation. It allows the behavior to set up or clear out any information that it might need each frame.

Parameters:
[in] t - the current simulation time
{}
virtual void InitBeforeSim ( int  FirstFrame,
INode n,
int  SimStart,
int  AssignIndex 
) [inline, virtual]

Initializes the behavior at the start of a simulation.

This function is called once just before the beginning of the simulation. It is called so that the behavior knows which frame the simulation is actually starting at so it can perform any actions necessary to assure its repeatability. For example, if a behavior knows that the simulation is starting at frame 10 instead of frame 0, it may run it's random number generator 10 times so that the value it generates will be consistent with what it normally computes when the simulation starts from the beginning, as specified by the SimStart parameter.

Parameters:
[in] FirstFrame - the value of the first frame for this particular run of the simulation. This is the same as Start Solve in the crowd solve rollout. It is not in time ticks, but in frames.
[in] n - the delegate
[in] SimStart - the value of the frame when the simulation starts This is the same as Simulation Start in the crowd solve rollout. It is not in time ticks, but in frames.
[in] AssignIndex - the index of the assignment used to apply this behavior to the delegate. If this equals -1, the behavior has been assigned using a cognitive controller. The AssignIndex can be used as a parameter to the IDelegate class's IsAssignmentActive function, which allows you to find out if this assignment is active at a given time. You may need this information to properly initialize a behavior before a simulation.
{}
virtual int Perform ( INode node,
TimeValue  t,
int  numsubsamples,
BOOL  DisplayHelpers,
float  BhvrWeight,
PerformOut out 
) [inline, virtual]

This is the main function for FORCE behaviors.

It is called every frame for each delegate the behavior is acting upon. It is within this function that the behavior may set any combination of the following: the force acting upon the delegate, the delegate's speed, or a goal towards which the delegate should move.

Parameters:
[in] node - the delegate
[in] t - the current simulation time
[in] numsubsamples - how many subsamples are being computed per frame. Currently not used.
[in] DisplayHelpers - whether or not the behavior should display it's visual information. This parameter exists so that the Crowd system has global control over when the behavior information is displayed.
[in] BhvrWeight - A float value, greater than or equal to 0.0, that corresponds to the BehaviorWeight value in the Crowd Assignment dialog. This value should be used to scale the returned force value in the out parameter.
[out] out - A PerformOut structure that is used to pass out information that the behavior calculates. The structure has 4 items that may be set, depending upon whether the behavior sets a force, a speed or a goal.
  • frc: If the behavior sets a force, the frc value should be filled with a vector normalized to the average speed of the delegate it is acting upon. Usually this normalized vector will be scaled also by the BhvrWeight parameter.
  • goal: If the behavior sets a goal, then the goal item should be set with the world space goal that the delegate is trying to reach. It is important to note that in order for the behavior to work with Biped crowds, it needs to set a goal. Even if the behavior doesn't appear to have a clear-cut goal, a goal may still be set for it by placing the goal along the force vector at some reasonable distance. The delegate's average speed works well.
  • speedwt: If the behavior sets a speed, then the speedwt should be set to the behavior's desired speed for the delegate at the current simulation time. The speedwt will be multipled by the delegate's average speed. (so a value of 1.0 is equal to the delegate's average speed).
  • speedAtGoalwt: The speedAtGoalWt is only used by delegates associated with bipeds. If the speedAtGoalWt equals 1.0, the biped will attempt to maintain its current speed. If it is 0.0, the biped will attempt to stop. If it is < 0, the biped will try to slow down. If it is > 1, the biped will try to speed up.
    Note that the behavior doesn't need to be setting a goal in order to set the speedAtGoalwt. This is how the SpeedVary function works. The instantaneous speed is the speed at the current time, while the speedAtGoalwt is the speed that it wants to reach at some time in the future.
Returns:
0 if the behavior isn't active for this particular delegate, or if the behavior, for any reason, wishes to have no effect on the delegate at this time. Otherwise, it returns the union of three #defines, BHVR_SETS_FORCE, BHVR_SETS_GOAL, and BHVR_SETS_SPEED, depending upon whether or not the behavior sets a force, a goal or the speed. For example, a behavior that sets both a force and goal will return BHVR_SETS_FORCE | BHVR_SETS_GOAL. Which of these values are returned determines which items in the PeformOut structure are valid.
                                                               {return FALSE;}
virtual int Constraint ( INode node,
TimeValue  t,
int  numsubsamples,
BOOL  DesGoalExists,
BOOL  DisplayHelpers,
BOOL  finalSet,
ConstraintInOut inOut 
) [inline, virtual]

This is the main function for CONSTRAINT behaviors.

It is called multiple times per frame for each delegate the behavior is acting upon. It is within this function that the behavior may set a modified velocity or modified position for a delegate.

Parameters:
[in] node - the delegate
[in] t - the current simulation time
[in] numsubsamples - how many subsamples are being computed per frame. Currently not used.
[in] DesGoalExists - whether or not a goal currently exists for this delegate. If it exists, it will be passed in via the inOut parameter.
[in] DisplayHelpers - whether or not the behavior should display it's visual information. This parameter exists so that the Crowd system has global control over when the behavior information is displayed.
[in] finalSet - whether or not this is the final time that the constraint will be called during the frame. Currently, each constraint behavior is called two times per frame (this may change in the future) - once after all of the forces have been assembled and just before a possible avoid behavior, and then once again at the end of the frame. It is called multiple times per frame so that the behavior may work correctly with any active avoid behavior. This flag lets the behavior know if it is the last time it will be called during that frame and thus should cache any information that it needs for the next frame calculation. Usually if the behavior isn't doing any sort of per frame caching it should just ignore this flag.
[in,out] &inOut - a ConstraintInOut structure that is used to pass in information that the behavior will modify and then pass out. The structure contains 4 parameters:
  • vel: the newly calculated velocity of the delegate
  • pos: the current position of the delegate
  • speed: the current speed of the delegate
  • desGoal: the current desired goal of the delegate
    The behavior may change one or all of these parameters. Note that the next position that the delegate will be at is nextPos = pos + normalized(vel)*Speed. So changing the current position doesn't actually change where it is, but it just changes the position that is used to calculate it's new position. Usually the behavior will change the velocity parameter, and it will only change the pos parameter if it needs to so that the Speed, and thus the energy of the delegate, doesn't change. Note that the velocity that you return should be normalized.
Returns:
0 if the behavior isn't active for this particular delegate, or if the behavior, for any reason, wishes to have no effect on the delegate at this time. Otherwise, it returns 1.
                                                        {return FALSE;}
virtual int Orient ( const Point3 vel,
INode node,
TimeValue  t,
Quat quat 
) [inline, virtual]

This is the main function for ORIENTATION behaviors.

It is called every frame for each delegate the behavior is acting upon. It is within this function that the behavior may set a quaternion representing the delegate's orientation.

Parameters:
[in] vel - the current velocity of the delegate
[in] node - the delegate
[in] t - the current simulation time
[out] quat - The quaternion specifying the new orientation of the delegate
Returns:
0 if the behavior isn't active for this particular delegate, or if the behavior, for any reason, wishes to have no effect on the delegate at this time. Otherwise, it returns 1.
{return FALSE;}
virtual int Display ( TimeValue  t,
ViewExp vpt 
) [inline, virtual]

Using this function, a behavior can display text or graphics inside the viewports.

This is typically used to display a bounding area or a target. Often the behavior will allow the user to turn on or off this display by providing a checkbox in its interface.

Parameters:
[in] t - the time to display the object.
[in] vpt - an interface pointer that may be used to call functions associated with the viewports
Returns:
1 if something is drawn, otherwise 0.
{return 0;}
virtual void GetWorldBoundBox ( TimeValue  t,
ViewExp vpt,
Box3 box 
) [inline, virtual]

Gets the world bounding box of what the behavior draws.

Parameters:
[in] t - the time at which to compute the bounding box
[in] vpt - an interface pointer that may be used to call functions associated with the viewports
[out] box - the bounding box
{}

BaseBehavior BaseBehavior BaseBehavior BaseBehavior BaseBehavior BaseBehavior BaseBehavior BaseBehavior BaseBehavior BaseBehavior
BaseBehavior BaseBehavior BaseBehavior BaseBehavior BaseBehavior BaseBehavior BaseBehavior BaseBehavior BaseBehavior BaseBehavior