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  	public boolean isBotWaiting() {
49  		return botWaiting && self.getVelocity().isZero(10);
50  	}
51  	
52  	@Override
53  	public void setBot(UT2004Bot bot) {
54  		this.bot = bot;
55  	}
56  
57  	@Override
58  	public void setExecutor(IUT2004PathExecutorHelper<PATH_ELEMENT> owner) {
59  		this.executor = owner;		
60  	}
61  	
62  	@Override
63  	public void navigate(ILocated focus) {
64  		if (bot == null) throw new PogamutException("The 'bot' field is null (or was not set by the executor), can't navigate.", this);
65  		if (executor == null) throw new PogamutException("The 'executor' field is null (ow was not set by the executor), can't navigate.", this);
66  		// check that we're running in correct context
67  		int pathElementIndex = executor.getPathElementIndex();
68  		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);
69  		if (self == null) {
70  			self = bot.getWorldView().getSingle(Self.class);
71  			if (self == null) throw new PogamutException("Can't navigate the bot, no Self instance is available in the world view.", this);
72  		}
73  		// fire the navigation
74  		navigate(focus, pathElementIndex);
75  	}
76  		
77  	/**
78  	 * Does the actual navigation of the bot, it should steer it towards path element of the index 'pathElementIndex'.
79  	 * Called (after several checks) from {@link AbstractUT2004PathNavigator#navigate()}.
80  	 * @param pathElementIndex
81  	 */
82  	protected abstract void navigate(ILocated focus, int pathElementIndex);
83  
84  }