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.unreal.environment.UnrealEnvironmentException;
27  import nl.tudelft.goal.ut2004.util.Skin;
28  import nl.tudelft.goal.ut2004.util.Team;
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.eis2java.exception.TranslationException;
39  import eis.eis2java.translation.Translator;
40  import eis.iilang.Parameter;
41  /**
42   * Holds parameters specific for the bot.
43   * <ul>
44   * Parameters stored are:
45   * <li>{@link Key#BOTNAME}</li>
46   * <li>{@link Key#LEADTARGET}</li>
47   * <li>{@link Key#LOGLEVEL}</li>
48   * <li>{@link Key#BOTSERVER}</li>
49   * <li>{@link Key#SKILL}</li>
50   * <li>{@link Key#SKIN}</li>
51   * <li>{@link Key#TEAM}</li>
52   * <li>{@link Key#STARTLOCATION}</li>
53   * <li>{@link Key#STARTROTATION}</li>
54   * </ul>
55   * 
56   * Also provides functionality to assign defaults to parameters that have not
57   * been assigned.
58   * 
59   * @author M.P. Korstanje
60   * 
61   */
62  public class BotParameters extends Parameters implements IComponent {
63  
64  	// Bot parameters
65  	private Level logLevel;
66  	private Boolean shouldLeadTarget;
67  	private Integer skill;
68  	private Skin skin;
69  
70  	public BotParameters(IAgentLogger logger) {
71  		super(logger);
72  	}
73  
74  	public BotParameters(Map<String, Parameter> parameters, IAgentLogger logger) throws UnrealEnvironmentException {
75  		super(parameters, logger);
76  	}
77  
78  	public BotParameters(Parameters defaults, IAgentLogger logger) {
79  		super(logger);
80  		assignDefaults(defaults);
81  
82  		// Reset name to ensure name is unique.
83  		if (getAgentId() != null) {
84  			String name = getAgentId().getName().getFlag();
85  			setAgentId(name);
86  		}
87  	}
88  
89  	@Override
90  	public void assignDefaults(IAgentParameters defaults) {
91  		super.assignDefaults(defaults);
92  
93  		if (defaults instanceof BotParameters) {
94  			BotParameters parameters = (BotParameters) defaults;
95  
96  			if (logLevel == null)
97  				logLevel = parameters.getLogLevel();
98  			if (shouldLeadTarget == null)
99  				shouldLeadTarget = parameters.shouldLeadTarget();
100 			if (skill == null)
101 				skill = parameters.getSkill();
102 			if (skin == null)
103 				skin = parameters.getSkin();
104 		}
105 	}
106 
107 	@Override
108 	public IToken getComponentId() {
109 		return Tokens.get(getClass().getSimpleName());
110 	}
111 
112 	public Level getLogLevel() {
113 		return logLevel;
114 	}
115 
116 	public int getSkill() {
117 		return skill;
118 	}
119 
120 	public Skin getSkin() {
121 
122 		return skin;
123 	}
124 
125 	public BotParameters setLogLevel(Level level) {
126 		assert level != null;
127 		log.info(String.format("Set %s to %s.", Key.LOGLEVEL, level));
128 		this.logLevel = level;
129 		return this;
130 	}
131 
132 	public BotParameters setShouldLeadTarget(boolean shouldLeadTarget) {
133 		log.info(String.format("Set %s to %s.", Key.LEADTARGET, shouldLeadTarget));
134 		this.shouldLeadTarget = shouldLeadTarget;
135 		return this;
136 	}
137 
138 	/**
139 	 * Sets the bots skill between 0..7. The bots skill only influences the bots
140 	 * accuracy. Values outside the valid range are clamped.
141 	 * 
142 	 * @param skill
143 	 *            an integer in the range 0..7
144 	 */
145 	public BotParameters setSkill(int skill) {
146 		// Clamp between 0 and 7.
147 		this.skill = Math.min(7, Math.max(skill, 0));
148 		log.info(String.format("Set %s to %s.", Key.SKILL, this.skill));
149 		return this;
150 	}
151 
152 	public BotParameters setSkin(Skin skin) {
153 		log.info(String.format("Set %s to %s.", Key.SKIN, skin.toString()));
154 		this.skin = skin;
155 		return this;
156 	}
157 
158 	/**
159 	 * Sets the bots preferred team. Typically teams are either 0 or 1.
160 	 * 
161 	 * @param team
162 	 */
163 	public BotParameters setTeam(Team team) {
164 		log.info(String.format("Set %s to %s.", Key.TEAM, team));
165 		super.setTeam(team.id());
166 		return this;
167 	}
168 
169 	@Override
170 	public BotParameters setInitialLocation(Location location) {
171 		log.info(String.format("Set %s to %s.", Key.STARTLOCATION, location.toString()));
172 		super.setInitialLocation(location);
173 		return this;
174 	}
175 	
176 	@Override
177 	public BotParameters setInitialRotation(Rotation rotation) {
178 		log.info(String.format("Set %s to %s.", Key.STARTROTATION, rotation.toString()));
179 		super.setInitialRotation(rotation);
180 		return this;
181 	}
182 
183 	public Boolean shouldLeadTarget() {
184 		return shouldLeadTarget;
185 	}
186 
187 	@Override
188 	public String toString() {
189 		return "BotParameters [logLevel=" + logLevel + ", shouldLeadTarget=" + shouldLeadTarget + ", skill=" + skill
190 				+ ", skin=" + skin + ", getTeam()=" + getTeam() + ", getWorldAddress()=" + getWorldAddress()
191 				+ ", getAgentId()=" + getAgentId() + "]";
192 	}
193 
194 	@Override
195 	protected void setKey(Key key, Parameter value) throws TranslationException {
196 
197 		switch (key) {
198 		case LEADTARGET:
199 			setShouldLeadTarget(value);
200 			break;
201 		case LOGLEVEL:
202 			setLogLevel(value);
203 			break;
204 		case SKILL:
205 			setSkill(value);
206 			break;
207 		case SKIN:
208 			setSkin(value);
209 			break;
210 		case TEAM:
211 			setTeam(value);
212 			break;
213 		case BOTSERVER:
214 			setWorldAddress(value);
215 			break;
216 		case STARTLOCATION:
217 			setInitialLocation(value);
218 			break;
219 		case STARTROTATION:
220 			setInitialRotation(value);
221 			break;
222 		default:
223 			// Not one of our keys.
224 			break;
225 		}
226 
227 	}
228 
229 	private void setInitialRotation(Parameter value) throws TranslationException {
230 		setInitialRotation(Translator.getInstance().translate2Java(value, Rotation.class));
231 
232 	}
233 
234 	private void setInitialLocation(Parameter value) throws TranslationException {
235 		setInitialLocation(Translator.getInstance().translate2Java(value, Location.class));
236 	}
237 
238 	private void setLogLevel(Parameter value) throws TranslationException {
239 		setLogLevel(Translator.getInstance().translate2Java(value, Level.class));
240 	}
241 
242 	private void setShouldLeadTarget(Parameter value) throws TranslationException {
243 		setShouldLeadTarget(Translator.getInstance().translate2Java(value, Boolean.class));
244 	}
245 
246 	private void setSkill(Parameter value) throws TranslationException {
247 		setSkill(Translator.getInstance().translate2Java(value, Integer.class));
248 	}
249 
250 	private void setSkin(Parameter value) throws TranslationException {
251 		setSkin(Translator.getInstance().translate2Java(value, Skin.class));
252 	}
253 
254 	private void setTeam(Parameter value) throws TranslationException {
255 		setTeam(Translator.getInstance().translate2Java(value, Team.class));
256 	}
257 
258 	private void setWorldAddress(Parameter value) throws TranslationException {
259 		URI uri = Translator.getInstance().translate2Java(value, URI.class);
260 		setWorldAddress(new SocketConnectionAddress(uri));
261 	}
262 
263 	public static BotParameters getDefaults(IAgentLogger logger) {
264 
265 		BotParameters parameters = new BotParameters(logger);
266 
267 		parameters.logLevel = Level.WARNING;
268 		parameters.shouldLeadTarget = true;
269 		parameters.skill = 3;
270 		parameters.skin = Skin.BotB;
271 
272 		parameters.setAgentIdSilent(DEFAULT_NAME);
273 		parameters.setWorldAddressSilent(LOCAL_HOST, BOT_SERVER_PORT);
274 
275 		return parameters;
276 	}
277 
278 	private void setWorldAddressSilent(String host, int port) {
279 		super.setWorldAddress(new SocketConnectionAddress(host, port));
280 	}
281 
282 	private void setAgentIdSilent(String name) {
283 		super.setAgentId(new AgentId(name));
284 	}
285 
286 
287 
288 }