View Javadoc

1   package cz.cuni.amis.pogamut.ut2004.agent.module.sensomotoric;
2   
3   import cz.cuni.amis.pogamut.base.agent.module.SensorModule;
4   import cz.cuni.amis.pogamut.base.communication.worldview.IWorldView;
5   import cz.cuni.amis.pogamut.base.communication.worldview.object.IWorldObjectEvent;
6   import cz.cuni.amis.pogamut.base.communication.worldview.object.IWorldObjectEventListener;
7   import cz.cuni.amis.pogamut.base.communication.worldview.object.event.WorldObjectUpdatedEvent;
8   import cz.cuni.amis.pogamut.base.utils.logging.LogCategory;
9   import cz.cuni.amis.pogamut.base3d.worldview.object.Rotation;
10  import cz.cuni.amis.pogamut.ut2004.bot.IUT2004BotController;
11  import cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004Bot;
12  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.Configuration;
13  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.Pick;
14  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.ConfigChange;
15  
16  /**
17   * Memory module specialized on the agent's configuration inside UT2004.
18   * <p><p>
19   * It should be instantiated inside {@link IUT2004BotController#prepareBot(UT2004Bot)()} class and it may be used since
20   * {@link IUT2004BotController#botInitialized(cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.GameInfo, ConfigChange, cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.InitedMessage)}
21   * is called.
22   * 
23   * @author Jimmy
24   */
25  public class AgentConfig extends SensorModule<UT2004Bot> {
26  	
27  	/**
28  	 * True, if you have to spawn the bot manually after each death.
29  	 */
30  	public boolean isManualSpawn() {
31  		if (lastConfig == null) return false;
32  		return lastConfig.isManualSpawn();
33  	}
34  	
35  	/**
36  	 * Enables/disables manual spawning.
37  	 * <p><p>
38  	 * Sets if the bot will have to be respawned after death
39  	 * manually by RESPAWN command. If false, the bot will respawn automatically.
40  	 * @param state
41  	 */
42  	public void setManualSpawn(boolean state) {
43  		bot.getAct().act(new Configuration().setManualSpawn(state));
44  	}
45  
46  	/**
47  	 * True, if the bot is using auto ray tracing (is provided with synchronous
48  	 * ATR messages). See ATR messages for more details.
49  	 */
50  	public boolean isAutoTrace() {
51  		if (lastConfig == null) return false;
52  		return lastConfig.isAutoTrace();
53  	}
54  	
55  	/**         
56       * Enables/disables auto ray tracing feature.
57       */
58  	public void setAutoTrace(boolean state) {
59  		bot.getAct().act(new Configuration().setAutoTrace(state));
60  	}
61  
62  	/**
63  	 * The bot's name.
64  	 */
65  	public String getName() {
66  		if (lastConfig == null) return "unknown yet";
67  		return lastConfig.getName();
68  	}
69  	
70  	/**         
71       * You can change the name of the bot in the game.
72       */
73  	public void setName(String newName) {
74  		bot.getAct().act(new Configuration().setName(newName));
75  		bot.getComponentId().getName().setFlag(newName);
76  	}
77  	
78  	/**
79  	 * Bots default speed will be multiplied by this number. Ranges from 0.1 to
80  	 * 2 (default, can be set in ini in [RemoteBot] MaxSpeed).
81  	 */
82  	public double getSpeedMultiplier() {
83  		if (lastConfig == null) return -1;
84  		return lastConfig.getSpeedMultiplier();
85  	}
86  	
87  	/**         
88       * Bots default speed will be multiplied by this number. Ranges from 0.1 to 2 (default, can be set in ini in [RemoteBot] MaxSpeed).
89       */
90  	public void setSpeedMultiplier(double value) {
91  		if (value < 0.1) value = 0.1;
92  		else if (value > 2) value = 2;
93  		bot.getAct().act(new Configuration().setSpeedMultiplier(value));
94  	}
95  
96  	/**
97  	 * If bot is invulnerable (cannot die) or not.
98  	 */
99  	public boolean isInvulnerable() {
100 		if (lastConfig == null) return false;
101 		return lastConfig.isInvulnerable();
102 	}
103 	
104 	 /**         
105       * Will set godmode for bot on (bot can't be killed). This can
106 	  * be changed just when cheating is enabled on the server.
107 	  * (bAllowCheats = True)
108       */
109 	public void setInvulnerability(boolean state) {
110 		bot.getAct().act(new Configuration().setInvulnerable(state));
111 	}	
112 
113 	/**
114 	 * The delay between two synchronous batches (can range from 0.1 to 2
115 	 * seconds).
116 	 */
117 	public double getVisionTime() {
118 		if (lastConfig == null) return -1;
119 		return lastConfig.getVisionTime();
120 	}
121 	
122 	 /**         
123 	  * Between 0.1 to 2 seconds, it sets the delay between two
124 	  * synchronous batches.
125 	  */
126 	public void setVisionTime(double value) {
127 		if (value < 0.1) value = 0.1;
128 		else if (value > 2) value = 2;
129 		bot.getAct().act(new Configuration().setVisionTime(value));
130 	}
131 
132 	/**
133 	 * If some additional debug information will be shown in the UT2004 server
134 	 * console window.
135 	 */
136 	public boolean isShowDebug() {
137 		if (lastConfig == null) return false;
138 		return lastConfig.isShowDebug();
139 	}
140 	
141 	/**         
142      * If true some additional debug information will be logged to
143 	 * UT2004 server console window.
144 	 */
145 	public void setShowDebug(boolean state) {
146 		bot.getAct().act(new Configuration().setShowDebug(state));
147 	}
148 
149 	/**
150 	 * If true an actor visualizing the location the bot is actually looking at
151 	 * will appear in the game.
152 	 */
153 	public boolean isShowFocalPoint() {
154 		if (lastConfig == null) return false;
155 		return lastConfig.isShowFocalPoint();
156 	}
157 	
158 	/**         
159      * If set to true a marker will appear in the game on the
160 	 * location the bot is actually looking at.
161 	 */
162 	public void setShowFocalPoint(boolean state) {
163 		bot.getAct().act(new Configuration().setShowFocalPoint(state));
164 	}
165 
166 	/**
167 	 * If the GB should draw lines representing the auto ray traces of the bot
168 	 * (for more information see ATR message).
169 	 */
170 	public boolean isDrawTraceLines() {
171 		if (lastConfig == null) return false;
172 		return lastConfig.isDrawTraceLines();
173 	}
174 	
175 	/**
176 	 * If set to true an actor will appear in the game on the
177 	 * location the bot is actually looking at.
178 	 */
179 	public void setDrawTraceLines(boolean state) {
180 		bot.getAct().act(new Configuration().setDrawTraceLines(state));
181 	}
182 
183 	/**
184 	 * It informs whether the sending of all GB synchronous messages is enabled or
185 	 * disabled.
186 	 */
187 	public boolean isSynchronousBatchExported() {
188 		if (lastConfig == null) return false;		
189 		return !lastConfig.isSynchronousOff();
190 	}
191 	
192 	/**         
193      * It enables/disables sending of all GB synchronous messages
194 	 * for the bot.
195      */
196 	public void setSynchronousBatchExport(boolean state) {
197 		bot.getAct().act(new Configuration().setSynchronousOff(!state));
198 	}
199 
200 	/**
201 	 * It enables/disables automatic pickup of the bot. If false the items can be
202 	 * picked up only through {@link Pick} command.
203 	 */
204 	public boolean isAutoPickup() {
205 		if (lastConfig == null) return false;		
206 		return !lastConfig.isAutoPickupOff();
207 	}
208 	
209 	/**         
210      * 
211 	 * It enables/disables automatic pickup of the bot. If false is set the items can be picked up through {@link Pick} command.
212      */
213 	public void setAutoPickup(boolean state) {
214 		bot.getAct().act(new Configuration().setAutoPickupOff(!state));
215 	}
216 	
217 	/**
218 	 * Tells how fast the bot is rotating in all three directions (pitch, yaw, roll).
219 	 * @return
220 	 */
221 	public Rotation getRotationSpeed() {
222 		if (lastConfig == null) return null;
223 		return lastConfig.getRotationRate();
224 	}
225 	
226 	/**
227 	 * Sets how fast the bot will rotate horizontally (i.e., yaw rotation == left/right).
228 	 * @param speed desired rotation speed
229 	 */
230 	public void setRotationHorizontalSpeed(double speed) {
231 		Rotation actual = getRotationSpeed();
232 		if (actual == null) return;
233 		actual = new Rotation(actual);
234 		actual.yaw = speed;
235 		bot.getAct().act(new Configuration().setRotationRate(actual));
236 	}
237 	
238 	/**
239 	 * Sets how fast the bot will rotate vertically (i.e., pitch rotation == up/down).
240 	 * @param speed desired rotation speed
241 	 */
242 	public void setRotationVerticalSpeed(double speed) {
243 		Rotation actual = getRotationSpeed();
244 		if (actual == null) return;
245 		actual = new Rotation(actual);
246 		actual.pitch = speed;
247 		bot.getAct().act(new Configuration().setRotationRate(actual));
248 	}
249 	
250 	/**
251 	 * Sets how fast the bot will rotate in all three axes (yaw/pitch/roll).
252 	 * @param rotationSpeeds desired rotation speeds
253 	 */
254 	public void setRotationSpeed(Rotation rotationSpeeds) {
255 		bot.getAct().act(new Configuration().setRotationRate(rotationSpeeds));
256 	}
257 	
258 	/**
259 	 * Retrieves the configuration of the bot inside UT2004.
260 	 * @return Configuration of the bot.
261 	 */
262 	public ConfigChange getConfig() {
263 		return lastConfig;
264 	}
265 	
266 	/*========================================================================*/
267 	
268 	/**
269 	 * {@link ConfigChange} listener.
270 	 */
271 	private class ConfigChangeListener implements IWorldObjectEventListener<ConfigChange, IWorldObjectEvent<ConfigChange>>
272 	{
273 		private IWorldView worldView;
274 
275 		/**
276 		 * Constructor. Registers itself on the given WorldView object.
277 		 * @param worldView WorldView object to listent to.
278 		 */
279 		public ConfigChangeListener(IWorldView worldView)
280 		{
281 			worldView.addObjectListener(ConfigChange.class, WorldObjectUpdatedEvent.class, this);
282 			this.worldView = worldView;
283 		}
284 
285 		@Override
286 		public void notify(IWorldObjectEvent<ConfigChange> event) {
287 			lastConfig = event.getObject();			
288 		}
289 	}
290 
291 	/** {@link ConfigChange} listener */
292 	private ConfigChangeListener configChangeListener;
293 	
294 	private ConfigChange lastConfig = null;
295 
296 	private UT2004Bot bot;
297 
298 
299 	/*========================================================================*/
300 	
301 	/**
302 	 * Constructor. Setups the memory module based on bot's world view.
303 	 * @param bot owner of the module
304 	 */
305 	public AgentConfig(UT2004Bot bot) {
306 		this(bot, null);		
307 	}
308 
309 	/**
310 	 * Constructor. Setups the memory module based on bot's world view.
311 	 * @param bot owner of the module
312 	 * @param moduleLog where to log module's messages
313 	 */
314 	public AgentConfig(UT2004Bot bot, LogCategory moduleLog) {
315 		super(bot, moduleLog);
316 		this.bot = bot;
317 		configChangeListener = new ConfigChangeListener(bot.getWorldView());
318 		
319 		cleanUp();
320 	}	
321 		
322 	@Override
323 	protected void cleanUp() {
324 		super.cleanUp();
325 		lastConfig = null;
326 	}
327 	
328 	
329 }