View Javadoc

1   /*
2    * To change this template, choose Tools | Templates
3    * and open the template in the editor.
4    */
5   package cz.cuni.amis.pogamut.usar2004.agent;
6   
7   import com.google.inject.Inject;
8   import cz.cuni.amis.pogamut.base.communication.command.IAct;
9   import cz.cuni.amis.pogamut.base.communication.worldview.IWorldView;
10  import cz.cuni.amis.pogamut.base.communication.worldview.event.IWorldEventListener;
11  import cz.cuni.amis.pogamut.base.component.bus.IComponentBus;
12  import cz.cuni.amis.pogamut.base.utils.guice.AgentScoped;
13  import cz.cuni.amis.pogamut.base.utils.logging.IAgentLogger;
14  import cz.cuni.amis.pogamut.base3d.agent.AbstractAgent3D;
15  import cz.cuni.amis.pogamut.base3d.worldview.IVisionWorldView;
16  import cz.cuni.amis.pogamut.base3d.worldview.object.Location;
17  import cz.cuni.amis.pogamut.base3d.worldview.object.Rotation;
18  import cz.cuni.amis.pogamut.base3d.worldview.object.Velocity;
19  import cz.cuni.amis.pogamut.usar2004.agent.params.USAR2004AgentParameters;
20  import cz.cuni.amis.pogamut.usar2004.communication.messages.usarcommands.GetStartPoses;
21  import cz.cuni.amis.pogamut.usar2004.communication.messages.usarinfomessages.NfoMessage;
22  import cz.cuni.amis.utils.exception.PogamutException;
23  
24  /**
25   *
26   * @author vejmanm
27   */
28  @AgentScoped
29  public class USAR2004Bot<WORLD_VIEW extends IVisionWorldView, ACT extends IAct, CONTROLLER extends IUSAR2004BotController> extends AbstractAgent3D<WORLD_VIEW, ACT> implements IUSAR2004Bot {
30  
31      IUSAR2004BotController controller;
32  
33      boolean bRobotInitializedCalled = false;
34  
35      @Override
36      public Location getLocation() {
37          return null;
38      }
39  
40      @Override
41      public Velocity getVelocity() {
42          return null;
43      }
44  
45      @Override
46      public Rotation getRotation() {
47          return null;
48      }
49  
50      @Override
51      public void respawn() throws PogamutException {
52      }
53      // -----------------------
54      // -=-=-=-=-=-=-=-=-=-=-=-
55      // NFO MESSAGE LISTENER
56      // -=-=-=-=-=-=-=-=-=-=-=-
57      // -----------------------
58      /**
59       * Listener that is hooked to WorldView awaiting first event NfoMessage calling
60       * robotInitialized method upon receiving the event.
61       */
62      private IWorldEventListener<NfoMessage> firstNfoMessageListener =
63              new IWorldEventListener<NfoMessage>() {
64                  @Override
65                  public void notify(NfoMessage event) {
66                      //setState(new BotStateInited("InitedMessage received, calling botInitialized()."));
67                      if (!bRobotInitializedCalled) 
68                      {
69                          if(isStartPoseMessage(event))
70                          {
71                              controller.robotInitialized(event);
72                              /** dereferencing listener */
73                              getWorldView().removeEventListener(NfoMessage.class, this);
74                              USAR2004Bot.this.firstNfoMessageListener = null;
75                              bRobotInitializedCalled = true;                        
76                          }
77                          else
78                          {
79                              getAct().act(new GetStartPoses());
80                              //To be able to obtain name of the map or game mode eventually time limit in the logic, we need to 
81                              //remove this part or create another method that will trigger before robotInitialized() and call it here.
82                          }                            
83                      }
84                      //setState(new BotStateInited("Bot initialized."));
85                  }
86                  /**
87                   * This method is used to recognise if the NFO message is the first with level info or if it bears data about start poses.
88                   */
89                  private boolean isStartPoseMessage(NfoMessage message)
90                  {
91                      if(message.getGameType() == null || message.getGameType().equals(""))
92                      {
93                          return true;
94                      }
95                      return false;
96                  }
97              };
98  
99      @Inject
100     public USAR2004Bot(USAR2004AgentParameters parameters, IComponentBus eventBus, IAgentLogger logger, IWorldView worldView, IAct act, IUSAR2004BotController init) {
101         super(parameters.getAgentId(), eventBus, logger, (WORLD_VIEW) worldView, (ACT) act);
102 
103         controller = init;
104 
105         init.initializeController(this);
106         init.prepareBot(this);
107         getWorldView().addEventListener(NfoMessage.class, firstNfoMessageListener);
108     }
109 }