@@ -48,18 +48,18 @@ public final strictfp class GameMapIO {
4848 * @return LiveMap for map
4949 * @throws IOException if the map fails to load or can't be found.
5050 */
51- public static LiveMap loadMap (String mapName , File mapDir ) throws IOException {
51+ public static LiveMap loadMap (String mapName , File mapDir , boolean teamsReversed ) throws IOException {
5252 final LiveMap result ;
5353
5454 final File mapFile = new File (mapDir , mapName + MAP_EXTENSION );
5555 if (mapFile .exists ()) {
56- result = loadMap (new FileInputStream (mapFile ));
56+ result = loadMap (new FileInputStream (mapFile ), teamsReversed );
5757 } else {
5858 final InputStream backupStream = BACKUP_LOADER .getResourceAsStream (DEFAULT_MAP_PACKAGE + mapName + MAP_EXTENSION );
5959 if (backupStream == null ) {
6060 throw new IOException ("Can't load map: " + mapName + " from dir " + mapDir + " or default maps." );
6161 }
62- result = loadMap (backupStream );
62+ result = loadMap (backupStream , teamsReversed );
6363 }
6464
6565 if (!result .getMapName ().equals (mapName )) {
@@ -73,7 +73,7 @@ public static LiveMap loadMap(String mapName, File mapDir) throws IOException {
7373
7474 public static LiveMap loadMapAsResource (final ClassLoader loader ,
7575 final String mapPackage ,
76- final String map ) throws IOException {
76+ final String map , final boolean teamsReversed ) throws IOException {
7777 final InputStream mapStream = loader .getResourceAsStream (
7878 mapPackage + (mapPackage .endsWith ("/" )? "" : "/" ) +
7979 map + MAP_EXTENSION
@@ -83,7 +83,7 @@ public static LiveMap loadMapAsResource(final ClassLoader loader,
8383 throw new IOException ("Can't load map: " + map + " from package " + mapPackage );
8484 }
8585
86- final LiveMap result = loadMap (mapStream );
86+ final LiveMap result = loadMap (mapStream , teamsReversed );
8787
8888 if (!result .getMapName ().equals (map )) {
8989 throw new IOException ("Invalid map: name (" + result .getMapName ()
@@ -101,8 +101,8 @@ public static LiveMap loadMapAsResource(final ClassLoader loader,
101101 * @return a map read from the stream
102102 * @throws IOException if the read fails somehow
103103 */
104- public static LiveMap loadMap (InputStream stream ) throws IOException {
105- return Serial .deserialize (IOUtils .toByteArray (stream ));
104+ public static LiveMap loadMap (InputStream stream , boolean teamsReversed ) throws IOException {
105+ return Serial .deserialize (IOUtils .toByteArray (stream ), teamsReversed );
106106 }
107107
108108 /**
@@ -189,12 +189,12 @@ public static class Serial {
189189 * @param mapBytes the raw bytes of the map
190190 * @return a new copy of the map as a LiveMap
191191 */
192- public static LiveMap deserialize (byte [] mapBytes ) {
192+ public static LiveMap deserialize (byte [] mapBytes , boolean teamsReversed ) {
193193 battlecode .schema .GameMap rawMap = battlecode .schema .GameMap .getRootAsGameMap (
194194 ByteBuffer .wrap (mapBytes )
195195 );
196196
197- return Serial .deserialize (rawMap );
197+ return Serial .deserialize (rawMap , teamsReversed );
198198 }
199199
200200 /**
@@ -219,7 +219,7 @@ public static byte[] serialize(LiveMap gameMap) {
219219 * @param raw the flatbuffer map pointer
220220 * @return a new copy of the map as a LiveMap
221221 */
222- public static LiveMap deserialize (battlecode .schema .GameMap raw ) {
222+ public static LiveMap deserialize (battlecode .schema .GameMap raw , boolean teamsReversed ) {
223223 final int width = (int ) (raw .maxCorner ().x () - raw .minCorner ().x ());
224224 final int height = (int ) (raw .maxCorner ().y () - raw .minCorner ().y ());
225225 final MapLocation origin = new MapLocation ((int ) raw .minCorner ().x (), (int ) raw .minCorner ().y ());
@@ -243,7 +243,7 @@ public static LiveMap deserialize(battlecode.schema.GameMap raw) {
243243
244244 ArrayList <RobotInfo > initBodies = new ArrayList <>();
245245 SpawnedBodyTable bodyTable = raw .bodies ();
246- initInitialBodiesFromSchemaBodyTable (bodyTable , initBodies );
246+ initInitialBodiesFromSchemaBodyTable (bodyTable , initBodies , teamsReversed );
247247
248248 RobotInfo [] initialBodies = initBodies .toArray (new RobotInfo [initBodies .size ()]);
249249
@@ -335,7 +335,7 @@ public static int serialize(FlatBufferBuilder builder, LiveMap gameMap) {
335335 // *** HELPER METHODS *********
336336 // ****************************
337337
338- private static void initInitialBodiesFromSchemaBodyTable (SpawnedBodyTable bodyTable , ArrayList <RobotInfo > initialBodies ) {
338+ private static void initInitialBodiesFromSchemaBodyTable (SpawnedBodyTable bodyTable , ArrayList <RobotInfo > initialBodies , boolean teamsReversed ) {
339339 VecTable locs = bodyTable .locs ();
340340 for (int i = 0 ; i < bodyTable .robotIDsLength (); i ++) {
341341 // all initial bodies should be headquarters
@@ -344,11 +344,14 @@ private static void initInitialBodiesFromSchemaBodyTable(SpawnedBodyTable bodyTa
344344 int bodyX = locs .xs (i );
345345 int bodyY = locs .ys (i );
346346 Team bodyTeam = TeamMapping .team (bodyTable .teamIDs (i ));
347+ if (teamsReversed ) {
348+ bodyTeam = bodyTeam .opponent ();
349+ }
347350 if (bodyType == RobotType .HEADQUARTERS ) {
348351 Inventory headquarterInventory = new Inventory ();
349352 initialBodies .add (new RobotInfo (bodyID , bodyTeam , bodyType , headquarterInventory , RobotType .HEADQUARTERS .health , new MapLocation (bodyX , bodyY )));
350353 }
351- // ignore robots that are not archons , TODO throw error?
354+ // ignore robots that are not headquarters , TODO throw error?
352355 }
353356 }
354357 }
0 commit comments