📄 entityencoder.java
字号:
/* * MegaMek - Copyright (C) 2003, 2004 Ben Mazur (bmazur@sev.org) * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation; either version 2 of the License, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * for more details. */package megamek.common.xml;import gd.xml.tiny.ParsedXML;import megamek.common.AmmoType;import megamek.common.BattleArmor;import megamek.common.BipedMech;import megamek.common.Coords;import megamek.common.CriticalSlot;import megamek.common.Entity;import megamek.common.GunEmplacement;import megamek.common.IArmorState;import megamek.common.IGame;import megamek.common.Infantry;import megamek.common.InfernoTracker;import megamek.common.Mech;import megamek.common.Mounted;import megamek.common.Pilot;import megamek.common.Player;import megamek.common.Protomech;import megamek.common.QuadMech;import megamek.common.Tank;import megamek.common.TechConstants;import java.io.IOException;import java.io.Writer;import java.util.Enumeration;import java.util.Iterator;import java.util.Vector;/** * Objects of this class can encode a <code>Entity</code> object as XML * into an output writer and decode one from a parsed XML node. It is used * when saving games into a version- neutral format. * * @author James Damour <suvarov454@users.sourceforge.net> */public class EntityEncoder { /** * Encode a <code>Entity</code> object to an output writer. * * @param entity - the <code>Entity</code> to be encoded. * This value must not be <code>null</code>. * @param out - the <code>Writer</code> that will receive the XML. * This value must not be <code>null</code>. * @throws IllegalArgumentException if the entity is * <code>null</code>. * @throws IOException if there's any error on write. */ 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. Iterator iter2 = entity.getEquipment().iterator(); if ( iter2.hasNext() ) { out.write( "<entityEquipment>" ); int index = 0; while ( iter2.hasNext() ) { substr = EntityEncoder.formatEquipment ( index, (Mounted) iter2.next(), 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>" ); } /** * Produce a string describing all NARC pods on the entity. * * @param entity - the <code>Entity</code> being examined. * This value may be <code>null</code>. * @return a <code>String</code> describing the equipment. * This value may be <code>null</code>. */ private static String getNarcString( Entity entity ) { // null in, null out if ( null == entity ) return null; // Show all teams that have NARCed the entity. StringBuffer output = new StringBuffer(); boolean narced = false; for ( int team = Player.TEAM_NONE; team < Player.MAX_TEAMS; team++ ) { if ( entity.isNarcedBy( team ) ) { // Is this the first narc on the entity? if ( !narced ) { output.append( "<narcs>" ); narced = true; } // Add this team to the NARC pods. output.append( "<narc type=\"Standard\" team=\"" ); output.append( String.valueOf( team ) ); output.append( "\" />" ); } } // If the entity wasn't narced, return a null. if ( !narced ) return null; // Finish off this section, and return the string. output.append( "</narcs>" ); return output.toString(); } /** * Produce a string describing a piece of equipment. * * @param index - the <code>int</code> index of this equipment * on the given entity. * @param mount - the <code>Mounted</code> object of the equipment. * This value should not be <code>null</code>. * @param entity - the <code>Entity</code> that has this mount. * @return a <code>String</code> describing the equipment. * This value will be <code>null</code> if a <code>null</code>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -