View Javadoc

1   package cz.cuni.amis.pogamut.defcon.consts;
2   
3   import cz.cuni.amis.pogamut.defcon.base3d.worldview.object.DefConLocation;
4   import cz.cuni.amis.pogamut.defcon.communication.messages.infos.*;
5   import cz.cuni.amis.pogamut.defcon.consts.state.*;
6   
7   import java.util.EnumSet;
8   import java.util.HashMap;
9   import java.util.Map;
10  
11  import javabot.JBot;
12  
13  import javabot.JBot.UnitData;
14  
15  /**
16       * Contains all types of defcon units and supplies user with a factory method for each of them.
17       * GENERATED by GameObjectsFactoryGenerator.xslt so any modifications should go there!!!     
18       * 
19       * @author Radek 'Black_Hand' Pibil          
20       */
21  public enum UnitType {INVALID(JBot.TypeInvalid), CITY(JBot.TypeCity), SILO(JBot.TypeSilo), 
22      RADAR(JBot.TypeRadarStation), NUKE(JBot.TypeNuke), EXPLOSION(JBot.TypeExplosion), 
23      SUB(JBot.TypeSub), BATTLE_SHIP(JBot.TypeBattleShip), AIR_BASE(JBot.TypeAirBase), 
24      FIGHTER(JBot.TypeFighter), BOMBER(JBot.TypeBomber), CARRIER(JBot.TypeCarrier), 
25      TORNADO(JBot.TypeTornado), SAUCER(JBot.TypeSaucer), FLEET(JBot.TypeFleet), 
26      GUNSHOT(JBot.TypeGunshot), QUEUE_ITEM(JBot.TypeQueueItem), NUM_OBJECTS(18);
27      /**
28       * Contains mapping from integer (defcon) unit type to UnitType enum. Used when
29       * translation from "int" to UnitType is needed, i.e., when {@link UnitType#getEnum(int)} is
30       * called.
31       */
32      private static Map<Integer, UnitType> enums = new HashMap<Integer, UnitType>();
33  
34      /**
35       * Returns appropriate UnitType for given integer.
36       *
37       * @param id integer (defcon) id type
38       *
39       * @return UnitType type
40       */
41      public static UnitType getEnum(int id) {
42          return enums.get(id);
43      }
44  
45      static {
46          for (UnitType item : UnitType.values()) {
47              enums.put(item.id, item);
48          }
49      }
50  
51      /**
52       * Holds integer (defcon) unit type of this unit..type.
53       */
54      public final int id;
55  
56      /**
57       * Contains all units that can receive commands.<p></p>
58       *  <p>SILO, RADAR, NUKE, SUB, BATTLE_SHIP, AIR_BASE, FIGHTER, BOMBER, CARRIER, FLEET
59       * TODO: should NUKE be here?</p>
60       */
61      public static final EnumSet<UnitType> units;
62  
63      /**
64       * Contains all air units that can receive commands.<p></p>
65       *  <p>NUKE, FIGHTER, BOMBER TODO: should NUKE be here?</p>
66       */
67      public static final EnumSet<UnitType> air;
68  
69      /**
70       * Contains all naval units that can receive commands.<p></p>
71       *  <p>SUB, BATTLE_SHIP, CARRIER, FLEET</p>
72       */
73      public static final EnumSet<UnitType> naval;
74  
75      /**
76       * Contains all ground units that can receive commands.<p></p>
77       *  <p>SILO, RADAR, AIR_BASE</p>
78       */
79      public static final EnumSet<UnitType> ground;
80  
81      /**
82       * All other types.<p></p>
83       *  <p>INVALID, CITY, EXPLOSION, TORNADO, SAUCER, GUNSHOT, QUEUE_ITEM, NUM_OBJECTS</p>
84       */
85      public static final EnumSet<UnitType> misc;
86  
87  /**
88         * Constructor for this enum.
89         *
90         * @param id integer (defcon) unittype
91         */
92      private UnitType(int id) {
93          this.id = id;
94      }
95  
96      static {
97          units = EnumSet.of(SILO, RADAR, NUKE, SUB, BATTLE_SHIP, AIR_BASE, FIGHTER, BOMBER, CARRIER,
98                  FLEET);
99          air = EnumSet.of(NUKE, FIGHTER, BOMBER);
100         naval = EnumSet.of(SUB, BATTLE_SHIP, CARRIER, FLEET);
101         ground = EnumSet.of(SILO, RADAR, AIR_BASE);
102         misc = EnumSet.of(INVALID, CITY, EXPLOSION, TORNADO, SAUCER, GUNSHOT, QUEUE_ITEM,
103                 NUM_OBJECTS);
104     }
105 
106     /**
107      * May the unit receive commands?<p></p>
108      *  <p>SILO, RADAR, NUKE, SUB, BATTLE_SHIP, AIR_BASE, FIGHTER, BOMBER, CARRIER, FLEET</p>
109      *
110      * @return
111      */
112     public boolean isUnit() {
113         return units.contains(this);
114     }
115 
116     /**
117      * Is the unit airborne?<p></p>
118      *  <p>NUKE, FIGHTER, BOMBER</p>
119      *
120      * @return
121      */
122     public boolean isAir() {
123         return air.contains(this);
124     }
125 
126     /**
127      * Is it ground unit?<p></p>
128      *  <p>SILO, RADAR, AIR_BASE</p>
129      *
130      * @return
131      */
132     public boolean isGround() {
133         return ground.contains(this);
134     }
135 
136     /**
137      * Is it naval unit?<p></p>
138      *  <p>SUB, BATTLE_SHIP, CARRIER, FLEET</p>
139      *
140      * @return
141      */
142     public boolean isNaval() {
143         return naval.contains(this);
144     }
145 
146     /**
147      * Is it misc?<p></p>
148      *  <p>INVALID, CITY, EXPLOSION, TORNADO, SAUCER, GUNSHOT, QUEUE_ITEM, NUM_OBJECTS</p>
149      *
150      * @return
151      */
152     public boolean isMisc() {
153         return misc.contains(this);
154     }
155 
156     /**
157      * Factory styled method, which provides you with an appropriate instance of unit
158      * object based on the given UnitData data.
159      *
160      * @param data unit data
161      * @param time time of creation
162      *
163      * @return instantiated defcon unit
164      */
165     public static DefConObject getInstanceOfUnitTypeFromUnitData(UnitData data, double time) {
166         try {
167             switch (UnitType.getEnum(data.m_type)) {
168             case AIR_BASE:return new AirBase(data.m_objectId, data.m_teamId,
169                     new DefConLocation(data.m_longitude, data.m_latitude), data.m_visible,
170                     AirBaseState.getState(data.m_currentState),
171                     JBot.GetStateCount(data.m_objectId, data.m_currentState),
172                     JBot.GetCurrentTargetId(data.m_objectId), JBot.GetNukeSupply(data.m_objectId),
173                     JBot.GetStateCount(data.m_objectId, AirBaseState.FIGHTER_LAUNCH.getStateId()),
174                     JBot.GetStateCount(data.m_objectId, AirBaseState.BOMBER_LAUNCH.getStateId()),
175                     time);
176 
177             case BATTLE_SHIP:return new Battleship(data.m_objectId, data.m_teamId,
178                     new DefConLocation(data.m_longitude, data.m_latitude), data.m_visible,
179                     BattleshipState.getState(data.m_currentState),
180                     JBot.GetStateCount(data.m_objectId, data.m_currentState),
181                     JBot.GetCurrentTargetId(data.m_objectId), time);
182 
183             case BOMBER:return new Bomber(data.m_objectId, data.m_teamId,
184                     new DefConLocation(data.m_longitude, data.m_latitude), data.m_visible,
185                     BomberState.getState(data.m_currentState),
186                     JBot.GetStateCount(data.m_objectId, data.m_currentState),
187                     JBot.GetCurrentTargetId(data.m_objectId),
188                     new DefConLocation(JBot.GetBomberNukeTarget(data.m_objectId)), time);
189 
190             case CARRIER:return new Carrier(data.m_objectId, data.m_teamId,
191                     new DefConLocation(data.m_longitude, data.m_latitude), data.m_visible,
192                     CarrierState.getState(data.m_currentState),
193                     JBot.GetStateCount(data.m_objectId, data.m_currentState),
194                     JBot.GetCurrentTargetId(data.m_objectId), JBot.GetNukeSupply(data.m_objectId),
195                     JBot.GetStateCount(data.m_objectId, CarrierState.FIGHTER_LAUNCH.getStateId()),
196                     JBot.GetStateCount(data.m_objectId, CarrierState.BOMBER_LAUNCH.getStateId()),
197                     time);
198 
199             case CITY:return new City(data.m_objectId, data.m_teamId,
200                     new DefConLocation(data.m_longitude, data.m_latitude), data.m_visible,
201                     JBot.GetCityPopulation(data.m_objectId), time);
202 
203             case EXPLOSION:return new Explosion(data.m_objectId, data.m_teamId,
204                     new DefConLocation(data.m_longitude, data.m_latitude), data.m_visible, time);
205 
206             case FIGHTER:return new Fighter(data.m_objectId, data.m_teamId,
207                     new DefConLocation(data.m_longitude, data.m_latitude), data.m_visible,
208                     FighterState.getState(data.m_currentState),
209                     JBot.GetStateCount(data.m_objectId, data.m_currentState),
210                     JBot.GetCurrentTargetId(data.m_objectId), time);
211 
212             case FLEET:return new Fleet(data.m_objectId, data.m_teamId,
213                     new DefConLocation(data.m_longitude, data.m_latitude), data.m_visible,
214                     JBot.GetFleetMembers(data.m_objectId), time);
215 
216             case GUNSHOT:return new Gunshot(data.m_objectId, data.m_teamId,
217                     new DefConLocation(data.m_longitude, data.m_latitude), data.m_visible, time);
218 
219             case INVALID:return new InvalidObject(data.m_objectId, time);
220 
221             case NUKE:return new Nuke(data.m_objectId, data.m_teamId,
222                     new DefConLocation(data.m_longitude, data.m_latitude), data.m_visible,
223                     NukeState.getState(data.m_currentState),
224                     JBot.GetStateCount(data.m_objectId, data.m_currentState),
225                     JBot.GetCurrentTargetId(data.m_objectId), time);
226 
227             case QUEUE_ITEM:return new QueueItem(data.m_objectId, time);
228 
229             case RADAR:return new Radar(data.m_objectId, data.m_teamId,
230                     new DefConLocation(data.m_longitude, data.m_latitude), data.m_visible,
231                     RadarState.getState(data.m_currentState),
232                     JBot.GetStateCount(data.m_objectId, data.m_currentState),
233                     JBot.GetCurrentTargetId(data.m_objectId), time);
234 
235             case SAUCER:return new Saucer(data.m_objectId, data.m_teamId,
236                     new DefConLocation(data.m_longitude, data.m_latitude), data.m_visible, time);
237 
238             case SILO:return new Silo(data.m_objectId, data.m_teamId,
239                     new DefConLocation(data.m_longitude, data.m_latitude), data.m_visible,
240                     SiloState.getState(data.m_currentState),
241                     JBot.GetStateCount(data.m_objectId, data.m_currentState),
242                     JBot.GetCurrentTargetId(data.m_objectId), JBot.GetNukeSupply(data.m_objectId),
243                     time);
244 
245             case SUB:return new Sub(data.m_objectId, data.m_teamId,
246                     new DefConLocation(data.m_longitude, data.m_latitude), data.m_visible,
247                     SubState.getState(data.m_currentState),
248                     JBot.GetStateCount(data.m_objectId, data.m_currentState),
249                     JBot.GetCurrentTargetId(data.m_objectId), JBot.GetNukeSupply(data.m_objectId),
250                     time);
251 
252             case TORNADO:return new Tornado(data.m_objectId, data.m_teamId,
253                     new DefConLocation(data.m_longitude, data.m_latitude), data.m_visible, time);
254             }
255         } catch (Exception e) {
256         }
257 
258         return null;
259     }
260 
261     /**
262      * Provides mapping from UnitType to DefConObject class
263      *
264      * @return class of unit
265      */
266     public Class<?extends DefConObject> getClassOfUnitType() {
267         return getClassOfUnitType(this);
268     }
269 
270     /**
271      * Provides mapping from UnitType to DefConObject class
272      *
273      * @param data unit type
274      *
275      * @return class of unit
276      */
277     public static Class<?extends DefConObject> getClassOfUnitType(UnitType data) {
278         switch (data) {
279         case AIR_BASE:return AirBase.class;
280 
281         case BATTLE_SHIP:return Battleship.class;
282 
283         case BOMBER:return Bomber.class;
284 
285         case CARRIER:return Carrier.class;
286 
287         case CITY:return City.class;
288 
289         case EXPLOSION:return Explosion.class;
290 
291         case FIGHTER:return Fighter.class;
292 
293         case FLEET:return Fleet.class;
294 
295         case GUNSHOT:return Gunshot.class;
296 
297         case INVALID:return InvalidObject.class;
298 
299         case NUKE:return Nuke.class;
300 
301         case QUEUE_ITEM:return QueueItem.class;
302 
303         case RADAR:return Radar.class;
304 
305         case SAUCER:return Saucer.class;
306 
307         case SILO:return Silo.class;
308 
309         case SUB:return Sub.class;
310 
311         case TORNADO:return Tornado.class;
312         }
313 
314         return null;
315     }
316 }