Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //-----------------------------------------------------------------------------
- // getTargetList Behavior for TGB
- // programmed by Deozaan
- //
- // The purpose of this behavior is to allow any object to easily choose a
- // target to be used in other behaviors.
- //
- // This behavior is a little unique in that the information you enter in using
- // the level builder is only a scratch on the surface of what this can do. To
- // get the most out of this behavior, you will be making calls to it from script
- // to add to the list, update it, remove from the list, or clear it.
- //
- // %objType, a string, is what this behavior uses to determine what type of
- // information to be looking for. The Level Builder will only let you get your
- // targetList from Name, ID, Class, or t2d Object Class. However, using script
- // calls, you can enter any custom criteria.
- //
- // %typeName, a string, is the specific information to look for from
- // the type while getting the list. For example, if you wanted to look for all
- // objects with the class name "enemy" %objType would be "Class" and %nameType
- // would be "enemy"
- //
- // %autoUpdate, an integer, is the number of milliseconds between updates to the
- // list. Useful if targets are continuously being spawned and need to be on the
- // list.
- //
- // For more information see the comments in each function.
- //
- // Also, it should be noted that it stores the object IDs in the list. If you
- // want to see a prettified named version, use echoList() to print the names
- // of all the objects in the list to the console.
- //-----------------------------------------------------------------------------
- if (!isObject(GetTargetListBehavior))
- {
- %template = new BehaviorTemplate(GetTargetListBehavior);
- %template.friendlyName = "Get Target List";
- %template.behaviorType = "AI";
- %template.description = "Gets a list of targets for the object and other behaviors to use";
- %template.addBehaviorField(objType, "Select Target by:", Enum, "Name", "Name" NL "Class" NL "Object" NL "ID");
- %template.addBehaviorField(typeName, "Name/Class/Object/ID to make a list of", Default, "");
- %template.addBehaviorField(autoUpdate, "Update this list how often? (milliseconds, use 0 to disable autoupdate)", Int, 0);
- }
- function GetTargetListBehavior::onBehaviorAdd(%this)
- //------------------------------------------------------------------------------
- // onBehaviorAdd() is the default code to run when the behavior is first added
- // to the object. Basically it's good for initializing the behavior.
- //------------------------------------------------------------------------------
- {
- %this.targetList = new SimSet();
- %this.owner.targetList = new SimSet();
- %this.arrayCount = 0;
- if (%this.objType && %this.typeName) // if we have values, get things going
- { %this.addWatch(%this.objType, %this.typeName); }
- }
- function GetTargetListBehavior::update(%this, %frequency)
- //------------------------------------------------------------------------------
- // update() will update the list and schedule autoUpdates if appropriate.
- //------------------------------------------------------------------------------
- {
- %this.owner.targetList.clear();
- %this.owner.targetList = %this.getList(); // assign the list to owner
- if (%frequency > 0) // set up a schedule
- {
- if (isEventPending(%this.updateSchedule)) // if we've got a schedule
- {
- cancel(%this.updateSchedule); // cancel it so we can...
- } // make a new one
- %this.updateSchedule = %this.schedule(%frequency, "update", %frequency);
- }
- %this.autoUpdate = %frequency;
- }
- function GetTargetListBehavior::addWatch(%this, %objType, %typeName)
- //------------------------------------------------------------------------------
- // addWatch() first checks to make sure we're not already watching for this type
- // and if not will add %objType and %typeName to the watch list.
- //------------------------------------------------------------------------------
- {
- if (%this.getIndex(%objType, %typeName) < 0)
- { // if we don't have a match, add it to the list
- %index = %this.arrayCount;
- %this.objTypeArray[%index] = %objType;
- %this.typeNameArray[%index] = %typeName;
- %this.arrayCount++; // don't forget to increase count!
- }
- %this.update(%this.autoUpdate);
- }
- function GetTargetListBehavior::remWatch(%this, %objType, %typeName)
- //------------------------------------------------------------------------------
- // remWatch() removes this type from the watch list (if it exists) and then
- // reorganizes the array to keep it clean. Actually, since TGB doesn't have real
- // arrays what I'm doing is moving down one index slot any items that are after
- // the one we want to get rid of.
- //------------------------------------------------------------------------------
- {
- %index = %this.getIndex(%objType, %typeName);
- %count = %this.arrayCount;
- if (%index >= 0) // if it's in the list and
- {
- if (%index == %count-1) // if it's the last one in the list
- {
- %this.arrayCount--; // take one away from the list
- } else if (%index < %count-1) {
- // what we're about to do is take the next one in the list and write it
- // over the current one, then move to the next one and repeat the process
- for (%i = %index;%i<%count;%i++)
- {
- %this.objTypeArray[%i] = %this.objTypeArray[%i+1];
- %this.typeNameArray[%i] = %this.typeNameArray[%i+1];
- }
- %this.arrayCount--; // we just overwrote one, so reduce count by one
- }
- }
- }
- function GetTargetListBehavior::clearWatch(%this)
- //------------------------------------------------------------------------------
- // clearWatch() removes all items from the watch list. Actually, since TGB
- // doesn't have real arrays there is no easy way to start afresh, so all this
- // does is set the array count to 0 so it won't iterate through the list.
- //------------------------------------------------------------------------------
- {
- %this.arrayCount = 0;
- }
- function GetTargetListBehavior::getIndex(%this, %objType, %typeName)
- //------------------------------------------------------------------------------
- // getIndex() will search through the array and return the index of the match.
- // Returns -1 if no match.
- //------------------------------------------------------------------------------
- {
- %match = -1; // assume no match
- if (%this.arrayCount <= 0) // if no count in the array, no match exists
- { return %match; }
- for(%i=0;%i<= %this.arrayCount;%i++)
- {
- if (%objType $= %this.objTypeArray[%i] && %typeName $= %this.typeNameArray[%i])
- {
- %match = %i; // we found a match at index %i
- break; // break out of the for loop
- }
- }
- return %match; // return the index for a match, or -1 for no match
- }
- function GetTargetListBehavior::getList(%this)
- //------------------------------------------------------------------------------
- // getList() makes sure we have a watchlist and then iterates through each item
- // in that list and calls the appropriate function to add to the targetList.
- //------------------------------------------------------------------------------
- {
- if (%this.arrayCount > 0) // if we have a watchlist, do this stuff
- // we're in!
- {
- for (%i = 0; %i < %this.arrayCount; %i++)
- {
- %objType = %this.objTypeArray[%i];
- %typeName = %this.typeNameArray[%i];
- switch$(%objType)
- {
- case "Name":
- // Set the name as the target
- // This could potentially have more than one object since you
- // can have multiple objects with the same name.
- %this.getNameList(%typeName);
- case "ID":
- // This will provide only one result
- %this.getIDList(%typeName);
- case "Class":
- // Gets the list of all objects with the class of typeName
- %this.getClassList(%typeName);
- case "Object":
- // Gets the list of all t2d objects of typeName
- %this.getObjectList(%typeName);
- default:
- // Gets the list of a custom type and value
- %this.getCustomList(%objType, %typeName);
- }
- }
- return %this.targetList; // this is to make it easy to assign a list
- } else { // if we have no watch list, clear the list and return it
- %this.targetList.clear();
- return %this.targetList;
- }
- }
- function GetTargetListBehavior::echoList(%this)
- {
- %this.targetList.listObjects();
- }
- function GetTargetListBehavior::getIDList(%this, %ID)
- //------------------------------------------------------------------------------
- // getIDList() grabs the object with the ID defined in %ID
- //------------------------------------------------------------------------------
- {
- %this.targetList.add(%ID.getID());
- }
- function GetTargetListBehavior::getNameList(%this, %name)
- //------------------------------------------------------------------------------
- // getNameList() grabs a list of all objects with the name defined in %name
- //------------------------------------------------------------------------------
- {
- %scenegraph = %this.owner.getSceneGraph();
- %count = %scenegraph.getSceneObjectCount();
- for( %i = 0; %i < %count; %i++ )
- {
- %sceneObject = %scenegraph.getSceneObject( %i );
- if( %sceneObject.getName() !$= %name )
- continue;
- %this.targetList.add(%sceneObject.getID());
- }
- %count = $managedDatablockSet.getCount();
- for( %i = %this.targetList.getCount(); %i < (%this.targetList.getCount() + %count); %i++ )
- {
- %object = $managedDatablockSet.getObject( %i );
- if( %object.getName() !$= %name )
- continue;
- %this.targetList.add(%object.getID());
- }
- }
- function GetTargetListBehavior::getClassList(%this, %className)
- //------------------------------------------------------------------------------
- // getClassList() grabs a list of all objects of the class defined in %className
- //------------------------------------------------------------------------------
- {
- %scenegraph = %this.owner.getSceneGraph();
- %count = %scenegraph.getSceneObjectCount();
- for( %i = 0; %i < %count; %i++ )
- {
- %sceneObject = %scenegraph.getSceneObject( %i );
- if( %sceneObject.Class !$= %className )
- continue;
- %this.targetList.add(%sceneObject.getID());
- }
- %count = $managedDatablockSet.getCount();
- for( %i = %this.targetList.getCount(); %i < (%this.targetList.getCount() + %count); %i++ )
- {
- %object = $managedDatablockSet.getObject( %i );
- if( %object.Class !$= %className )
- continue;
- %this.targetList.add(%object.getID());
- }
- }
- function GetTargetListBehavior::getObjectList(%this, %object)
- //------------------------------------------------------------------------------
- // getObjectList() grabs a list of all t2d objects of the class defined in
- // %object
- //------------------------------------------------------------------------------
- {
- %scenegraph = %this.owner.getSceneGraph();
- %count = %scenegraph.getSceneObjectCount();
- for( %i = 0; %i < %count; %i++ )
- {
- %sceneObject = %scenegraph.getSceneObject( %i );
- if( !%sceneObject.isMemberOfClass( %object ) )
- continue;
- %this.targetList.add(%sceneObject.getID());
- }
- %count = $managedDatablockSet.getCount();
- for( %i = 0; %i < %count; %i++ )
- {
- %object = $managedDatablockSet.getObject( %i );
- if( !%object.isMemberOfClass( %object ) )
- continue;
- %this.targetList.add(%object.getID());
- }
- }
- function GetTargetListBehavior::getCustomList(%this, %type, %value)
- //------------------------------------------------------------------------------
- // getClassList() grabs a list of all objects of the class defined in %className
- //------------------------------------------------------------------------------
- {
- %scenegraph = %this.owner.getSceneGraph();
- %count = %scenegraph.getSceneObjectCount();
- for( %i = 0; %i < %count; %i++ )
- {
- %sceneObject = %scenegraph.getSceneObject( %i );
- %sceneObjTest = "if(" @ %sceneObject @ "." @ %type @ " $= " @ %value @ ") " @ %this @ ".targetList.add(" @ %sceneObject @ ".getID());";
- // if(%sceneObject.%type $= %value) %this.targetList.add(%sceneObject.getID());
- eval(%sceneObjTest);
- }
- %count = $managedDatablockSet.getCount();
- for( %i = %this.targetList.getCount(); %i < (%this.targetList.getCount() + %count); %i++ )
- {
- %object = $managedDatablockSet.getObject( %i );
- %dataBlockTest = "if(" @ %object @ "." @ %type @ " !$= " @ %value @ ") " %this @ ".targetList.add(" @ %object @ ".getID());";
- //if(%object.%type $= %value) %this.targetList.add(%object.getID());
- eval(%dataBlockTest);
- }
- }
- function GetTargetListBehavior::getSomeList(%this)
- //------------------------------------------------------------------------------
- // getSomeList() grabs a list of all objects of the class defined in %className
- //------------------------------------------------------------------------------
- {
- if (%this.arrayCount > 0) // if we have a watchlist, do this stuff
- // we're in!
- {
- %scenegraph = %this.owner.getSceneGraph();
- %countSO = %scenegraph.getSceneObjectCount();
- %countDB = $managedDatablockSet.getCount();
- for (%i = 0; %i < %this.arrayCount; %i++)
- {
- %objType = %this.objTypeArray[%i];
- %typeName = %this.typeNameArray[%i];
- for( %j = 0; %j < %countSO; %j++ )
- {
- %sceneObject = %scenegraph.getSceneObject( %j );
- // This next part is a little complex and might be confusing.
- // Basically what is happening is that since we are checking many
- // different types of object values, there are multiple methods we
- // have to use to skip over objects that don't meet the parameters.
- // First we check for t2d object using %obj.isMemberOfClass(), then
- // we check for name with %obj.getName(), last we check to make sure
- // we are checking for anything except ID, then similarly compare
- // %obj.%objType with %typeName. If no match, it skips.
- // if any of these match it will add the object to the list.
- if (%objType $= "Object") // if we're searching by t2d Object type
- {
- if( !%sceneObject.isMemberOfClass( %typeName ) ) // and no match
- continue;
- } else if (%objType $= "Name") { // if we're searching by Name
- if (!%sceneObject.getName() !$= %typeName) // and no match
- continue;
- } else if (%objType !$= "ID") { // or if we're searching by anything else other than ID
- %sceneObjTest = "if(" @ %sceneObject @ "." @ %type @ " $= " @ %value @ ") " @ %this @ ".targetList.add(" @ %sceneObject @ ".getID());";
- // if(%sceneObject.%type $= %value) %this.targetList.add(%sceneObject.getID());
- eval(%sceneObjTest);
- continue;
- }
- // if we got this far, we have a match so add it to the list
- %this.targetList.add(%sceneObject.getID());
- }
- for( %j = %this.targetList.getCount(); %j < (%this.targetList.getCount() + %countDB); %j++ )
- {
- %object = $managedDatablockSet.getObject( %j );
- // This next part is a little complex and might be confusing.
- // Basically what is happening is that since we are checking many
- // different types of object values, there are multiple methods we
- // have to use to skip over objects that don't meet the parameters.
- // First we check for t2d object using %obj.isMemberOfClass(), then
- // we check for name with %obj.getName(), last we check to make sure
- // we are checking for anything except ID, then similarly compare
- // %obj.%objType with %typeName. If no match, it skips.
- // if any of these match it will add the object to the list.
- if (%objType $= "Object") // if we're searching by t2d Object type
- {
- if( !%object.isMemberOfClass( %typeName ) ) // and no match
- continue;
- } else if (%objType $= "Name") { // if we're searching by Name
- if (!%object.getName() !$= %typeName) // and no match
- continue;
- } else if (%objType !$= "ID") { // or if we're searching by anything else other than ID
- %dataBlockTest = "if( " @ %object @ "." @ %objType @ " !$= " @ %typeName @ " )";
- eval(%dataBlockTest);
- //above eval line asks: if( %object.%objType !$= %typeName )
- continue;
- }
- // if we got this far, we have a match so add it to the list
- %this.targetList.add(%object.getID());
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment