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).setYaw(speed);
234 		bot.getAct().act(new Configuration().setRotationRate(actual));
235 	}
236 	
237 	/**
238 	 * Sets how fast the bot will rotate vertically (i.e., pitch rotation == up/down).
239 	 * @param speed desired rotation speed
240 	 */
241 	public void setRotationVerticalSpeed(double speed) {
242 		Rotation actual = getRotationSpeed();
243 		if (actual == null) return;
244 		actual = new Rotation(actual).setPitch(speed);
245 		bot.getAct().act(new Configuration().setRotationRate(actual));
246 	}
247 	
248 	/**
249 	 * Sets how fast the bot will rotate in all three axes (yaw/pitch/roll).
250 	 * @param rotationSpeeds desired rotation speeds
251 	 */
252 	public void setRotationSpeed(Rotation rotationSpeeds) {
253 		bot.getAct().act(new Configuration().setRotationRate(rotationSpeeds));
254 	}
255 	
256 	/**
257 	 * Retrieves the configuration of the bot inside UT2004.
258 	 * @return Configuration of the bot.
259 	 */
260 	public ConfigChange getConfig() {
261 		return lastConfig;
262 	}
263 	
264 	/*========================================================================*/
265 	
266 	/**
267 	 * {@link ConfigChange} listener.
268 	 */
269 	private class ConfigChangeListener implements IWorldObjectEventListener<ConfigChange, IWorldObjectEvent<ConfigChange>>
270 	{
271 		private IWorldView worldView;
272 
273 		/**
274 		 * Constructor. Registers itself on the given WorldView object.
275 		 * @param worldView WorldView object to listent to.
276 		 */
277 		public ConfigChangeListener(IWorldView worldView)
278 		{
279 			worldView.addObjectListener(ConfigChange.class, WorldObjectUpdatedEvent.class, this);
280 			this.worldView = worldView;
281 		}
282 
283 		@Override
284 		public void notify(IWorldObjectEvent<ConfigChange> event) {
285 			lastConfig = event.getObject();			
286 		}
287 	}
288 
289 	/** {@link ConfigChange} listener */
290 	private ConfigChangeListener configChangeListener;
291 	
292 	private ConfigChange lastConfig = null;
293 
294 	private UT2004Bot bot;
295 
296 
297 	/*========================================================================*/
298 	
299 	/**
300 	 * Constructor. Setups the memory module based on bot's world view.
301 	 * @param bot owner of the module
302 	 */
303 	public AgentConfig(UT2004Bot bot) {
304 		this(bot, null);		
305 	}
306 
307 	/**
308 	 * Constructor. Setups the memory module based on bot's world view.
309 	 * @param bot owner of the module
310 	 * @param moduleLog where to log module's messages
311 	 */
312 	public AgentConfig(UT2004Bot bot, LogCategory moduleLog) {
313 		super(bot, moduleLog);
314 		this.bot = bot;
315 		configChangeListener = new ConfigChangeListener(bot.getWorldView());
316 		
317 		cleanUp();
318 	}	
319 		
320 	@Override
321 	protected void cleanUp() {
322 		super.cleanUp();
323 		lastConfig = null;
324 	}
325 	
326 	
327 }