View Javadoc

1   package nl.tudelft.goal.ut2004.server;
2   
3   import java.util.ArrayList;
4   import java.util.Collection;
5   import java.util.List;
6   
7   import nl.tudelft.goal.unreal.messages.Percept;
8   import nl.tudelft.goal.unreal.messages.UnrealIdOrLocation;
9   import nl.tudelft.goal.ut2004.messages.FireMode;
10  import nl.tudelft.goal.ut2004.util.Team;
11  
12  import com.google.inject.Inject;
13  
14  import cz.cuni.amis.pogamut.base.communication.command.IAct;
15  import cz.cuni.amis.pogamut.base.communication.connection.impl.socket.SocketConnection;
16  import cz.cuni.amis.pogamut.base.communication.worldview.event.IWorldEventListener;
17  import cz.cuni.amis.pogamut.base.component.bus.IComponentBus;
18  import cz.cuni.amis.pogamut.base.utils.logging.IAgentLogger;
19  import cz.cuni.amis.pogamut.base3d.worldview.object.ILocated;
20  import cz.cuni.amis.pogamut.base3d.worldview.object.Rotation;
21  import cz.cuni.amis.pogamut.unreal.communication.messages.UnrealId;
22  import cz.cuni.amis.pogamut.ut2004.agent.params.UT2004AgentParameters;
23  import cz.cuni.amis.pogamut.ut2004.communication.messages.ItemType;
24  import cz.cuni.amis.pogamut.ut2004.communication.messages.ItemType.Category;
25  import cz.cuni.amis.pogamut.ut2004.communication.messages.ItemType.Group;
26  import cz.cuni.amis.pogamut.ut2004.communication.messages.UT2004ItemType;
27  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.AddInventory;
28  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.ChangeTeam;
29  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.Pause;
30  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.Respawn;
31  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.SetGameSpeed;
32  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.SpawnActor;
33  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.GameInfo;
34  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.NavPoint;
35  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.Player;
36  import cz.cuni.amis.pogamut.ut2004.communication.worldview.UT2004WorldView;
37  import cz.cuni.amis.pogamut.ut2004.server.impl.UT2004Server;
38  import cz.cuni.amis.utils.exception.PogamutException;
39  import eis.eis2java.annotation.AsAction;
40  import eis.eis2java.annotation.AsPercept;
41  import eis.eis2java.translation.Filter.Type;
42  
43  public class EnvironmentControllerServer extends UT2004Server {
44  
45  	private IWorldEventListener<GameInfo> pausedListener = new IWorldEventListener<GameInfo>() {
46  
47  		@Override
48  		public void notify(GameInfo event) {
49  
50  		}
51  	};
52  
53  	@Inject
54  	public EnvironmentControllerServer(UT2004AgentParameters params,
55  			IAgentLogger agentLogger, IComponentBus bus,
56  			SocketConnection connection, UT2004WorldView worldView, IAct act) {
57  		super(params, agentLogger, bus, connection, worldView, act);
58  
59  		worldView.addEventListener(GameInfo.class, pausedListener);
60  
61  	}
62  
63  	public void sendResumeGame() {
64  		Pause resume = new Pause(false, false);
65  		getAct().act(resume);
66  	}
67  
68  	public void sendPausegame() {
69  		Pause pause = new Pause(true, false);
70  		getAct().act(pause);
71  
72  	}
73  
74  	@AsAction(name = "addInventory")
75  	public void addInventory(UnrealId id, Category category, Group group) {
76  		for (ItemType item : group.getTypes()) {
77  			if (item.getCategory().equals(category)) {
78  				AddInventory addInventory = new AddInventory(id, item.getName());
79  				getAct().act(addInventory);
80  				return;
81  			}
82  		}
83  
84  		String message = String.format(
85  				"%s was not a ItemType in the %s category.", group, category);
86  		throw new PogamutException(message, this);
87  	}
88  
89  	@AsAction(name = "respawn")
90  	public void respawn(UnrealId id, UnrealIdOrLocation unrealIdOrLocation,
91  			Rotation rotation) {
92  
93  		ILocated location = unrealIdOrLocation.toILocated(getWorldView());
94  
95  		if (location == null) {
96  			String message = String.format(
97  					"Could not resolve %s to a location", unrealIdOrLocation);
98  			throw new PogamutException(message, this);
99  		}
100 
101 		getAct().act(new Respawn(id, location.getLocation(), rotation));
102 
103 	}
104 
105 	@AsAction(name = "changeTeam")
106 	public void changeTeam(UnrealId id) {
107 
108 		Player player = getWorldView().get(id, Player.class);
109 
110 		if (player == null) {
111 			String message = String.format("Could not resolve %s to a player",
112 					id);
113 			throw new PogamutException(message, this);
114 		}
115 
116 		getAct().act(new ChangeTeam(id, 1 - player.getTeam()));
117 
118 	}
119 
120 	@AsAction(name = "setGameSpeed")
121 	public void setGameSpeed(Double speed) {
122 		getAct().act(new SetGameSpeed(speed));
123 	}
124 
125 	@AsAction(name = "spawnItem")
126 	public void spawnItem(UnrealIdOrLocation unrealIdOrLocation,
127 			Category category, Group group) {
128 
129 		ILocated location = unrealIdOrLocation.toILocated(getWorldView());
130 
131 		if (location == null) {
132 			String message = String.format(
133 					"Could not resolve %s to a location", unrealIdOrLocation);
134 			throw new PogamutException(message, this);
135 		}
136 
137 		for (ItemType item : group.getTypes()) {
138 			if (item.getCategory().equals(category)) {
139 				SpawnActor spawn = new SpawnActor(location.getLocation(),
140 						Rotation.ZERO, item.getName());
141 				getAct().act(spawn);
142 				return;
143 			}
144 		}
145 
146 		String message = String.format(
147 				"%s was not a ItemType in the %s category.", group, category);
148 		throw new PogamutException(message, this);
149 
150 	}
151 
152 	/**
153 	 * <p>
154 	 * Information about point in the map. Together these form a directed graph
155 	 * that spans the entire map.
156 	 * </p>
157 	 * <p>
158 	 * Type: Once
159 	 * </p>
160 	 * 
161 	 * <p>
162 	 * Syntax: navPoint(UnrealID, location(X,Y,Z), [NeigsUnrealID])
163 	 * <ol>
164 	 * <li>UnrealID: The unique id of this navpoint.</li>
165 	 * <li>Location: The location of this navpoint in the map.</li>
166 	 * <li>[NeigsUnrealID]: A list of Id's for the neighbouring navpoints that
167 	 * are reachable from this navpoint.</li>
168 	 * </ol>
169 	 * </p>
170 	 * 
171 	 * 
172 	 */
173 	@AsPercept(name = "navPoint", multiplePercepts = true, filter = Type.ONCE)
174 	public Collection<Percept> navPoint() {
175 		Collection<NavPoint> navPoints = getWorldView().getAll(NavPoint.class)
176 				.values();
177 		List<Percept> percepts = new ArrayList<Percept>(navPoints.size());
178 
179 		for (NavPoint p : navPoints) {
180 			percepts.add(new Percept(p.getId(), p.getLocation(), p
181 					.getOutgoingEdges().keySet()));
182 		}
183 
184 		return percepts;
185 	}
186 
187 	/**
188 	 * <p>
189 	 * Percept provided when another bot becomes visible to this bot.
190 	 * </p>
191 	 * 
192 	 * <p>
193 	 * Type: On change with negation.
194 	 * </p>
195 	 * 
196 	 * <p>
197 	 * Syntax: bot(UnrealId, Team, location(X,Y,Z), Weapon, FireMode)
198 	 * </p>
199 	 * 
200 	 * <ul>
201 	 * <li>UnrealId: Unique identifier for this bot assigned by Unreal.</li>
202 	 * <li>Team: Either red or blue.</li>
203 	 * <li>location(X,Y,Z): Location of the bot in the world.</li>
204 	 * <li>Weapon: The weapon the bot is holding. TODO: Any of the following:</li>
205 	 * <li>FireMode: Mode of shooting, either primary, secondary or none.</li>
206 	 * </ul>
207 	 * </p>
208 	 * 
209 	 * 
210 	 */
211 	@AsPercept(name = "bot", multiplePercepts = true, filter = Type.ON_CHANGE_NEG)
212 	public Collection<Percept> bot() {
213 		Collection<Player> visible = getWorldView().getAll(Player.class)
214 				.values();
215 		Collection<Percept> wrapped = new ArrayList<Percept>(visible.size());
216 
217 		for (Player p : visible) {
218 			wrapped.add(new Percept(p.getId(), p.getName(), Team.valueOf(p
219 					.getTeam()), p.getLocation(), UT2004ItemType.getItemType(p
220 					.getWeapon()), FireMode.valueOf(p.getFiring())));
221 		}
222 
223 		return wrapped;
224 	}
225 
226 }