📄 entitylistfile.java
字号:
/* * MegaMek - Copyright (C) 2003, 2004, 2005 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;import gd.xml.ParseException;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStreamWriter;import java.io.Writer;import java.util.Enumeration;import java.util.Vector;/** * This class provides static methods to save a list of <code>Entity</code>s * to, and load a list of <code>Entity</code>s from a file. */public class EntityListFile { /** * Produce a string describing this armor value. Valid output values * are any integer from 0 to 100, N/A, or Destroyed. * * @param points - the <code>int</code> value of the armor. This * value may be any valid value of entity armor (including * NA, DOOMED, and DESTROYED). * @return a <code>String</code> that matches the armor value. */ private static String formatArmor(int points) { // Is the armor destroyed or doomed? if (points == IArmorState.ARMOR_DOOMED || points == IArmorState.ARMOR_DESTROYED) { return "Destroyed"; } // Was there armor to begin with? if (points == IArmorState.ARMOR_NA) { return "N/A"; } // Translate the int to a String. return String.valueOf(points); } /** * Produce a string describing the equipment in a critical slot. * * @param index - the <code>String</code> index of the slot. This * value should be a positive integer or "N/A". * @param mount - the <code>Mounted</code> object of the equipment. * This value should be <code>null</code> for a slot with * system equipment. * @param isHit - a <code>boolean</code> that identifies this slot * as having taken a hit. * @param isDestroyed - a <code>boolean</code> that identifies the * equipment as having been destroyed. Note that a single * slot in a multi-slot piece of equipment can be destroyed * but not hit; it is still available to absorb additional * critical hits. * @return a <code>String</code> describing the slot. */ private static String formatSlot(String index, Mounted mount, boolean isHit, boolean isDestroyed) { StringBuffer output = new StringBuffer(); output.append(" <slot index=\""); output.append(index); output.append("\" type=\""); if (mount == null) { output.append("System"); } else { output.append(mount.getType().getInternalName()); if (mount.isRearMounted()) { output.append("\" isRear=\"true"); } if (mount.getType() instanceof AmmoType) { output.append("\" shots=\""); output.append(String.valueOf (mount.getShotsLeft())); } if (mount.getType() instanceof WeaponType && (mount.getType()).hasFlag(WeaponType.F_ONESHOT)) { output.append("\" munition=\""); output.append(mount.getLinked().getType().getInternalName()); } } if (isHit) { output.append("\" isHit=\""); output.append(String.valueOf(isHit)); } output.append("\" isDestroyed=\""); output.append(String.valueOf(isDestroyed)); output.append("\"/>"); output.append(CommonConstants.NL); // Return a String. return output.toString(); } /** * Helper function that generates a string identifying the state of * the locations for an entity. * * @param entity - the <code>Entity</code> whose location state is needed */ private static String getLocString(Entity entity) { boolean isMech = entity instanceof Mech; boolean haveSlot = false; StringBuffer output = new StringBuffer(); StringBuffer thisLoc = new StringBuffer(); boolean isDestroyed = false; boolean blownOff = false; // Walk through the locations for the entity, // and only record damage and ammo. for (int loc = 0; loc < entity.locations(); loc++) { // Record destroyed locations. if (entity.getOInternal(loc) != IArmorState.ARMOR_NA && entity.getInternal(loc) <= 0) { isDestroyed = true; } // Record damage to armor and internal structure. // Destroyed locations have lost all their armor and IS. if (!isDestroyed) { if (entity.getOArmor(loc) != entity.getArmor(loc)) { thisLoc.append(" <armor points=\""); thisLoc.append(formatArmor(entity.getArmor(loc))); thisLoc.append("\"/>"); thisLoc.append(CommonConstants.NL); } if (entity.getOInternal(loc) != entity.getInternal(loc)) { thisLoc.append(" <armor points=\""); thisLoc.append(formatArmor(entity.getInternal(loc))); thisLoc.append("\" type=\"Internal\"/>"); thisLoc.append(CommonConstants.NL); } if (entity.hasRearArmor(loc) && entity.getOArmor(loc, true) != entity.getArmor(loc, true)) { thisLoc.append(" <armor points=\""); thisLoc.append(formatArmor(entity.getArmor(loc, true))); thisLoc.append("\" type=\"Rear\"/>"); thisLoc.append(CommonConstants.NL); } } // Walk through the slots in this location. for (int loop = 0; loop < entity.getNumberOfCriticals(loc); loop++) { // Get this slot. CriticalSlot slot = entity.getCritical(loc, loop); // Did we get a slot? if (null == slot) { // Nope. Record missing actuators on Biped Mechs. if (isMech && !entity.entityIsQuad() && (loc == Mech.LOC_RARM || loc == Mech.LOC_LARM) && (loop == 2 || loop == 3)) { thisLoc.append(" <slot index=\""); thisLoc.append(String.valueOf(loop + 1)); thisLoc.append("\" type=\"Empty\"/>"); thisLoc.append(CommonConstants.NL); haveSlot = true; } } else { // Yup. If the equipment isn't a system, get it. Mounted mount = null; if (CriticalSlot.TYPE_EQUIPMENT == slot.getType()) { mount = entity.getEquipment(slot.getIndex()); } // Destroyed locations on Mechs that contain slots // that are missing but not hit or destroyed must // have been blown off. if (isDestroyed && isMech && slot.isMissing() && !slot.isHit() && !slot.isDestroyed()) { thisLoc.append(formatSlot(String.valueOf(loop + 1), mount, slot.isHit(), slot.isDestroyed())); haveSlot = true; blownOff = true; } // Record damaged slots in undestroyed locations. else if (!isDestroyed && slot.isDamaged()) { thisLoc.append(formatSlot(String.valueOf(loop + 1), mount, slot.isHit(), slot.isDestroyed())); haveSlot = true; } // Record ammunition slots in undestroyed locations. // N.B. the slot CAN\"T be damaged at this point. else if (!isDestroyed && mount != null && mount.getType() instanceof AmmoType) { thisLoc.append(" <slot index=\""); thisLoc.append(String.valueOf(loop + 1)); thisLoc.append("\" type=\""); thisLoc.append(mount.getType().getInternalName()); thisLoc.append("\" shots=\""); thisLoc.append(String.valueOf(mount.getShotsLeft())); thisLoc.append("\"/>"); thisLoc.append(CommonConstants.NL); haveSlot = true; } // Record the munition type of oneshot launchers else if (!isDestroyed && mount != null && mount.getType() instanceof WeaponType && (mount.getType()).hasFlag(WeaponType.F_ONESHOT)) { thisLoc.append(formatSlot(String.valueOf(loop + 1), mount, slot.isHit(), slot.isDestroyed())); haveSlot = true; } } // End have-slot } // Check the next slot in this location // Tanks don't have slots, and Protomechs only have // system slots, so we have to handle the ammo specially. if (entity instanceof Tank || entity instanceof Protomech) { for (Mounted mount : entity.getAmmo()) { // Is this ammo in the current location? if (mount.getLocation() == loc) { thisLoc.append(formatSlot("N/A",
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -