View Javadoc

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