View Javadoc

1   package cz.cuni.amis.pogamut.udk.agent.navigation;
2   
3   import java.util.List;
4   
5   import cz.cuni.amis.pogamut.base.agent.navigation.IPathExecutorHelper;
6   import cz.cuni.amis.pogamut.base.agent.navigation.PathExecutorState;
7   import cz.cuni.amis.pogamut.base3d.worldview.object.ILocated;
8   import cz.cuni.amis.pogamut.udk.bot.impl.UDKBot;
9   import cz.cuni.amis.pogamut.udk.communication.messages.gbinfomessages.Self;
10  import cz.cuni.amis.utils.exception.PogamutException;
11  
12  /**
13   * Simple stub of the {@link IUDKPathNavigator} that implements {@link AbstractUDKPathNavigator#setBot(UDKBot)} and 
14   * {@link AbstractUDKPathNavigator#setExecutor(IPathExecutorHelper)}.
15   * 
16   * @author Jimmy
17   *
18   * @param <PATH_ELEMENT>
19   */
20  public abstract class AbstractUDKPathNavigator<PATH_ELEMENT extends ILocated> implements IUDKPathNavigator<PATH_ELEMENT>{
21  
22  	/**
23  	 * Bot, the navigator is navigating. Set by {@link AbstractUDKPathNavigator#setBot(UDKBot)}.
24  	 * See {@link IUDKPathNavigator#setBot(UDKBot)}.
25  	 */
26  	protected UDKBot bot;
27  	
28  	/**
29  	 * Executor who is using the navigator. See {@link IUDKPathNavigator#setExecutor(IPathExecutorHelper)}
30  	 * for more info.
31  	 */
32  	protected IPathExecutorHelper<PATH_ELEMENT> executor;
33  
34  	/**
35  	 * {@link Self} object that is lazy-initialized inside {@link AbstractUDKPathNavigator#self}.
36  	 */
37  	protected Self self;
38  	
39  	@Override
40  	public void setBot(UDKBot bot) {
41  		this.bot = bot;
42  	}
43  
44  	@Override
45  	public void setExecutor(IPathExecutorHelper<PATH_ELEMENT> owner) {
46  		this.executor = owner;		
47  	}
48  	
49  	@Override
50  	public void navigate() {
51  		if (bot == null) throw new PogamutException("The 'bot' field is null (or was not set by the executor), can't navigate.", this);
52  		if (executor == null) throw new PogamutException("The 'executor' field is null (ow was not set by the executor), can't navigate.", this);
53                  
54                  if(executor.inState(PathExecutorState.STOPPED)) {
55                      //On rare occasions this may happen when the bot is being killed. The following code throw NullPointerException in such cases
56                      //so we don't let it execute. 
57                      if(bot.getLog()!= null){
58                          bot.getLog().severe("AbstractUDKPathNavigator.navigate() called, eventhough the executor is stopped. Ignoring.");                        
59                      }
60                      return;
61                  }
62                  
63  		// check that we're running in correct context
64  		int pathElementIndex = executor.getPathElementIndex();
65  		if (pathElementIndex < 0 || pathElementIndex >= executor.getPath().size()) throw new PogamutException("Can't navigate as the current path element index is out of path range (index = " + pathElementIndex + ", path.size() = " + executor.getPath().size() + ".", this);
66  		if (self == null) {
67  			self = bot.getWorldView().getSingle(Self.class);
68  			if (self == null) throw new PogamutException("Can't navigate the bot, no Self instance is available in the world view.", this);
69  		}
70  		// fire the navigation
71  		navigate(pathElementIndex);
72  	}
73  		
74  	/**
75  	 * Does the actual navigation of the bot, it should steer it towards path element of the index 'pathElementIndex'.
76  	 * Called (after several checks) from {@link AbstractUDKPathNavigator#navigate()}.
77  	 * @param pathElementIndex
78  	 */
79  	protected abstract void navigate(int pathElementIndex);
80  
81  }