View Javadoc

1   package cz.cuni.amis.pogamut.ut2004.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.IStuckDetector;
7   import cz.cuni.amis.pogamut.base3d.worldview.object.ILocated;
8   import cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004Bot;
9   import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.Self;
10  import cz.cuni.amis.utils.exception.PogamutException;
11  
12  /**
13   * Simple stub of the {@link IUT2004PathNavigator} that implements {@link AbstractUT2004PathNavigator#setBot(UT2004Bot)} and 
14   * {@link AbstractUT2004PathNavigator#setExecutor(IPathExecutorHelper)}.
15   * 
16   * @author Jimmy
17   *
18   * @param <PATH_ELEMENT>
19   */
20  public abstract class AbstractUT2004PathNavigator<PATH_ELEMENT extends ILocated> implements IUT2004PathNavigator<PATH_ELEMENT>{
21  
22  	/**
23  	 * Bot, the navigator is navigating. Set by {@link AbstractUT2004PathNavigator#setBot(UT2004Bot)}.
24  	 * See {@link IUT2004PathNavigator#setBot(UT2004Bot)}.
25  	 */
26  	protected UT2004Bot bot;
27  	
28  	/**
29  	 * Executor who is using the navigator. See {@link IUT2004PathNavigator#setExecutor(IPathExecutorHelper)}
30  	 * for more info.
31  	 */
32  	protected IUT2004PathExecutorHelper<PATH_ELEMENT> executor;
33  
34  	/**
35  	 * {@link Self} object that is lazy-initialized inside {@link AbstractUT2004PathNavigator#self}.
36  	 */
37  	protected Self self;
38  	
39  	protected boolean botWaiting = false;
40  	
41  	protected void setBotWaiting(boolean state) {
42  		botWaiting = state;
43  		for (IStuckDetector detector : executor.getStuckDetectors()) {
44  			detector.setBotWaiting(state);
45  		}
46  	}
47  	
48  	@Override
49  	public void setBot(UT2004Bot bot) {
50  		this.bot = bot;
51  	}
52  
53  	@Override
54  	public void setExecutor(IUT2004PathExecutorHelper<PATH_ELEMENT> owner) {
55  		this.executor = owner;		
56  	}
57  	
58  	@Override
59  	public void navigate(ILocated focus) {
60  		if (bot == null) throw new PogamutException("The 'bot' field is null (or was not set by the executor), can't navigate.", this);
61  		if (executor == null) throw new PogamutException("The 'executor' field is null (ow was not set by the executor), can't navigate.", this);
62  		// check that we're running in correct context
63  		int pathElementIndex = executor.getPathElementIndex();
64  		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);
65  		if (self == null) {
66  			self = bot.getWorldView().getSingle(Self.class);
67  			if (self == null) throw new PogamutException("Can't navigate the bot, no Self instance is available in the world view.", this);
68  		}
69  		// fire the navigation
70  		navigate(focus, pathElementIndex);
71  	}
72  		
73  	/**
74  	 * Does the actual navigation of the bot, it should steer it towards path element of the index 'pathElementIndex'.
75  	 * Called (after several checks) from {@link AbstractUT2004PathNavigator#navigate()}.
76  	 * @param pathElementIndex
77  	 */
78  	protected abstract void navigate(ILocated focus, int pathElementIndex);
79  
80  }