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
14
15
16
17
18
19
20 public abstract class AbstractUT2004PathNavigator<PATH_ELEMENT extends ILocated> implements IUT2004PathNavigator<PATH_ELEMENT>{
21
22
23
24
25
26 protected UT2004Bot bot;
27
28
29
30
31
32 protected IUT2004PathExecutorHelper<PATH_ELEMENT> executor;
33
34
35
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
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
74 navigate(focus, pathElementIndex);
75 }
76
77
78
79
80
81
82 protected abstract void navigate(ILocated focus, int pathElementIndex);
83
84 }