Deozaan

getTargetList Behavior for TGB

Apr 28th, 2009
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 17.12 KB | None | 0 0
  1. //-----------------------------------------------------------------------------
  2. // getTargetList Behavior for TGB
  3. // programmed by Deozaan
  4. //
  5. // The purpose of this behavior is to allow any object to easily choose a
  6. // target to be used in other behaviors.
  7. //
  8. // This behavior is a little unique in that the information you enter in using
  9. // the level builder is only a scratch on the surface of what this can do. To
  10. // get the most out of this behavior, you will be making calls to it from script
  11. // to add to the list, update it, remove from the list, or clear it.
  12. //
  13. // %objType, a string, is what this behavior uses to determine what type of
  14. // information to be looking for. The Level Builder will only let you get your
  15. // targetList from Name, ID, Class, or t2d Object Class. However, using script
  16. // calls, you can enter any custom criteria.
  17. //
  18. // %typeName, a string, is the specific information to look for from
  19. // the type while getting the list. For example, if you wanted to look for all
  20. // objects with the class name "enemy" %objType would be "Class" and %nameType
  21. // would be "enemy"
  22. //
  23. // %autoUpdate, an integer, is the number of milliseconds between updates to the
  24. // list. Useful if targets are continuously being spawned and need to be on the
  25. // list.
  26. //
  27. // For more information see the comments in each function.
  28. //
  29. // Also, it should be noted that it stores the object IDs in the list. If you
  30. // want to see a prettified named version, use echoList() to print the names
  31. // of all the objects in the list to the console.
  32. //-----------------------------------------------------------------------------
  33.  
  34.  
  35. if (!isObject(GetTargetListBehavior))
  36. {
  37.    %template = new BehaviorTemplate(GetTargetListBehavior);
  38.    
  39.    %template.friendlyName = "Get Target List";
  40.    %template.behaviorType = "AI";
  41.    %template.description = "Gets a list of targets for the object and other behaviors to use";
  42.    
  43.    %template.addBehaviorField(objType, "Select Target by:", Enum, "Name", "Name" NL "Class" NL "Object" NL "ID");
  44.    %template.addBehaviorField(typeName, "Name/Class/Object/ID to make a list of", Default, "");
  45.    %template.addBehaviorField(autoUpdate, "Update this list how often? (milliseconds, use 0 to disable autoupdate)", Int, 0);
  46. }
  47.  
  48. function GetTargetListBehavior::onBehaviorAdd(%this)
  49. //------------------------------------------------------------------------------
  50. // onBehaviorAdd() is the default code to run when the behavior is first added
  51. // to the object. Basically it's good for initializing the behavior.
  52. //------------------------------------------------------------------------------
  53. {
  54.    %this.targetList = new SimSet();
  55.    %this.owner.targetList = new SimSet();
  56.    %this.arrayCount = 0;
  57.    if (%this.objType && %this.typeName) // if we have values, get things going
  58.       { %this.addWatch(%this.objType, %this.typeName); }
  59. }
  60.  
  61. function GetTargetListBehavior::update(%this, %frequency)
  62. //------------------------------------------------------------------------------
  63. // update() will update the list and schedule autoUpdates if appropriate.
  64. //------------------------------------------------------------------------------
  65. {
  66.    %this.owner.targetList.clear();
  67.    %this.owner.targetList = %this.getList(); // assign the list to owner
  68.    if (%frequency > 0) // set up a schedule
  69.    {
  70.       if (isEventPending(%this.updateSchedule)) // if we've got a schedule
  71.       {
  72.          cancel(%this.updateSchedule); // cancel it so we can...
  73.       } // make a new one
  74.       %this.updateSchedule = %this.schedule(%frequency, "update", %frequency);
  75.    }
  76.    %this.autoUpdate = %frequency;
  77. }
  78.  
  79. function GetTargetListBehavior::addWatch(%this, %objType, %typeName)
  80. //------------------------------------------------------------------------------
  81. // addWatch() first checks to make sure we're not already watching for this type
  82. // and if not will add %objType and %typeName to the watch list.
  83. //------------------------------------------------------------------------------
  84. {  
  85.    if (%this.getIndex(%objType, %typeName) < 0)
  86.    { // if we don't have a match, add it to the list
  87.       %index = %this.arrayCount;
  88.       %this.objTypeArray[%index] = %objType;
  89.       %this.typeNameArray[%index] = %typeName;
  90.       %this.arrayCount++; // don't forget to increase count!
  91.    }
  92.    %this.update(%this.autoUpdate);
  93. }
  94.  
  95. function GetTargetListBehavior::remWatch(%this, %objType, %typeName)
  96. //------------------------------------------------------------------------------
  97. // remWatch() removes this type from the watch list (if it exists) and then
  98. // reorganizes the array to keep it clean. Actually, since TGB doesn't have real
  99. // arrays what I'm doing is moving down one index slot any items that are after
  100. // the one we want to get rid of.
  101. //------------------------------------------------------------------------------
  102. {
  103.    %index = %this.getIndex(%objType, %typeName);
  104.    %count = %this.arrayCount;
  105.    if (%index >= 0) // if it's in the list and
  106.    {
  107.       if (%index == %count-1) // if it's the last one in the list
  108.       {
  109.          %this.arrayCount--; // take one away from the list
  110.       } else if (%index < %count-1) {
  111.          // what we're about to do is take the next one in the list and write it
  112.          // over the current one, then move to the next one and repeat the process
  113.          for (%i = %index;%i<%count;%i++)
  114.          {
  115.             %this.objTypeArray[%i] = %this.objTypeArray[%i+1];
  116.             %this.typeNameArray[%i] = %this.typeNameArray[%i+1];
  117.          }
  118.          %this.arrayCount--; // we just overwrote one, so reduce count by one
  119.       }
  120.    }
  121. }
  122.  
  123. function GetTargetListBehavior::clearWatch(%this)
  124. //------------------------------------------------------------------------------
  125. // clearWatch() removes all items from the watch list. Actually, since TGB
  126. // doesn't have real arrays there is no easy way to start afresh, so all this
  127. // does is set the array count to 0 so it won't iterate through the list.
  128. //------------------------------------------------------------------------------
  129. {
  130.    %this.arrayCount = 0;
  131. }
  132.  
  133. function GetTargetListBehavior::getIndex(%this, %objType, %typeName)
  134. //------------------------------------------------------------------------------
  135. // getIndex() will search through the array and return the index of the match.
  136. // Returns -1 if no match.
  137. //------------------------------------------------------------------------------
  138. {
  139.    %match = -1; // assume no match
  140.    if (%this.arrayCount <= 0) // if no count in the array, no match exists
  141.       { return %match; }
  142.    for(%i=0;%i<= %this.arrayCount;%i++)
  143.    {
  144.       if (%objType $= %this.objTypeArray[%i] && %typeName $= %this.typeNameArray[%i])
  145.       {
  146.          %match = %i; // we found a match at index %i
  147.          break; // break out of the for loop
  148.       }
  149.    }
  150.    return %match; // return the index for a match, or -1 for no match
  151. }
  152.  
  153. function GetTargetListBehavior::getList(%this)
  154. //------------------------------------------------------------------------------
  155. // getList() makes sure we have a watchlist and then iterates through each item
  156. // in that list and calls the appropriate function to add to the targetList.
  157. //------------------------------------------------------------------------------
  158. {
  159.    if (%this.arrayCount > 0) // if we have a watchlist, do this stuff
  160.    // we're in!
  161.    {
  162.       for (%i = 0; %i < %this.arrayCount; %i++)
  163.       {
  164.          %objType = %this.objTypeArray[%i];
  165.          %typeName = %this.typeNameArray[%i];
  166.          switch$(%objType)
  167.          {
  168.             case "Name":
  169.                // Set the name as the target
  170.                // This could potentially have more than one object since you
  171.                // can have multiple objects with the same name.
  172.                %this.getNameList(%typeName);
  173.             case "ID":
  174.                // This will provide only one result
  175.                %this.getIDList(%typeName);
  176.             case "Class":
  177.                // Gets the list of all objects with the class of typeName
  178.                %this.getClassList(%typeName);
  179.             case "Object":
  180.                // Gets the list of all t2d objects of typeName
  181.                %this.getObjectList(%typeName);
  182.             default:
  183.                // Gets the list of a custom type and value
  184.                %this.getCustomList(%objType, %typeName);
  185.          }
  186.       }
  187.       return %this.targetList; // this is to make it easy to assign a list
  188.    } else { // if we have no watch list, clear the list and return it
  189.       %this.targetList.clear();
  190.       return %this.targetList;
  191.    }
  192. }
  193.  
  194. function GetTargetListBehavior::echoList(%this)
  195. {
  196.    %this.targetList.listObjects();
  197. }
  198.  
  199. function GetTargetListBehavior::getIDList(%this, %ID)
  200. //------------------------------------------------------------------------------
  201. // getIDList() grabs the object with the ID defined in %ID
  202. //------------------------------------------------------------------------------
  203. {
  204.    %this.targetList.add(%ID.getID());
  205. }
  206.  
  207. function GetTargetListBehavior::getNameList(%this, %name)
  208. //------------------------------------------------------------------------------
  209. // getNameList() grabs a list of all objects with the name defined in %name
  210. //------------------------------------------------------------------------------
  211. {
  212.    %scenegraph = %this.owner.getSceneGraph();
  213.    %count = %scenegraph.getSceneObjectCount();
  214.    for( %i = 0; %i < %count; %i++ )
  215.    {
  216.       %sceneObject = %scenegraph.getSceneObject( %i );
  217.       if( %sceneObject.getName() !$= %name )
  218.          continue;
  219.                
  220.       %this.targetList.add(%sceneObject.getID());
  221.    }
  222.    
  223.    %count = $managedDatablockSet.getCount();
  224.    for( %i = %this.targetList.getCount(); %i < (%this.targetList.getCount() + %count); %i++ )
  225.    {
  226.       %object = $managedDatablockSet.getObject( %i );
  227.       if( %object.getName() !$= %name )
  228.          continue;
  229.                
  230.       %this.targetList.add(%object.getID());
  231.    }  
  232. }
  233.  
  234. function GetTargetListBehavior::getClassList(%this, %className)
  235. //------------------------------------------------------------------------------
  236. // getClassList() grabs a list of all objects of the class defined in %className
  237. //------------------------------------------------------------------------------
  238. {
  239.    %scenegraph = %this.owner.getSceneGraph();
  240.    %count = %scenegraph.getSceneObjectCount();
  241.    for( %i = 0; %i < %count; %i++ )
  242.    {
  243.       %sceneObject = %scenegraph.getSceneObject( %i );
  244.       if( %sceneObject.Class !$= %className )
  245.          continue;
  246.                
  247.       %this.targetList.add(%sceneObject.getID());
  248.    }
  249.    
  250.    %count = $managedDatablockSet.getCount();
  251.    for( %i = %this.targetList.getCount(); %i < (%this.targetList.getCount() + %count); %i++ )
  252.    {
  253.       %object = $managedDatablockSet.getObject( %i );
  254.       if( %object.Class !$= %className )
  255.          continue;
  256.                
  257.       %this.targetList.add(%object.getID());
  258.    }
  259. }
  260.  
  261. function GetTargetListBehavior::getObjectList(%this, %object)
  262. //------------------------------------------------------------------------------
  263. // getObjectList() grabs a list of all t2d objects of the class defined in
  264. // %object
  265. //------------------------------------------------------------------------------
  266. {
  267.    %scenegraph = %this.owner.getSceneGraph();
  268.    %count = %scenegraph.getSceneObjectCount();
  269.    for( %i = 0; %i < %count; %i++ )
  270.    {
  271.       %sceneObject = %scenegraph.getSceneObject( %i );
  272.       if( !%sceneObject.isMemberOfClass( %object ) )
  273.          continue;
  274.                
  275.       %this.targetList.add(%sceneObject.getID());
  276.    }
  277.    
  278.    %count = $managedDatablockSet.getCount();
  279.    for( %i = 0; %i < %count; %i++ )
  280.    {
  281.       %object = $managedDatablockSet.getObject( %i );
  282.       if( !%object.isMemberOfClass( %object ) )
  283.          continue;
  284.      
  285.       %this.targetList.add(%object.getID());
  286.    }
  287. }
  288.  
  289. function GetTargetListBehavior::getCustomList(%this, %type, %value)
  290. //------------------------------------------------------------------------------
  291. // getClassList() grabs a list of all objects of the class defined in %className
  292. //------------------------------------------------------------------------------
  293. {
  294.    %scenegraph = %this.owner.getSceneGraph();
  295.    %count = %scenegraph.getSceneObjectCount();
  296.    for( %i = 0; %i < %count; %i++ )
  297.    {
  298.       %sceneObject = %scenegraph.getSceneObject( %i );
  299.       %sceneObjTest = "if(" @ %sceneObject @ "." @ %type @ " $= " @ %value @ ") " @ %this @ ".targetList.add(" @ %sceneObject @ ".getID());";
  300.       // if(%sceneObject.%type $= %value) %this.targetList.add(%sceneObject.getID());
  301.       eval(%sceneObjTest);
  302.    }
  303.    
  304.    %count = $managedDatablockSet.getCount();
  305.    for( %i = %this.targetList.getCount(); %i < (%this.targetList.getCount() + %count); %i++ )
  306.    {
  307.       %object = $managedDatablockSet.getObject( %i );
  308.       %dataBlockTest = "if(" @ %object @ "." @ %type @ " !$= " @ %value @ ") " %this @ ".targetList.add(" @ %object @ ".getID());";
  309.       //if(%object.%type $= %value) %this.targetList.add(%object.getID());
  310.       eval(%dataBlockTest);
  311.    }
  312. }
  313.  
  314. function GetTargetListBehavior::getSomeList(%this)
  315. //------------------------------------------------------------------------------
  316. // getSomeList() grabs a list of all objects of the class defined in %className
  317. //------------------------------------------------------------------------------
  318. {
  319.    if (%this.arrayCount > 0) // if we have a watchlist, do this stuff
  320.    // we're in!
  321.    {
  322.       %scenegraph = %this.owner.getSceneGraph();
  323.       %countSO = %scenegraph.getSceneObjectCount();
  324.       %countDB = $managedDatablockSet.getCount();
  325.       for (%i = 0; %i < %this.arrayCount; %i++)
  326.       {
  327.          %objType = %this.objTypeArray[%i];
  328.          %typeName = %this.typeNameArray[%i];
  329.  
  330.          for( %j = 0; %j < %countSO; %j++ )
  331.          {
  332.             %sceneObject = %scenegraph.getSceneObject( %j );
  333.             // This next part is a little complex and might be confusing.
  334.             // Basically what is happening is that since we are checking many
  335.             // different types of object values, there are multiple methods we
  336.             // have to use to skip over objects that don't meet the parameters.
  337.             // First we check for t2d object using %obj.isMemberOfClass(), then
  338.             // we check for name with %obj.getName(), last we check to make sure
  339.             // we are checking for anything except ID, then similarly compare
  340.             // %obj.%objType with %typeName. If no match, it skips.
  341.             // if any of these match it will add the object to the list.
  342.             if (%objType $= "Object") // if we're searching by t2d Object type
  343.             {
  344.                if( !%sceneObject.isMemberOfClass( %typeName ) ) // and no match
  345.                   continue;
  346.             } else if (%objType $= "Name") { // if we're searching by Name
  347.                if (!%sceneObject.getName() !$= %typeName) // and no match
  348.                   continue;
  349.             } else if (%objType !$= "ID") { // or if we're searching by anything else other than ID
  350.                %sceneObjTest = "if(" @ %sceneObject @ "." @ %type @ " $= " @ %value @ ") " @ %this @ ".targetList.add(" @ %sceneObject @ ".getID());";
  351.                // if(%sceneObject.%type $= %value) %this.targetList.add(%sceneObject.getID());
  352.                eval(%sceneObjTest);
  353.                continue;
  354.             }
  355.             // if we got this far, we have a match so add it to the list
  356.             %this.targetList.add(%sceneObject.getID());
  357.          }
  358.    
  359.          for( %j = %this.targetList.getCount(); %j < (%this.targetList.getCount() + %countDB); %j++ )
  360.          {
  361.             %object = $managedDatablockSet.getObject( %j );
  362.             // This next part is a little complex and might be confusing.
  363.             // Basically what is happening is that since we are checking many
  364.             // different types of object values, there are multiple methods we
  365.             // have to use to skip over objects that don't meet the parameters.
  366.             // First we check for t2d object using %obj.isMemberOfClass(), then
  367.             // we check for name with %obj.getName(), last we check to make sure
  368.             // we are checking for anything except ID, then similarly compare
  369.             // %obj.%objType with %typeName. If no match, it skips.
  370.             // if any of these match it will add the object to the list.
  371.             if (%objType $= "Object") // if we're searching by t2d Object type
  372.             {
  373.                if( !%object.isMemberOfClass( %typeName ) ) // and no match
  374.                   continue;
  375.             } else if (%objType $= "Name") { // if we're searching by Name
  376.                if (!%object.getName() !$= %typeName) // and no match
  377.                   continue;
  378.             } else if (%objType !$= "ID") { // or if we're searching by anything else other than ID
  379.                %dataBlockTest = "if( " @ %object @ "." @ %objType @ " !$= " @ %typeName @ " )";
  380.                eval(%dataBlockTest);
  381.                //above eval line asks: if( %object.%objType !$= %typeName )
  382.                   continue;
  383.             }
  384.             // if we got this far, we have a match so add it to the list              
  385.             %this.targetList.add(%object.getID());
  386.          }
  387.       }
  388.    }
  389. }
Advertisement
Add Comment
Please, Sign In to add comment