View Javadoc

1   package Steerings;
2   
3   import SteeringStuff.SteeringManager;
4   import SteeringStuff.RefBoolean;
5   import SteeringProperties.SteeringProperties;
6   import SteeringProperties.TargetApproachingProperties;
7   import SteeringProperties.Target_packet;
8   import cz.cuni.amis.pogamut.base3d.worldview.object.Location;
9   import javax.vecmath.Vector3d;
10  import cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004Bot;
11  import SteeringStuff.ISteering;
12  import java.util.ArrayList;
13  import javax.vecmath.Tuple3d;
14  
15  
16  /**
17   * Using this steering the bot can approach one or more targets.
18   *
19   * @author Marki
20   */
21  public class TargetApproachingSteer implements ISteering {
22  
23      /** This steering needs botself. */
24      private UT2004Bot botself;
25  
26      private ArrayList<Target_packet> targets = new ArrayList<Target_packet>();
27  
28      private static int NEARLY_THERE_DISTANCE = 150;
29  
30      /**
31       * @param bot Instance of the steered bot.
32       */
33      public TargetApproachingSteer(UT2004Bot bot) {
34          botself = bot;
35      }
36  
37      /** When called, the bot starts steering, when possible, he get's nearer the target location. */
38      public Vector3d run(Vector3d scaledActualVelocity, RefBoolean wantsToGoFaster, RefBoolean wantsToStop, Location focus)
39      {
40          // Supposed velocity in the next tick of logic, after applying various steering forces to the bot.
41          Vector3d nextVelocity = new Vector3d(0,0,0);
42  
43          for(Target_packet tp : targets) {
44  
45              /** ISteering properties: target location - bot approaches this location. */
46              Location targetLocation = tp.getTargetLocation();
47  
48              // A vector from the bot to the target location.
49              Vector3d vectorToTarget = new Vector3d(targetLocation.x - botself.getLocation().x, targetLocation.y - botself.getLocation().y, 0);
50  
51              double distFromTarget = vectorToTarget.length();
52  
53              /** ISteering properties: target gravity - a parameter meaning how attracted the bot is to his target location. */
54              int attractiveForce = tp.getAttractiveForce(distFromTarget);
55              
56              if (distFromTarget < NEARLY_THERE_DISTANCE) {
57                  wantsToStop.setValue(true);
58                  //if (SteeringManager.DEBUG) System.out.println("We reached the target");
59              } else {
60                  vectorToTarget.normalize();
61                  vectorToTarget.scale(attractiveForce);
62                  nextVelocity.add((Tuple3d) vectorToTarget);
63              }
64          }
65          wantsToGoFaster.setValue(true);
66          return nextVelocity;
67      }
68  
69      public void setProperties(SteeringProperties newProperties) {
70          ArrayList<Target_packet> al = ((TargetApproachingProperties)newProperties).getTargets();
71          targets.clear();
72          for(Target_packet tp : al) {
73              targets.add(new Target_packet(tp.getTargetLocation(), tp.getForce_packet()));
74          }
75      }
76  
77      public TargetApproachingProperties getProperties() {
78          TargetApproachingProperties properties = new TargetApproachingProperties();
79          properties.setTargets(targets);
80          return properties;
81      }
82  
83  }