View Javadoc

1   /**
2    * BaseUnrealEnvironment, an implementation of the environment interface standard that 
3    * facilitates the connection between GOAL and the UT2004 engine. 
4    * 
5    * Copyright (C) 2012 BaseUnrealEnvironment authors.
6    * 
7    * This program is free software: you can redistribute it and/or modify it under
8    * the terms of the GNU General Public License as published by the Free Software
9    * Foundation, either version 3 of the License, or (at your option) any later
10   * version.
11   * 
12   * This program is distributed in the hope that it will be useful, but WITHOUT
13   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14   * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15   * details.
16   * 
17   * You should have received a copy of the GNU General Public License along with
18   * this program. If not, see <http://www.gnu.org/licenses/>.
19   */
20  package nl.tudelft.goal.unreal.messages;
21  
22  import java.net.URI;
23  import java.util.Map;
24  import java.util.logging.Level;
25  
26  import nl.tudelft.goal.EIS2Java.exception.TranslationException;
27  import nl.tudelft.goal.EIS2Java.translation.Translator;
28  import nl.tudelft.goal.unreal.environment.UnrealEnvironmentException;
29  import cz.cuni.amis.pogamut.base.agent.impl.AgentId;
30  import cz.cuni.amis.pogamut.base.agent.params.IAgentParameters;
31  import cz.cuni.amis.pogamut.base.communication.connection.impl.socket.SocketConnectionAddress;
32  import cz.cuni.amis.pogamut.base.component.IComponent;
33  import cz.cuni.amis.pogamut.base.utils.logging.IAgentLogger;
34  import cz.cuni.amis.pogamut.base3d.worldview.object.Location;
35  import cz.cuni.amis.pogamut.base3d.worldview.object.Rotation;
36  import cz.cuni.amis.utils.token.IToken;
37  import cz.cuni.amis.utils.token.Tokens;
38  import eis.iilang.Parameter;
39  
40  /**
41   * Holds parameters specific for the bot.
42   * <ul>
43   * Parameters stored are:
44   * <li>{@link Key#BOTNAME}</li>
45   * <li>{@link Key#LEADTARGET}</li>
46   * <li>{@link Key#LOGLEVEL}</li>
47   * <li>{@link Key#BOTSERVER}</li>
48   * <li>{@link Key#SKILL}</li>
49   * <li>{@link Key#SKIN}</li>
50   * <li>{@link Key#TEAM}</li>
51   * <li>{@link Key#STARTLOCATION}</li>
52   * <li>{@link Key#STARTROTATION}</li>
53   * </ul>
54   * 
55   * Also provides functionality to assign defaults to parameters that have not
56   * been assigned.
57   * 
58   * @author M.P. Korstanje
59   * 
60   */
61  public class BotParameters extends Parameters implements IComponent {
62  
63  	// Bot parameters
64  	private Level logLevel;
65  	private Boolean shouldLeadTarget;
66  	private Integer skill;
67  	private Skin skin;
68  	private Location location;
69  	private Rotation rotation;
70  
71  	public BotParameters(IAgentLogger logger) {
72  		super(logger);
73  	}
74  
75  	public BotParameters(Map<String, Parameter> parameters, IAgentLogger logger) throws UnrealEnvironmentException {
76  		super(parameters, logger);
77  	}
78  
79  	public BotParameters(Parameters defaults, IAgentLogger logger) {
80  		super(logger);
81  		assignDefaults(defaults);
82  
83  		// Reset name to ensure name is unique.
84  		if (getAgentId() != null) {
85  			String name = getAgentId().getName().getFlag();
86  			setAgentId(name);
87  		}
88  	}
89  
90  	@Override
91  	public void assignDefaults(IAgentParameters defaults) {
92  		super.assignDefaults(defaults);
93  
94  		if (defaults instanceof BotParameters) {
95  			BotParameters parameters = (BotParameters) defaults;
96  
97  			if (logLevel == null)
98  				logLevel = parameters.getLogLevel();
99  			if (shouldLeadTarget == null)
100 				shouldLeadTarget = parameters.shouldLeadTarget();
101 			if (skill == null)
102 				skill = parameters.getSkill();
103 			if (skin == null)
104 				skin = parameters.getSkin();
105 			if(location == null)
106 				location = parameters.getStartLocation();
107 			if(rotation == null)
108 				rotation = parameters.getStartRotation();
109 		}
110 	}
111 
112 	@Override
113 	public IToken getComponentId() {
114 		return Tokens.get(getClass().getSimpleName());
115 	}
116 
117 	public Level getLogLevel() {
118 		return logLevel;
119 	}
120 
121 	public int getSkill() {
122 		return skill;
123 	}
124 
125 	public Skin getSkin() {
126 
127 		return skin;
128 	}
129 
130 	public BotParameters setLogLevel(Level level) {
131 		assert level != null;
132 		log.info(String.format("Set %s to %s.", Key.LOGLEVEL, level));
133 		this.logLevel = level;
134 		return this;
135 	}
136 
137 	public BotParameters setShouldLeadTarget(boolean shouldLeadTarget) {
138 		log.info(String.format("Set %s to %s.", Key.LEADTARGET, shouldLeadTarget));
139 		this.shouldLeadTarget = shouldLeadTarget;
140 		return this;
141 	}
142 
143 	/**
144 	 * Sets the bots skill between 0..7. The bots skill only influences the bots
145 	 * accuracy. Values outside the valid range are clamped.
146 	 * 
147 	 * @param skill
148 	 *            an integer in the range 0..7
149 	 */
150 	public BotParameters setSkill(int skill) {
151 		// Clamp between 0 and 7.
152 		this.skill = Math.min(7, Math.max(skill, 0));
153 		log.info(String.format("Set %s to %s.", Key.SKILL, this.skill));
154 		return this;
155 	}
156 
157 	public BotParameters setSkin(Skin skin) {
158 		log.info(String.format("Set %s to %s.", Key.SKIN, skin.toString()));
159 		this.skin = skin;
160 		return this;
161 	}
162 
163 	/**
164 	 * Sets the bots preferred team. Typically teams are either 0 or 1.
165 	 * 
166 	 * @param team
167 	 */
168 	public BotParameters setTeam(Team team) {
169 		log.info(String.format("Set %s to %s.", Key.TEAM, team));
170 		super.setTeam(team.id());
171 		return this;
172 	}
173 
174 	public BotParameters setStartLocation(Location location) {
175 		log.info(String.format("Set %s to %s.", Key.STARTLOCATION, location.toString()));
176 		this.location = location;
177 		return this;
178 	}
179 
180 	public BotParameters setStartRotation(Rotation rotation) {
181 		log.info(String.format("Set %s to %s.", Key.STARTROTATION, rotation.toString()));
182 		this.rotation = rotation;
183 		return this;
184 	}
185 
186 	public Boolean shouldLeadTarget() {
187 		return shouldLeadTarget;
188 	}
189 
190 	public Location getStartLocation() {
191 		return location;
192 	}
193 
194 	@Override
195 	public String toString() {
196 		return "BotParameters [logLevel=" + logLevel + ", shouldLeadTarget=" + shouldLeadTarget + ", skill=" + skill
197 				+ ", skin=" + skin + ", getTeam()=" + getTeam() + ", getWorldAddress()=" + getWorldAddress()
198 				+ ", getAgentId()=" + getAgentId() + "]";
199 	}
200 
201 	@Override
202 	protected void setKey(Key key, Parameter value) throws TranslationException {
203 
204 		switch (key) {
205 		case LEADTARGET:
206 			setShouldLeadTarget(value);
207 			break;
208 		case LOGLEVEL:
209 			setLogLevel(value);
210 			break;
211 		case SKILL:
212 			setSkill(value);
213 			break;
214 		case SKIN:
215 			setSkin(value);
216 			break;
217 		case TEAM:
218 			setTeam(value);
219 			break;
220 		case BOTSERVER:
221 			setWorldAddress(value);
222 			break;
223 		case STARTLOCATION:
224 			setStartLocation(value);
225 			break;
226 		case STARTROTATION:
227 			setStartRotation(value);
228 			break;
229 		default:
230 			// Not one of our keys.
231 			break;
232 		}
233 
234 	}
235 
236 	private void setStartRotation(Parameter value) throws TranslationException {
237 		setStartRotation(Translator.getInstance().translate2Java(value, Rotation.class));
238 
239 	}
240 
241 	private void setStartLocation(Parameter value) throws TranslationException {
242 		setStartLocation(Translator.getInstance().translate2Java(value, Location.class));
243 	}
244 
245 	private void setLogLevel(Parameter value) throws TranslationException {
246 		setLogLevel(Translator.getInstance().translate2Java(value, Level.class));
247 	}
248 
249 	private void setShouldLeadTarget(Parameter value) throws TranslationException {
250 		setShouldLeadTarget(Translator.getInstance().translate2Java(value, Boolean.class));
251 	}
252 
253 	private void setSkill(Parameter value) throws TranslationException {
254 		setSkill(Translator.getInstance().translate2Java(value, Integer.class));
255 	}
256 
257 	private void setSkin(Parameter value) throws TranslationException {
258 		setSkin(Translator.getInstance().translate2Java(value, Skin.class));
259 	}
260 
261 	private void setTeam(Parameter value) throws TranslationException {
262 		setTeam(Translator.getInstance().translate2Java(value, Team.class));
263 	}
264 
265 	private void setWorldAddress(Parameter value) throws TranslationException {
266 		URI uri = Translator.getInstance().translate2Java(value, URI.class);
267 		setWorldAddress(new SocketConnectionAddress(uri));
268 	}
269 
270 	public static BotParameters getDefaults(IAgentLogger logger) {
271 
272 		BotParameters parameters = new BotParameters(logger);
273 
274 		parameters.logLevel = Level.WARNING;
275 		parameters.shouldLeadTarget = true;
276 		parameters.skill = 3;
277 		parameters.skin = Skin.BotB;
278 		parameters.location = null;
279 		parameters.rotation = null;
280 
281 		parameters.setAgentIdSilent(DEFAULT_NAME);
282 		parameters.setWorldAddressSilent(LOCAL_HOST, BOT_SERVER_PORT);
283 
284 		return parameters;
285 	}
286 
287 	private void setWorldAddressSilent(String host, int port) {
288 		super.setWorldAddress(new SocketConnectionAddress(host, port));
289 	}
290 
291 	private void setAgentIdSilent(String name) {
292 		super.setAgentId(new AgentId(name));
293 	}
294 
295 	public Rotation getStartRotation() {
296 		return rotation;
297 	}
298 
299 }