Deozaan

getTargetFromList Behavior for TGB

Apr 28th, 2009
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.41 KB | None | 0 0
  1. //------------------------------------------------------------------------------
  2. // getTargetFromList Behavior for TGB
  3. // programmed by Deozaan
  4. //
  5. // The purpose of this behavior is to allow any object to easily pick one target
  6. // from a list of many objects based on varying and different criteria.
  7. //
  8. // %this.targetList is a SimSet list of targets to choose from. Make it easy on
  9. //    yourself and use my getTargetList behavior to create this list.
  10. // %this.method is a string and is the method to use to decide which object to
  11. //    target from the list. Details about each method are documented above each
  12. //    method's corresponding function.
  13. // %this.useRange is a boolean and determines whether or not the object needs
  14. //    to be within a certain range of the owner of this behavior.
  15. // %this.range is a float and is the range (radius) the object needs to be
  16. //    within to be considered in the choosing of a target.
  17. // %this.target is an object, and stores the ID of the current target. This
  18. //    value is later stored in %this.owner.target so that it can be accessed by
  19. //    other behaviors.
  20. //
  21. //------------------------------------------------------------------------------
  22.  
  23. if (!isObject(GetTargetFromListBehavior))
  24. {
  25.    %template = new BehaviorTemplate(GetTargetFromListBehavior);
  26.    
  27.    %template.friendlyName = "Get Target from List";
  28.    %template.behaviorType = "AI";
  29.    %template.description = "Gets a specific target from a list";
  30.    
  31.    %template.addBehaviorField(targetList, "SimSet list to use", Default, %this.owner.targetList);
  32.    %template.addBehaviorField(method, "Method to use to determine target", Enum, "Nearest", "Nearest" NL "Farthest"); // NL "Most Life" NL "Least Life" NL "Fastest" NL "Slowest" NL "Strongest" NL "Weakest");
  33.    %template.addBehaviorField(useRange, "Use Range?", Bool, true);
  34.    %template.addBehaviorField(range, "Range within which to get target", Float, 0.00);
  35. }
  36.  
  37. function GetTargetFromListBehavior::onBehaviorAdd(%this)
  38. {
  39.    %this.owner.enableUpdateCallback();
  40.    // if this behavior was added with code, it won't have targetList or method
  41.    // defined yet, so we deactivate it until we get some values passed in
  42.    if (!%this.targetList || !%this.method)
  43.       { %this.activate = false; }
  44.    if (%this.range <= 0) // if we have a 0 or negative range, don't use range
  45.       { %this.useRange = false; }
  46. }
  47.  
  48. function GetTargetFromListBehavior::onUpdate(%this)
  49. {
  50.    %this.getTarget();
  51. }
  52.  
  53. function GetTargetFromListBehavior::settings(%this, %list, %method, %useRange,  %range)
  54. //------------------------------------------------------------------------------
  55. // settings() makes it easy to change any of the settings through
  56. // script at any time. It is also very useful for adding behaviors manually,
  57. // which, depending on how you do it, they may not be initialized with settings
  58. // like they would if you enter everything in with the Level Builder.
  59. //------------------------------------------------------------------------------
  60. {
  61.    %this.targetList = %list;
  62.    %this.method = %method;
  63.    %this.useRange = %useRange;
  64.    %this.range = %range;
  65.    if (%this.range <= 0) { %this.useRange = false; }
  66.    %this.activate = true;
  67. }
  68.  
  69. function GetTargetFromListBehavior::getTarget(%this)
  70. //------------------------------------------------------------------------------
  71. // getTarget() is merely a redirector that makes sure we it's supposed to be
  72. // running, and if so, redirects to the appropriate function for getting the
  73. // target according to the designated method.
  74. //------------------------------------------------------------------------------
  75. {
  76.    if (%this.activate == false) // if we don't have settings, don't do anything
  77.       { return; }
  78.      
  79.    switch$(%this.method)
  80.    {
  81.       case "Nearest":
  82.          // targets closest object
  83.          %this.getNearest();
  84.       case "Farthest":
  85.          // targets farthest object
  86.          %this.getFarthest();
  87.       case "Most Life":
  88.          // tagets object with most life
  89.          %this.getMostLife();
  90.       case "Least Life":
  91.          // targets object with least life
  92.          %this.getLeastLife();
  93.       case "Fastest":
  94.          // targets fastest object
  95.          %this.getFastest();
  96.       case "Slowest":
  97.          // targets slowest object
  98.          %this.getSlowest();
  99.       case "Strongest":
  100.          // targets object with most defense
  101.          %this.getStrongest();
  102.       case "Weakest":
  103.          // tagets object with least defense
  104.          %this.getWeakest();
  105.    }
  106.    %this.owner.target = %this.target;
  107. }
  108.  
  109. function GetTargetFromListBehavior::getNearest(%this)
  110. //------------------------------------------------------------------------------
  111. // getNearest() chooses a target based on least distance to the object's owner.
  112. //------------------------------------------------------------------------------
  113. {
  114.    %target = %this.targetList.getObject(0);
  115.    %dist = t2dVectorDistance(%target.position, %this.owner.position);
  116.    %nearest = %dist;
  117.    
  118.    for( %i = 1; %i < %this.targetList.getCount(); %i++ )
  119.    {
  120.       %obj = %this.targetList.getObject(%i);
  121.       %dist = t2dVectorDistance(%obj.position, %this.owner.position);
  122.       if (%this.useRange == true)
  123.       { // if we're using range to validate this
  124.          if (%dist > %this.range) // and if the distance is outside the range
  125.             { continue; } // try the next one in the list
  126.       }
  127.       if (%dist < %nearest)
  128.       {
  129.          %nearest = %dist;
  130.          %target = %this.targetList.getObject(%i);
  131.       }
  132.    }
  133.    %this.target = %target;
  134. }
  135.  
  136. function GetTargetFromListBehavior::getFarthest(%this)
  137. //------------------------------------------------------------------------------
  138. // getFarthest() chooses a target based on most distance from the object's owner
  139. //------------------------------------------------------------------------------
  140. {
  141.    %target = "None";
  142.    %farthest = 0;
  143.    
  144.    for( %i = 0; %i < %this.targetList.getCount(); %i++ )
  145.    {
  146.       %obj = %this.targetList.getObject(%i);
  147.       %dist = t2dVectorDistance(%obj.position, %this.owner.position);
  148.       if (%this.useRange == true)
  149.       { // if we're using range to validate this
  150.          if (%dist > %this.range) // and if the distance is outside the range
  151.             { continue; }// try the next one in the list
  152.       }
  153.       if (%dist > %farthest)
  154.       {
  155.          %farthest = %dist;
  156.          %target = %this.targetList.getObject(%i);
  157.       }
  158.    }
  159.    %this.target = %target;
  160. }
  161.  
  162. function GetTargetFromListBehavior::getMostLife(%this)
  163. //------------------------------------------------------------------------------
  164. // getMostLife()
  165. //------------------------------------------------------------------------------
  166. {
  167.    echo("getMostLife is not yet defined, getting Nearest instead");
  168.    %this.getNearest();
  169. }
  170.  
  171. function GetTargetFromListBehavior::getLeastLife(%this)
  172. //------------------------------------------------------------------------------
  173. // getLeastLife()
  174. //------------------------------------------------------------------------------
  175. {
  176.    echo("getLeastLife is not yet defined, getting Nearest instead");
  177.    %this.getNearest();
  178. }
  179.  
  180. function GetTargetFromListBehavior::getFastest(%this)
  181. //------------------------------------------------------------------------------
  182. // getFastest()
  183. //------------------------------------------------------------------------------
  184. {
  185.    echo("getFastest is not yet defined, getting Nearest instead");
  186.    %this.getNearest();
  187. }
  188.  
  189. function GetTargetFromListBehavior::getSlowest(%this)
  190. //------------------------------------------------------------------------------
  191. // getSlowest
  192. //------------------------------------------------------------------------------
  193. {
  194.    echo("getSlowest is not yet defined, getting Nearest instead");
  195.    %this.getNearest();
  196. }
  197.  
  198. function GetTargetFromListBehavior::getStrongest(%this)
  199. //------------------------------------------------------------------------------
  200. // getStrongest
  201. //------------------------------------------------------------------------------
  202. {
  203.    echo("getStrongest is not yet defined, getting Nearest instead");
  204.    %this.getNearest();
  205. }
  206.  
  207. function GetTargetFromListBehavior::getWeakest(%this)
  208. //------------------------------------------------------------------------------
  209. // getWeakest
  210. //------------------------------------------------------------------------------
  211. {
  212.    echo("getWeakest is not yet defined, getting Nearest instead");
  213.    %this.getNearest();
  214. }
Advertisement
Add Comment
Please, Sign In to add comment