View Javadoc

1   package cz.cuni.amis.pogamut.udk.agent.navigation;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   import java.util.logging.Logger;
6   
7   import cz.cuni.amis.pogamut.base.agent.navigation.IPathPlanner;
8   import cz.cuni.amis.pogamut.base.agent.navigation.PathExecutorState;
9   import cz.cuni.amis.pogamut.base.agent.navigation.impl.BasePathExecutor;
10  import cz.cuni.amis.pogamut.base.communication.worldview.event.IWorldEventListener;
11  import cz.cuni.amis.pogamut.base.communication.worldview.object.IWorldObjectEventListener;
12  import cz.cuni.amis.pogamut.base.communication.worldview.object.event.WorldObjectFirstEncounteredEvent;
13  import cz.cuni.amis.pogamut.base3d.worldview.object.ILocated;
14  import cz.cuni.amis.pogamut.udk.agent.navigation.loquenavigator.LoqueNavigator;
15  import cz.cuni.amis.pogamut.udk.bot.impl.UDKBot;
16  import cz.cuni.amis.pogamut.udk.communication.messages.gbcommands.SetRoute;
17  import cz.cuni.amis.pogamut.udk.communication.messages.gbinfomessages.EndMessage;
18  import cz.cuni.amis.pogamut.udk.communication.messages.gbinfomessages.Self;
19  import cz.cuni.amis.utils.NullCheck;
20  
21  /**
22   * TO BE IMPLEMENTED!!!
23   * 
24   * TODO: FINISHE THE IMPLEMENTATION
25   * 
26   * @author Jimmy
27   *
28   * @param <PATH_ELEMENT>
29   */
30  public class UDKPathExecutorWithPlanner<PATH_ELEMENT extends ILocated> extends BasePathExecutor<PATH_ELEMENT> {
31  
32  	private IUDKPathNavigator<PATH_ELEMENT> navigator;
33  	
34  	private UDKBot bot;
35  	
36  	private Self self;
37  	
38  	private IWorldObjectEventListener<Self, WorldObjectFirstEncounteredEvent<Self>> selfListener = new IWorldObjectEventListener<Self, WorldObjectFirstEncounteredEvent<Self>>() {
39  		@Override
40  		public void notify(WorldObjectFirstEncounteredEvent<Self> event) {
41  			self = event.getObject();
42  		}
43  	};
44  	
45  	private IWorldEventListener<EndMessage> endMessageListener = new IWorldEventListener<EndMessage>() {
46  		@Override
47  		public void notify(EndMessage event) {
48  			eventEndMessage();
49  		}
50  	};
51  
52  	public UDKPathExecutorWithPlanner(UDKBot bot, IPathPlanner<PATH_ELEMENT> pathPlanner) {
53  		this(bot, pathPlanner, null, null);
54  	}
55  	
56  	public UDKPathExecutorWithPlanner(UDKBot bot, IPathPlanner<PATH_ELEMENT> pathPlanner, IUDKPathNavigator<PATH_ELEMENT> navigator) {
57  		this(bot, pathPlanner, navigator, null);
58  	}
59  	
60  	public UDKPathExecutorWithPlanner(UDKBot bot, IPathPlanner<PATH_ELEMENT> pathPlanner, IUDKPathNavigator<PATH_ELEMENT> navigator, Logger log) {
61  		super(log);
62  		if (getLog() == null) {
63  			setLog(bot.getLogger().getCategory(getClass().getSimpleName()));
64  		}
65  		NullCheck.check(bot, "bot");
66  		this.bot = bot;		
67  		this.navigator = navigator;
68  		if (this.navigator == null) {
69  			this.navigator = new LoqueNavigator<PATH_ELEMENT>(bot, getLog());
70  		}
71  		this.navigator.setBot(bot);
72  		this.navigator.setExecutor(this);
73  		bot.getWorldView().addObjectListener(Self.class, WorldObjectFirstEncounteredEvent.class, selfListener);
74  		bot.getWorldView().addEventListener(EndMessage.class, endMessageListener);
75  	}
76  	
77  	@Override
78  	protected void stopped() {		
79  	}
80  	
81  	@Override
82  	protected void followPathImpl() {		
83  	}
84  	
85  	/**
86  	 * If the path is not zero-length, recalls {@link IUDKPathNavigator#newPath(List)}
87  	 * and set the path into the GB2004 via {@link SetRoute}.
88  	 */
89  	@Override
90  	protected void pathComputedImpl() {
91  		if (getPath().size() == 0) {
92  			targetReached();
93  		} else {
94  			bot.getAct().act(new SetRoute().setRoute(getPath()));
95  			navigator.newPath(getPath());
96  		}
97  	}
98  
99  	@Override
100 	protected void pathComputationFailedImpl() {
101 	}
102 	
103 	@Override
104 	protected void stuckImpl() {
105 		navigator.reset();
106 	}
107 
108 	/**
109 	 * Sets the path into the GB2004 via {@link SetRoute} whenever switch occurs and the rest of the path is greater than
110 	 * 32 path elements.
111 	 */
112 	@Override
113 	protected void switchToAnotherPathElementImpl() {
114 		List<PATH_ELEMENT> path = getPath();
115 		if (path.size() > 31 + getPathElementIndex()) {
116 			List<PATH_ELEMENT> pathPart = new ArrayList<PATH_ELEMENT>(32);
117 			for (int i = getPathElementIndex(); i < path.size() && i < getPathElementIndex() + 31; ++i) {
118 				pathPart.add(path.get(i));
119 			}
120 			bot.getAct().act(new SetRoute().setRoute(pathPart));
121 		}
122 	}
123 
124 	@Override
125 	protected void targetReachedImpl() {
126 		navigator.reset();
127 	}
128 	
129 	protected void eventEndMessage() {
130 		if (inState(PathExecutorState.PATH_COMPUTED) || inState(PathExecutorState.SWITCHED_TO_ANOTHER_PATH_ELEMENT)) {
131 			navigator.navigate();
132 		}
133 	}
134 	
135 }