View Javadoc

1   package SocialSteeringsBeta;
2   
3   import SteeringStuff.ISteering;
4   import SteeringStuff.RefBoolean;
5   import SteeringStuff.SteeringManager;
6   import SteeringStuff.SteeringTools;
7   import SteeringStuff.SteeringType;
8   import cz.cuni.amis.pogamut.base3d.worldview.object.Location;
9   import cz.cuni.amis.pogamut.ut2004.agent.module.sensomotoric.Raycasting;
10  import cz.cuni.amis.pogamut.ut2004.bot.command.AdvancedLocomotion;
11  import cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004Bot;
12  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.Configuration;
13  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.Move;
14  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.SetWalk;
15  import javax.vecmath.Vector3d;
16  
17  /**
18   *
19   * @author Petr
20   */
21  public class SocialSteeringManager extends SteeringManager {
22  
23      protected IAnimationEngine animationEngine;
24      private int lastWS = 0;
25  
26      public SocialSteeringManager(UT2004Bot bot, Raycasting raycasting, AdvancedLocomotion locomotion, IAnimationEngine engine, double multiplier) {
27          super(bot, raycasting, locomotion, multiplier);
28          animationEngine = engine;
29      }
30  
31      public SocialSteeringManager(UT2004Bot bot, Raycasting raycasting, AdvancedLocomotion locomotion, IAnimationEngine engine) {
32          this(bot, raycasting, locomotion, engine, 1);
33  
34      }
35  
36      /**
37       * DOCASNY komentar v CZ Pridana funkcionalita by mela byt schopna <br/>
38       * a)zastavit pohyb pokud bude animationEngine prehravat neprerusitelnou
39       * animaci<br/> b)pouzivat 3 rychlosti pohybu misto puvodnich 2<br/> c)treti
40       * rychlost pohybu nechava postavu natocenou do focus celou dobu pohybu,
41       * tedy je vhodne pouzit "animaci slapani zeli" <br/>
42       *
43       * @param nextVelocity
44       * @param everyoneWantsToGoFaster
45       * @param focusLocation
46       */
47      @Override
48      public void moveTheBot(Vector3d nextVelocity, boolean everyoneWantsToGoFaster, Location focusLocation) {
49          
50          double precision;
51          if (nextVelocity instanceof SteeringResult && nextVelocity.length() > 0.001) { //also indicates social steering
52  
53              precision = ((SteeringResult) nextVelocity).getAccurancyMultiplier();
54             
55          } else if(nextVelocity.length() > 0.001){ //basic steerings
56              if (animationEngine.canBeInterupt(botself)) {
57                  //<editor-fold defaultstate="collapsed" desc="debug">
58                  if (SOC_STEER_LOG.DEBUG) {
59                      SOC_STEER_LOG.AddLogLine("basic steering manager", SOC_STEER_LOG.KSync + botself.getName());
60                  }
61                  //</editor-fold>
62                   animationEngine.playAnimation(2, botself, true,0);
63                 // botself.getAct().act(new SetWalk().setWalk(true).setWalkAnim("walk_loop"));
64                  super.moveTheBot(nextVelocity, everyoneWantsToGoFaster, focusLocation);
65                  return;
66              }else
67              {
68                  //<editor-fold defaultstate="collapsed" desc="debug">
69                  if (SOC_STEER_LOG.DEBUG) {
70                      SOC_STEER_LOG.AddLogLine("basic steering manager, wait for anim", SOC_STEER_LOG.KSync + botself.getName());
71                  }
72                  //</editor-fold>
73                  stopMovement(focusLocation);
74                  return;
75              }
76          }else //
77          {
78              precision = 0;
79          }
80          
81          if (SteeringManager.DEBUG) System.out.println("Precision: "+precision);
82  
83          //determinates how we will move(run = 3, walk = 2, pace = 1)
84          int walkType;
85  
86          double nextVelocityLength = nextVelocity.length() * multiplier; //The multiplier enables to enlarge or decrease the velocity. E.g. to make the bot to run.
87  
88          if (precision == 0) { //bot stops
89              walkType = 0;
90          } else if (precision < 3) { //bot 
91              walkType = 1;
92          } else if (nextVelocityLength < WALK_VELOCITY_LENGTH * 100)
93          {
94              walkType = 2;
95          } else //running
96          {
97              walkType = 3;
98          }
99  
100 
101 
102         //<editor-fold defaultstate="collapsed" desc="wants to go faster">
103 //        if (nextVelocityLength < 0.8*WALK_VELOCITY_LENGTH && everyoneWantsToGoFaster) {
104 //            if (SteeringManager.DEBUG) System.out.println("we enlarge the velocity");
105 //            nextVelocityLength = 0.8 * WALK_VELOCITY_LENGTH;
106 //        }
107 
108         //</editor-fold>
109 
110         double nextVelMult = nextVelocityLength / WALK_VELOCITY_LENGTH;
111         nextVelocityLength = nextVelMult * WALK_VELOCITY_LENGTH;
112         nextVelocity.normalize();
113         nextVelocity.scale(nextVelocityLength);
114 
115 
116         myNextVelocity = new Vector3d(nextVelocity.x, nextVelocity.y, nextVelocity.z);
117         botself.getAct().act(new Configuration().setSpeedMultiplier(nextVelocityLength / WALK_VELOCITY_LENGTH).setAutoTrace(true).setDrawTraceLines(drawRaycasting));
118 
119         if (SteeringManager.DEBUG) System.out.println("animationEngine.canBeInterupt "+animationEngine.canBeInterupt(botself));
120 
121         if (animationEngine.canBeInterupt(botself)) {
122             switch (walkType) {
123                 case 0:
124                     //<editor-fold defaultstate="collapsed" desc="debug">
125                     if (SOC_STEER_LOG.DEBUG) {
126                         SOC_STEER_LOG.AddLogLine("correct place, idling", SOC_STEER_LOG.KSync + botself.getName());
127                     }
128                     //</editor-fold>
129                     stopMovement( focusLocation);
130                     if (SteeringManager.DEBUG) System.out.println("stopMovement1");
131                     break;
132 
133                 case 1:
134                    
135                     Location tLoc = new Location(botself.getLocation().x + nextVelocity.x, botself.getLocation().y + nextVelocity.y, botself.getLocation().z);
136                     
137                     Move m = new Move();
138                     // todo: pomoci sinove vety spocitat uhel ve kterem se koukame a nastavit straffle left resp straffle right pro pohyb k cily
139                     int direction;
140                     double angle = SteeringTools.getAngle(botself.getLocation(), botself.getRotation(), tLoc);
141                     if(angle < Math.PI / 4)
142                     {
143                         direction = 0;
144                     }else if (angle > ( Math.PI / 4) * 3 )
145                     {
146                         direction = 2;
147                     }else 
148                     {
149                         if(SteeringTools.pointIsLeftFromTheVector(focusLocation.sub(botself.getLocation()).asVector3d(), tLoc.asVector3d()))
150                         {
151                             direction = 3;
152                         }else
153                         {
154                             direction = 1;
155                         }
156                     }
157                     //0.. rovne 1..prava 2..couva 3..leva
158                     m.setFirstLocation(tLoc);
159                     m.setFocusLocation(focusLocation);
160                     Configuration c = new Configuration();
161                     
162                     c.setSpeedMultiplier(0.4);
163                     botself.getAct().act(c);
164                     
165                     botself.getAct().act(m);
166                     
167                     if (lastWS == walkType) {
168                         break;
169                     }
170                     String s = "";
171                     if (myNextVelocity.length() > 20) {
172                         s = animationEngine.playAnimation(1, botself, true, direction);
173                         if (SteeringManager.DEBUG) System.out.println("Play animation strafe.");
174                     } else {
175                         //<editor-fold defaultstate="collapsed" desc="debug">
176                         if (SOC_STEER_LOG.DEBUG) {
177                             SOC_STEER_LOG.AddLogLine("slowMove not anim", SOC_STEER_LOG.KSync + botself.getName());
178                         }
179                         //</editor-fold>
180                     }//<editor-fold defaultstate="collapsed" desc="debug">
181                     if (SOC_STEER_LOG.DEBUG) {
182                         SOC_STEER_LOG.AddLogLine("slowMove " + angle + " angle " + s, SOC_STEER_LOG.KSync + botself.getName());
183                     }
184                     //</editor-fold>
185 
186                     //botself.getAct().act(new SetWalk().setWalk(true).setWalkAnim(s));
187 
188                     break;
189                 
190                 case 2:
191                      
192                     tLoc = new Location(botself.getLocation().x + nextVelocity.x, botself.getLocation().y + nextVelocity.y, botself.getLocation().z);
193 
194                     Move mm = new Move();
195                     mm.setFirstLocation(tLoc);
196                     mm.setFocusLocation(tLoc);
197 
198                     Configuration cc = new Configuration();
199                     
200                     cc.setSpeedMultiplier(0.8);
201                     botself.getAct().act(cc);
202                     
203                     botself.getAct().act(mm);
204                     if (lastWS == walkType) {
205                         break;
206                     }
207                  
208                     String s2 = animationEngine.playAnimation(2, botself, true,0);
209                     //<editor-fold defaultstate="collapsed" desc="debug">
210                     if (SOC_STEER_LOG.DEBUG) {
211                         SOC_STEER_LOG.AddLogLine("Walk" + s2, SOC_STEER_LOG.KSync + botself.getName());
212                     }
213                     //</editor-fold>
214                     botself.getAct().act(new SetWalk().setWalk(true).setWalkAnim(s2));
215                     break;
216                 case 3: break;
217             }
218         } else {
219             //animation that can not be interrupted is played on botself, so we do not move
220             //<editor-fold defaultstate="collapsed" desc="debug">
221             if (SOC_STEER_LOG.DEBUG) {
222                 SOC_STEER_LOG.AddLogLine("Wait for animation unineruptible animation", SOC_STEER_LOG.KSync + botself.getName());
223             }
224             //</editor-fold>
225          
226              stopMovement(focusLocation);
227             if (SteeringManager.DEBUG) System.out.println("stopMovement2");
228             
229         }
230         //this.myNextVelocity.x = 0;
231         //this.myNextVelocity.y = 0;
232         //this.myNextVelocity.z = 0;
233         lastWS = walkType; 
234     }
235 
236     private void stopMovement(Location focusLocation) {
237         
238         myNextVelocity = new Vector3d(0, 0, 0);
239         locomotion.setSpeed(0.0);//just for sure...
240         locomotion.stopMovement();
241         if (!focusLocation.equals(new Location(0,0,0))) {
242             locomotion.turnTo(focusLocation);
243         }
244         if (SteeringManager.DEBUG) System.out.println(botself.getName()+" We turn to "+focusLocation);
245         if(animationEngine.canBeInterupt(botself)){
246             animationEngine.playAnimation("ambi_stand_normal01", botself, false);
247         }
248     }
249 
250     @Override
251     protected Location setFocusSpecific(SteeringType steeringType, boolean wantsToStop, Location newFocus, Location focusLoc) {
252         if (steeringType == SteeringType.TRIANGLE) {
253             return newFocus;
254         }
255         return super.setFocusSpecific(steeringType, wantsToStop, newFocus, focusLoc);
256     }
257 
258     @Override
259     protected Vector3d setVelocitySpecific(ISteering steering, RefBoolean wantsToGoFaster, RefBoolean wantsToStop, RefLocation newFocus) {
260         if (steering instanceof ISocialSteering) {
261             return ((ISocialSteering) steering).runSocial(myNextVelocity, wantsToGoFaster, wantsToStop, newFocus);
262         } else {
263             return super.setVelocitySpecific(steering, wantsToGoFaster, wantsToStop, newFocus);
264         }
265     }
266 }