( index, (Mounted) iter.nextElement(), entity );
( index, (Mounted) iter2.next(), entity );
    public static void encode( Entity entity, Writer out )        throws IOException    {        Enumeration iter; // used when marching through a list of sub-elements        Coords coords;        int turns;        String substr;        // First, validate our input.        if ( null == entity ) {            throw new IllegalArgumentException( "The entity is null." );        }        if ( null == out ) {            throw new IllegalArgumentException( "The writer is null." );        }        // Make sure any transported entities are written first.        iter = entity.getLoadedUnits().elements();        while( iter.hasMoreElements() ) {            EntityEncoder.encode( (Entity) iter.nextElement(), out );        }        // Start writing this entity to the file.        out.write( "<entity chassis=\"" );        out.write( entity.getChassis() );        out.write( "\" model=\"" );        out.write( entity.getModel() );        out.write( "\" type=\"" );        out.write( entity.getMovementModeAsString() );        out.write( "\" typeVal=\"" );        out.write( String.valueOf(entity.getMovementMode()) );        out.write( "\" techBase=\"" );        out.write ( entity.getTechLevel() + ":" + TechConstants.getLevelName(entity.getTechLevel()) );        out.write( "\" year=\"" );        out.write( String.valueOf(entity.getYear()) );        out.write( "\" mass=\"" );        out.write( String.valueOf(entity.getWeight()) );        out.write( "\" walkMp=\"" );        out.write( String.valueOf(entity.getOriginalWalkMP()) );        out.write( "\" jumpMp=\"" );        out.write( String.valueOf(entity.getOriginalJumpMP()) );        out.write( "\">" );        // Add the crew this entity.        final Pilot crew = entity.getCrew();        out.write( "<pilot name=\"" );        out.write( crew.getName() );        out.write( "\" gunnery=\"" );        out.write( String.valueOf(crew.getGunnery()) );        out.write( "\" piloting=\"" );        out.write( String.valueOf(crew.getPiloting()) );        if ( crew.isDead() || crew.getHits() > 5 ) {            out.write( "\" hits=\"Dead" );        }        else if ( crew.getHits() > 0 ) {            out.write( "\" hits=\"" );            out.write( String.valueOf(crew.getHits()) );        }        if ( crew.countAdvantages() > 0 ) {            out.write( "\" advantages=\"" );            out.write( String.valueOf(crew.getAdvantageList(" ")) );        }        out.write( "\"/>" );        // Write the game-specific data.        out.write( "<entityData gameId=\"" );        out.write( String.valueOf(entity.getId()) );        out.write( "\" externalId=\"" );        out.write( String.valueOf(entity.getExternalId()) );        out.write( "\" ownerId=\"" );        out.write( String.valueOf(entity.getOwnerId()) );        out.write( "\" facing=\"" );        out.write( String.valueOf(entity.getFacing()) );        out.write( "\" secondaryFacing=\"" );        out.write( String.valueOf(entity.getSecondaryFacing()) );        out.write( "\" walkMpCurrent=\"" );        out.write( String.valueOf(entity.getWalkMP()) );        out.write( "\" isOmni=\"" );        out.write( entity.isOmni() ? "true" : "false" );        out.write( "\" jumpMpCurrent=\"" );        out.write( String.valueOf(entity.getJumpMP()) );        out.write( "\" C3MasterId=\"" );        out.write( String.valueOf(entity.getC3MasterId()) );        out.write( "\" transportId=\"" );        out.write( String.valueOf(entity.getTransportId()) );        out.write( "\" swarmTargetId=\"" );        out.write( String.valueOf(entity.getSwarmTargetId()) );        out.write( "\" swarmAttackerId=\"" );        out.write( String.valueOf(entity.getSwarmAttackerId()) );        out.write( "\" removalCondition=\"" );        out.write( String.valueOf(entity.getRemovalCondition()) );        out.write( "\" deployRound=\"" );        out.write( String.valueOf(entity.getDeployRound()) );        out.write( "\" isShutDown=\"" );        out.write( entity.isShutDown() ? "true" : "false" );        out.write( "\" isDoomed=\"" );        out.write( entity.isDoomed() ? "true" : "false" );        out.write( "\" isDestroyed=\"" );        out.write( entity.isDestroyed() ? "true" : "false" );        out.write( "\" isDone=\"" );        out.write( entity.isDone() ? "true" : "false" );        out.write( "\" isProne=\"" );        out.write( entity.isProne() ? "true" : "false" );        out.write( "\" isFindingClub=\"" );        out.write( entity.isFindingClub() ? "true" : "false" );        out.write( "\" isArmsFlipped=\"" );        out.write( entity.getArmsFlipped() ? "true" : "false" );        out.write( "\" isUnjammingRAC=\"" );        out.write( entity.isUnjammingRAC() ? "true" : "false" );        out.write( "\" isSpotting=\"" );        out.write( entity.isSpotting() ? "true" : "false" );        out.write( "\" isClearingMinefield=\"" );        out.write( entity.isClearingMinefield() ? "true" : "false" );        out.write( "\" isSalvage=\"" );        out.write( entity.isSalvage() ? "true" : "false" );        out.write( "\" isDeployed=\"" );        out.write( entity.isDeployed() ? "true" : "false" );        out.write( "\" isUnloadedThisTurn=\"" );        out.write( entity.isUnloadedThisTurn() ? "true" : "false" );        out.write( "\" heat=\"" );        out.write( String.valueOf(entity.heat) );        out.write( "\" heatBuildup=\"" );        out.write( String.valueOf(entity.heatBuildup) );        out.write( "\" delta_distance=\"" );        out.write( String.valueOf(entity.delta_distance) );        out.write( "\" mpUsed=\"" );        out.write( String.valueOf(entity.mpUsed) );        out.write( "\" moved=\"" );        out.write( String.valueOf(entity.moved) );        out.write( "\" damageThisPhase=\"" );        out.write( String.valueOf(entity.damageThisPhase) );        out.write( "\" engineHitsThisRound=\"" );        out.write( String.valueOf(entity.engineHitsThisRound) );        out.write( "\" rolledForEngineExplosion=\"" );        out.write( entity.rolledForEngineExplosion ? "true" : "false" );        out.write( "\" dodging=\"" );        out.write( entity.dodging ? "true" : "false" );        out.write( "\" >" );        // Now save the entity's coordinates.        coords = entity.getPosition();        if ( null != coords ) CoordsEncoder.encode( coords, out );        // Is the entity performing a displacement attack?        if ( entity.hasDisplacementAttack() ) {            EntityActionEncoder.encode( entity.getDisplacementAttack(), out );        }        // Add the narc pods attached to this entity (if any are needed).        substr = getNarcString( entity );        if ( null != substr ) out.write( substr );        // Encode the infernos burning on this entity.        if ( entity.infernos.isStillBurning() ) {            // Encode the infernos on this entity.            out.write( "<inferno>" );            turns = entity.infernos.getArrowIVTurnsLeftToBurn();            // This value may be zero.            if ( turns > 0 ) {                out.write( "<arrowiv turns=\"" );                out.write( String.valueOf(turns) );                out.write( "\" />" );            }            // -(Arrow IV turns - All Turns) = Standard Turns.            turns -= entity.infernos.getTurnsLeftToBurn();            turns = -turns;            if ( turns > 0 ) {                out.write( "<standard turns=\"" );                out.write( String.valueOf(turns) );                out.write( "\" />" );            }            out.write( "</inferno>" );        }        // Do we have any transporters?        String transporters = Entity.encodeTransporters( entity );        if ( null != transporters && 0 == transporters.length() ) {            out.write( "<transporters value=\"" );            out.write( transporters );            out.write( "\" />" );        }        // Record the IDs of all transported units (if any).        iter = entity.getLoadedUnits().elements();        if ( iter.hasMoreElements() ) {            out.write( "<loadedUnits>" );            while ( iter.hasMoreElements() ) {                Entity loaded = (Entity) iter.nextElement();                out.write( "<entityRef gameId=\"" );                out.write( String.valueOf(loaded.getId()) );                out.write( "\" />" );            }            out.write( "</loadedUnits>" );        }        // Handle sub-classes of Entity.        out.write( "<class name=\"" );        if ( entity instanceof BipedMech ) {            out.write( "BipedMech\">" );            BipedMechEncoder.encode( entity, out );        }        else if ( entity instanceof QuadMech ) {            out.write( "QuadMech\">" );            QuadMechEncoder.encode( entity, out );        }        else if ( entity instanceof Tank ) {            out.write( "Tank\">" );            TankEncoder.encode( entity, out );        }        else if ( entity instanceof BattleArmor ) {            out.write( "BattleArmor\">" );            BattleArmorEncoder.encode( entity, out );        }        else if ( entity instanceof Infantry ) {            out.write( "Infantry\">" );            InfantryEncoder.encode( entity, out );        }        else if ( entity instanceof Protomech ) {            out.write( "Protomech\">" );            ProtomechEncoder.encode( entity, out );        }        else if ( entity instanceof GunEmplacement ) {            out.write( "GunEmplacement\">" );            GunEmplacementEncoder.encode( entity, out );        }        else {            throw new IllegalStateException                ( "Unexpected entity type " + entity.getClass().getName() );        }        out.write( "</class>" );                // Finish the game-specific data.        out.write( "</entityData>" );        // Encode this unit's equipment.        iter = entity.getEquipment();        if ( iter.hasMoreElements() ) {            out.write( "<entityEquipment>" );            int index = 0;            while ( iter.hasMoreElements() ) {                substr = EntityEncoder.formatEquipment                    ( index, (Mounted) iter.nextElement(), entity );                if ( null != substr ) out.write( substr );                index++;            }            out.write( "</entityEquipment>" );        }        // Add the locations of this entity (if any are needed).        substr = getLocString( entity );        if ( null != substr ) out.write( substr );        // Finish the XML stream for this entity.        out.write( "</entity>" );    }