View Javadoc

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