/*
* UFO Saved Game Editor
* Copyright (C) 2010 Christopher Davoren
*
* 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 3 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.rubikscomplex.ufosge.data;
import java.io.EOFException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import net.rubikscomplex.ufosge.util.UFOInputStream;
import net.rubikscomplex.ufosge.util.UFOOutputStream;
/**
* Represents a single craft entry from a UFO saved game.
*
* This information is stored in the CRAFT.DAT saved game file. This file stores
* information for both human and alien craft, and some fields may only be used
* for one or the other, or may be used differently - see individual field
* documentation for specific behaviour.
*
* @author Chris Davoren
*/
public class Craft {
/**
* The number of item entries in a craft inventory.
*
* @see #items
*/
public static final int NUM_ITEMS = 55;
/**
* The size in bytes of the sixth unknown field in a craft entry.
*
* Fields 1 to 5 are all single numbers whose size corresponds to the their
* variable type.
*/
public static final int UNKNOWN6_SIZE = 5;
/**
* Constant representing craft type.
*
* @see #type
*/
public static final byte TYPE_SKYRANGER = 0;
/**
* Constant representing craft type.
*
* @see #type
*/
public static final byte TYPE_LIGHTNING = 1;
/**
* Constant representing craft type.
*
* @see #type
*/
public static final byte TYPE_AVENGER = 2;
/**
* Constant representing craft type.
*
* @see #type
*/
public static final byte TYPE_INTERCEPTOR = 3;
/**
* Constant representing craft type.
*
* @see #type
*/
public static final byte TYPE_FIRESTORM = 4;
/**
* Constant representing craft type.
*
* @see #type
*/
public static final byte TYPE_SMALLSCOUNT = 5;
/**
* Constant representing craft type.
*
* @see #type
*/
public static final byte TYPE_MEDIUMSCOUNT = 6;
/**
* Constant representing craft type.
*
* @see #type
*/
public static final byte TYPE_LARGESCOUT = 7;
/**
* Constant representing craft type.
*
* @see #type
*/
public static final byte TYPE_HARVESTER = 8;
/**
* Constant representing craft type.
*
* @see #type
*/
public static final byte TYPE_ABDUCTOR = 9;
/**
* Constant representing craft type.
*
* @see #type
*/
public static final byte TYPE_TERRORSHIP = 10;
/**
* Constant representing craft type.
*
* @see #type
*/
public static final byte TYPE_BATTLESHIP = 11;
/**
* Constant representing craft type.
*
* @see #type
*/
public static final byte TYPE_SUPPLYSHIP = 12;
/**
* Constant representing craft type.
*
* @see #type
*/
public static final byte TYPE_UNUSED = -1;
/**
* A list of maximum damage amounts for human craft.
*
* The index of each value in the array corresponds to the craft type whose
* maximum damage it represents. For example, the maximum damage for a
* Skyranger craft can be retrieved using the construction
* <code>MAX_DAMAGE_LIST[TYPE_SKYRANGER]</code>.
*
* @see #damage
* @see #type
*/
public static final short[] MAX_DAMAGE_LIST = {
150, 800, 1200, 100, 500
};
/**
* A list of maximum fuel amounts for human craft.
*
* The index of each value in the array corresponds to the craft type whose
* maximum fuel it represents. For example, the maximum fuel for a
* Skyranger can be retrieved using the construction
* <code>MAX_FUEL_LIST[TYPE_SKYRANGER]</code>.
*
* @see #fuel
* @see #type
*/
public static final short[] MAX_FUEL_LIST = {
1500, 30, 60, 1000, 20
};
/**
* Constant representing a craft weapon type.
*
* @see #leftType
* @see #rightType
*/
public static final byte WEAPON_STINGRAY = 0;
/**
* Constant representing a craft weapon type.
*
* @see #leftType
* @see #rightType
*/
public static final byte WEAPON_AVALANCHE = 1;
/**
* Constant representing a craft weapon type.
*
* @see #leftType
* @see #rightType
*/
public static final byte WEAPON_CANNON = 2;
/**
* Constant representing a craft weapon type.
*
* @see #leftType
* @see #rightType
*/
public static final byte WEAPON_FUSIONBALL = 3;
/**
* Constant representing a craft weapon type.
*
* @see #leftType
* @see #rightType
*/
public static final byte WEAPON_LASERCANNON = 4;
/**
* Constant representing a craft weapon type.
*
* @see #leftType
* @see #rightType
*/
public static final byte WEAPON_PLASMACANNON = 5;
/**
* Constant representing a craft weapon type.
*
* @see #leftType
* @see #rightType
*/
public static final byte WEAPON_NONE = -1;
/**
* A list maximum ammunition amounts for human craft weapons.
*
* The index of a value in the array corresponds to the weapon type that
* whose maximum ammunition it represents. For example, the code
* <code>MAX_AMMO_LIST[WEAPON_PLASMACANNON]</code> would return the maximum
* ammunition amount for a plasma cannon.
*/
public static final short[] MAX_AMMO_LIST = {
6, 3, 200, 2, 99, 100, 0
};
/**
* Constant representing a current possible activity of a craft.
*
* @see #activity
*/
public static final byte ACTIVITY_ATBASE = 0;
/**
* Constant representing a current possible activity of a craft.
*
* @see #activity
*/
public static final byte ACTIVITY_AIRBORNE = 1;
/**
* Constant representing a current possible activity of a craft.
*
* @see #activity
*/
public static final byte ACTIVITY_UFO = 2;
/**
* Constant representing a current possible altitude of a craft.
*
* @see #altitude
*/
public static final byte ALTITUDE_GROUND = 0;
/**
* Constant representing a current possible altitude of a craft.
*
* @see #altitude
*/
public static final byte ALTITUDE_VERYLOW = 1;
/**
* Constant representing a current possible altitude of a craft.
*
* @see #altitude
*/
public static final byte ALTITUDE_LOW = 2;
/**
* Constant representing a current possible altitude of a craft.
*
* @see #altitude
*/
public static final byte ALTITUDE_HIGH = 3;
/**
* Constant representing a current possible altitude of a craft.
*
* @see #altitude
*/
public static final byte ALTITUDE_VERYHIGH = 4;
/**
* Constant representing a mission type of an alien craft.
*
* @see #missionType
*/
public static final short MISSION_RESEARCH = 0;
/**
* Constant representing a mission type of an alien craft.
*
* @see #missionType
*/
public static final short MISSION_HARVEST = 1;
/**
* Constant representing a mission type of an alien craft.
*
* @see #missionType
*/
public static final short MISSION_ABDUCTION = 2;
/**
* Constant representing a mission type of an alien craft.
*
* @see #missionType
*/
public static final short MISSION_INFILTRATION = 3;
/**
* Constant representing a mission type of an alien craft.
*
* @see #missionType
*/
public static final short MISSION_BASE = 4;
/**
* Constant representing a mission type of an alien craft.
*
* @see #missionType
*/
public static final short MISSION_TERROR = 5;
/**
* Constant representing a mission type of an alien craft.
*
* @see #missionType
*/
public static final short MISSION_RETALIATION = 6;
/**
* Constant representing a mission type of an alien craft.
*
* @see #missionType
*/
public static final short MISSION_SUPPLY = 7;
/**
* Constant representing a target geographical zone for an alien craft's mission.
*
* @see #missionZone
*/
public static final short ZONE_NORTHAMERICA = 0;
/**
* Constant representing a target geographical zone for an alien craft's mission.
*
* @see #missionZone
*/
public static final short ZONE_ARCTIC = 1;
/**
* Constant representing a target geographical zone for an alien craft's mission.
*
* @see #missionZone
*/
public static final short ZONE_ANTARCTICA = 2;
/**
* Constant representing a target geographical zone for an alien craft's mission.
*
* @see #missionZone
*/
public static final short ZONE_SOUTHAMERICA = 3;
/**
* Constant representing a target geographical zone for an alien craft's mission.
*
* @see #missionZone
*/
public static final short ZONE_EUROPE = 4;
/**
* Constant representing a target geographical zone for an alien craft's mission.
*
* @see #missionZone
*/
public static final short ZONE_NORTHAFRICA = 5;
/**
* Constant representing a target geographical zone for an alien craft's mission.
*
* @see #missionZone
*/
public static final short ZONE_SOUTHERNAFRICA = 6;
/**
* Constant representing a target geographical zone for an alien craft's mission.
*
* @see #missionZone
*/
public static final short ZONE_CENTRALASIA = 7;
/**
* Constant representing a target geographical zone for an alien craft's mission.
*
* @see #missionZone
*/
public static final short ZONE_SOUTHEASTASIA = 8;
/**
* Constant representing a target geographical zone for an alien craft's mission.
*
* @see #missionZone
*/
public static final short ZONE_SIBERIA = 9;
/**
* Constant representing a target geographical zone for an alien craft's mission.
*
* @see #missionZone
*/
public static final short ZONE_AUSTRALASIA = 10;
/**
* Constant representing a target geographical zone for an alien craft's mission.
*
* @see #missionZone
*/
public static final short ZONE_PACIFIC = 11;
/**
* Constant representing a target geographical zone for an alien craft's mission.
*
* @see #missionZone
*/
public static final short ZONE_NORTHATLANTIC = 12;
/**
* Constant representing a target geographical zone for an alien craft's mission.
*
* @see #missionZone
*/
public static final short ZONE_SOUTHATLANTIC = 13;
/**
* Constant representing a target geographical zone for an alien craft's mission.
*
* @see #missionZone
*/
public static final short ZONE_INDIANOCEAN = 14;
/**
* Constant representing a current status of a human craft.
*
* @see #status
*/
public static final short STATUS_READY = 0;
/**
* Constant representing a current status of a human craft.
*
* @see #status
*/
public static final short STATUS_OUT = 1;
/**
* Constant representing a current status of a human craft.
*
* @see #status
*/
public static final short STATUS_REPAIRS = 2;
/**
* Constant representing a current status of a human craft.
*
* @see #status
*/
public static final short STATUS_REFUELING = 3;
/**
* Constant representing a current status of a human craft.
*
* @see #status
*/
public static final short STATUS_REARMING = 4;
/**
* The list of names corresponding to slots in a craft inventory. The index
* of a value corresponds to its slot in the items array.
*
* @see #items
*/
public static final String[] ITEM_NAMES = {
"Tank/Cannon",
"Tank/Rocket Launcher",
"Tank/Laser Cannon",
"Hover Tank/Plasma",
"Hover Tank/Launcher",
"PISTOL",
"PISTOL CLIP",
"RIFLE",
"RIFLE CLIP",
"HEAVY CANNON",
"CANNON AP-AMMO",
"CANNON HE-AMMO",
"CANNON I-AMMO",
"AUTO-CANNON",
"AUTO-CANNON AP-AMMO",
"AUTO-CANNON HE-AMMO",
"AUTO-CANNON I-AMMO",
"ROCKET LAUNCHER",
"SMALL ROCKET",
"LARGE ROCKET",
"INCENDIARY ROCKET",
"LASER PISTOL",
"LASER GUN",
"HEAVY LASER",
"GRENADE",
"SMOKE GRENADE",
"PROXIMITY GRENADE",
"HIGH EXPLOSIVE",
"MOTION SCANNER",
"MEDI-KIT",
"PSI-AMP",
"STUN ROD",
"Flare",
"empty*",
"empty*",
"empty*",
"CORPSE*",
"CORPSE & ARMOUR*",
"CORPSE & POWER SUIT*",
"Heavy Plasma",
"Heavy Plasma Clip",
"Plasma Rifle",
"Plasma Rifle Clip",
"Plasma Pistol",
"Plasma Pistol Clip",
"BLASTER LAUNCHER",
"BLASTER BOMB",
"SMALL LAUNCHER",
"STUN MISSILE",
"ALIEN GRENADE",
"ELERIUM-115",
"MIND PROBE",
">>UNDEFINED <<*",
">> empty <<*",
">> empty <<*"
};
/**
* The slot from the saved game data file that this craft record belongs to.
*/
public int index;
/**
* The ship type of the craft. This value also determines whether the craft
* is human or alien. <p>Human craft values are:
* <ul>
* <li>0 - Skyranger</li>
* <li>1 - Lightning</li>
* <li>2 - Avenger</li>
* <li>3 - Interceptor</li>
* <li>4 - Firestorm</li>
* </ul>
* Alien craft values are:
* <ul>
* <li>5 - Small Scout</li>
* <li>6 - Medium Scout</li>
* <li>7 - Large Scout</li>
* <li>8 - Harvester</li>
* <li>9 - Abductor</li>
* <li>10 - Terror Ship</li>
* <li>11 - Battleship</li>
* <li>12 - Supply Ship</li>
* </ul>
* These values are accessible via the <code>TYPE_*</code> constants
* of the <code>Craft</code> class.</p>
* A value of 0xFF or -1 indicates that the slot is not used. Note that
* other data associated with this craft may or may not be used or may be
* used differently depending on whether the craft is alien or human. See
* individual variables for notes on how they are handled specifically in
* this respect.
*/
public byte type;
/**
* The type of the craft's left weapon for human craft.
*
* Possible values are taken from the <code>WEAPON_</code> constants of the
* <code>Craft</code> class.
*/
public byte leftType;
/**
* The current ammunition of the left weapon for human craft.
*
* The maximum value of this field depends on the type of weapon (see <code>
* leftType</code> field. The <code>MAX_AMMO_LIST</code> field lists these
* values in order of weapon type.
*
* @see Craft#leftType
* @see Craft#MAX_AMMO_LIST
*/
public short leftAmmo;
/**
* The current activity of the craft. The possible values of this field are
* represented by the <code>ACTIVITY_*</code> constants of the <code>Craft</code>
* class. It is always <code>ACTIVITY_UFO</code> for alien craft.
*/
public byte activity;
/**
* The type of the craft's right weapon for human craft.
*
* Possible values are taken from the <code>WEAPON_</code> constants of the
* <code>Craft</code> class.
*/
public byte rightType;
/**
* The current ammunition of the right weapon for human craft.
*
* The maximum value of this field depends on the type of weapon (see <code>
* leftType</code> field. The <code>MAX_AMMO_LIST</code> field lists these
* values in order of weapon type.
*
* @see Craft#rightType
* @see Craft#MAX_AMMO_LIST
*/
public short rightAmmo;
/**
* The first unknown field of a craft entry. It is two bytes wide.
*/
public short unknown1;
/**
* The current damage suffered by a craft.
*/
public short damage;
/**
* The current altitude of an alien craft. Possible values are taken from
* the <code>ALTITUDE_*</code> constants of the <code>Craft</code> class.
*/
public short altitude;
/**
* The current speed of a craft.
*/
public short speed;
/**
* The location index of an alien craft's current destination.
*
* This value indexes into entries in the LOC.DAT file. They are represented
* by instances of the <code>Location</code> class.
*
* @see Location
*/
public short destinationIndex;
/**
* The location index of a human craft's current interception target, if in
* flight.
*
* This value indexes into entries in the LOC.DAT file. They are represented
* by instances of the <code>Location</code> class.
*
* @see Location
*/
public short interceptionIndex;
/**
* The location index of the first waypoint of a human craft, if in flight.
*
* This value indexes into entries in the LOC.DAT file. They are represented
* by instances of the <code>Location</code> class.
*/
public short waypoint1;
/**
* The location index of the second waypoint of a human craft, if in flight.
*
* This value indexes into entries in the LOC.DAT file. They are represented
* by instances of the <code>Location</code> class.
*/
public short waypoint2;
/**
* The current amount of fuel of a human craft.
*
* The maximum value of this field depends on the craft <code>type</code>.
* The <code>MAX_FUEL_LIST</code> field lists these values in order of
* craft type.
*
* @see #type
* @see #MAX_FUEL_LIST
*/
public short fuel;
/**
* The location index of the base this craft belongs to (human craft only).
*
* This value indexes into entries in the LOC.DAT file. They are represented
* by instances of the <code>Location</code> class.
*/
public short baseIndex;
/**
* The current mission type the craft (alien craft only).
*
* Possible values are represented by the <code>MISSION_*</code> constants
* of the <code>Craft</code> class.
*/
public short missionType;
/**
* The target geographical zone of the craft's current mission (alien craft only).
*
* Possible values are represented by the <code>ZONE_*</code> constants of
* the <code>Craft</code> class.
*/
public short missionZone;
/**
* The second unknown field loaded from this craft's original file entry.
* It is two bytes wide.
*/
public short unknown2;
/**
* The second unknown field loaded from this craft's original file entry.
* It is two bytes wide.
*/
public short unknown3;
/**
* The race of aliens that own this craft (alien craft only).
*
* Possible values are indexes into the original ENGLISH.DAT file. They are:
* <ul>
* <li>0 - Sectoid</li>
* <li>1 - Snakeman</li>
* <li>2 - Ethereal</li>
* <li>3 - Muton</li>
* <li>4 - Floater</li>
* <li>5 - Celatid1</li>
* <li>6 - Silacoid2</li>
* <li>7 - Chryssalid</li>
* <li>8 - Reaper</li>
* <li>9 - Sectopod</li>
* <li>10 - Cyberdisc</li>
* </ul>
*/
public short race;
/**
* The fourth unknown field loaded from this craft's original file entry.
* It is two bytes wide.
*/
public short unknown4;
/**
* The fifth unknown field loaded from this craft's original file entry.
* It is two bytes wide.
*/
public short unknown5;
/**
* The current status of this craft (human craft only).
*
* Possible values are represented by the <code>STATUS_*</code> constants
* of the <code>Craft</code> class.
*/
public short status;
/**
* The items currently carried by this craft (human troop carrier craft only).
*
* The size of this array is represented by the <code>NUM_ITEMS</code> constant.
*
* @see #NUM_ITEMS
*/
public short[] items;
/**
* The sixth unknown field loaded from this craft's original file entry.
* It's size in bytes is represented by the <code>UNKNOWN6_SIZE</code> constant.
*/
public byte[] unknown6;
/**
* Constructs a new craft entry. No fields are initialised.
*/
protected Craft() {
}
/**
* Constructs a new craft entry with all field values copied from the
* given <code>Craft</code>.
*
* @param other the craft from all field values will be copied into the new craft entry.
*/
public Craft(Craft other) {
copy(other);
}
/**
* Sets all fields of this craft to be the same of the given <code>Craft</code>
*
* @param other the craft from which all field values will be copied.
*/
public void copy(Craft other) {
this.index = other.index;
this.type = other.type;
this.leftType = other.leftType;
this.leftAmmo = other.leftAmmo;
this.activity = other.activity;
this.rightType = other.rightType;
this.rightAmmo = other.rightAmmo;
this.unknown1 = other.unknown1;
this.damage = other.damage;
this.altitude = other.altitude;
this.speed = other.speed;
this.destinationIndex = other.destinationIndex;
this.interceptionIndex = other.interceptionIndex;
this.waypoint1 = other.waypoint1;
this.waypoint2 = other.waypoint2;
this.fuel = other.fuel;
this.baseIndex = other.baseIndex;
this.missionType = other.missionType;
this.missionZone = other.missionZone;
this.unknown2 = other.unknown2;
this.unknown3 = other.unknown3;
this.race = other.race;
this.unknown4 = other.unknown4;
this.unknown5 = other.unknown5;
this.status = other.status;
this.items = new short[NUM_ITEMS];
System.arraycopy(other.items, 0, this.items, 0, NUM_ITEMS);
this.unknown6 = new byte[UNKNOWN6_SIZE];
System.arraycopy(other.unknown6, 0, this.unknown6, 0, UNKNOWN6_SIZE);
}
/**
* Reads a craft entry from the given <code>UFOInputStream</code>, storing
* the entry's data in this craft.
*
* Does not set the <code>index</code> field (this must be done manually).
*
* @param in the stream from which to read
* @return true if a complete record was read, false if the end of the stream was reached beforehand.
* @throws IOException if an I/O error occurred while reading the entry.
*/
public boolean readFromFile(UFOInputStream in) throws IOException {
try {
type = in.readByte();
leftType = in.readByte();
leftAmmo = in.readUFOShort();
activity = in.readByte();
rightType = in.readByte();
rightAmmo = in.readUFOShort();
unknown1 = in.readUFOShort();
damage = in.readUFOShort();
altitude = in.readUFOShort();
speed = in.readUFOShort();
destinationIndex = in.readUFOShort();
interceptionIndex = in.readUFOShort();
waypoint1 = in.readUFOShort();
waypoint2 = in.readUFOShort();
fuel = in.readUFOShort();
baseIndex = in.readUFOShort();
missionType = in.readUFOShort();
missionZone = in.readUFOShort();
unknown2 = in.readUFOShort();
unknown3 = in.readUFOShort();
race = in.readUFOShort();
unknown4 = in.readUFOShort();
unknown5 = in.readUFOShort();
status = in.readUFOShort();
items = new short[NUM_ITEMS];
for(int i = 0; i < NUM_ITEMS; i++) {
items[i] = in.readUFOByte();
}
unknown6 = new byte[UNKNOWN6_SIZE];
in.read(unknown6);
return true;
}
catch(EOFException e) {
return false;
}
}
/**
* Writes a craft entry to the given <code>UFOOutputStream</code> using
* this craft's fields as the entry data.
*
* The <code>index</code> field is not used - the entry is written to the
* current position of the stream.
*
* @param out the stream to write the entry to
* @throws IOException if an I/O error occurred while writing the entry.
*/
public void writeToFile(UFOOutputStream out) throws IOException {
out.writeByte(type);
out.writeByte(leftType);
out.writeUFOShort(leftAmmo);
out.writeByte(activity);
out.writeByte(rightType);
out.writeUFOShort(rightAmmo);
out.writeUFOShort(unknown1);
out.writeUFOShort(damage);
out.writeUFOShort(altitude);
out.writeUFOShort(speed);
out.writeUFOShort(destinationIndex);
out.writeUFOShort(interceptionIndex);
out.writeUFOShort(waypoint1);
out.writeUFOShort(waypoint2);
out.writeUFOShort(fuel);
out.writeUFOShort(baseIndex);
out.writeUFOShort(missionType);
out.writeUFOShort(missionZone);
out.writeUFOShort(unknown2);
out.writeUFOShort(unknown3);
out.writeUFOShort(race);
out.writeUFOShort(unknown4);
out.writeUFOShort(unknown5);
out.writeUFOShort(status);
for(int i = 0; i < NUM_ITEMS; i++) {
out.writeUFOByte(items[i]);
}
for(int i = 0; i < UNKNOWN6_SIZE; i++) {
out.writeByte(unknown6[i]);
}
}
/**
* Reads a list of craft entries from the given <code>UFOInputStream</code>.
*
* The index field of each <code>Craft</code> object is set; this method
* assumes the first entry read is index 0. As many entries as possible are
* read until the end of the stream is reached.
*
* @param in the input stream
* @return an <code>ArrayList</code> of <code>Craft</code> representing the entries read from the stream.
* @throws IOException if an I/O error occurred while reading the entries.
*/
public static ArrayList<Craft> readList(UFOInputStream in) throws IOException {
ArrayList<Craft> list = new ArrayList<Craft>();
int index = 0;
while(true) {
Craft craft = new Craft();
if (craft.readFromFile(in)) {
craft.index = index++;
list.add(craft);
}
else {
break;
}
}
return list;
}
/**
* Writes the given list of craft entries to the given <code>UFOOutputStream</code>.
*
* The <code>index</code> fields of the <code>Craft</code> objects are not used. The entries are written in the order they appear in the <code>ArrayList</code>.
*
* @param out the output stream to write the entries to.
* @param list a list of <code>Craft</code> objects representing the list of craft entries to be written.
* @throws IOException if an I/O error occurred while writing the entries.
*/
public static void writeList(UFOOutputStream out, ArrayList<Craft> list) throws IOException {
Iterator<Craft> ci = list.iterator();
while(ci.hasNext()) {
ci.next().writeToFile(out);
}
}
/**
* Returns true if this craft's <code>type</code> corresponds a human craft.
*
* @return true if the craft type is human
*
* @see #type
*/
public boolean isHuman() {
return (type == TYPE_SKYRANGER || type == TYPE_AVENGER || type == TYPE_INTERCEPTOR || type == TYPE_FIRESTORM || type == TYPE_LIGHTNING);
}
/**
* Convenience method that returns the string representation of the given craft type.
* Performs the reverse transformation of the <code>stringToType</code> method.
*
* @param type the type to return a string representation of
* @return a string representation of the given type
*
* @see #type
* @see #stringToType(java.lang.String)
*/
public static String typeToString(byte type) {
switch(type) {
case TYPE_SKYRANGER: return "Skyranger";
case TYPE_LIGHTNING: return "Lightning";
case TYPE_AVENGER: return "Avenger";
case TYPE_INTERCEPTOR: return "Interceptor";
case TYPE_FIRESTORM: return "Firestorm";
case TYPE_SMALLSCOUNT: return "Small Scout";
case TYPE_MEDIUMSCOUNT: return "Medium Scout";
case TYPE_LARGESCOUT: return "Large Scout";
case TYPE_HARVESTER: return "Harvester";
case TYPE_ABDUCTOR: return "Abductor";
case TYPE_TERRORSHIP: return "Terror Ship";
case TYPE_BATTLESHIP: return "Battleship";
case TYPE_SUPPLYSHIP: return "Supply Ship";
case TYPE_UNUSED: return "Unused";
default: return "Unknown";
}
}
/**
* Convenience method that returns the craft type corresponding to the given string.
* Performs the reverse transformation to the <code>typeToString</code> method.
*
* @param str the string to return the corresponding type of
* @return a numerical type corresponding to the given string.
*
* @see #type
* @see #typeToString(byte)
*/
public static byte stringToType(String str) {
if (str.equals("Skyranger")) {
return TYPE_SKYRANGER;
}
if (str.equals("Lightning")) {
return TYPE_LIGHTNING;
}
if (str.equals("Avenger")) {
return TYPE_AVENGER;
}
if (str.equals("Interceptor")) {
return TYPE_INTERCEPTOR;
}
if (str.equals("Firestorm")) {
return TYPE_FIRESTORM;
}
if (str.equals("Small Scout")) {
return TYPE_SMALLSCOUNT;
}
if (str.equals("Medium Scout")) {
return TYPE_MEDIUMSCOUNT;
}
if (str.equals("Large Scout")) {
return TYPE_LARGESCOUT;
}
if (str.equals("Harvester")) {
return TYPE_HARVESTER;
}
if (str.equals("Abductor")) {
return TYPE_ABDUCTOR;
}
if (str.equals("Terror Ship")) {
return TYPE_TERRORSHIP;
}
if (str.equals("Battleship")) {
return TYPE_BATTLESHIP;
}
if (str.equals("Supply Ship")) {
return TYPE_SUPPLYSHIP;
}
if (str.equals("Unknown")) {
return TYPE_UNUSED;
}
else {
return TYPE_UNUSED;
}
}
/**
* Returns a list of strings corresponding to the names of all human craft types.
* The list entries can be converted into their numerical types using the <code>stringToType</code> method.
*
* @return a list of type names of human craft
*
* @see #type
* @see #stringToType(java.lang.String)
*/
public static String[] getHumanTypeNames() {
String[] types = new String[TYPE_FIRESTORM + 1];
for(int i = 0; i <= TYPE_FIRESTORM; i++) {
types[i] = typeToString((byte)i);
}
return types;
}
/**
* Returns a list of strings corresponding to all possible craft status values.
* The list entries can be converted into numerical status values using the <code>stringToStatus</code> method.
*
* @return a list of all possible craft status names
*
* @see #status
* @see #stringToStatus(java.lang.String)
*/
public static String[] getStatusNames() {
String[] statuses = new String[STATUS_REARMING + 1];
for(int i = 0; i <= STATUS_REARMING; i++) {
statuses[i] = statusToString((short)i);
}
return statuses;
}
/**
* Returns a list of strings corresponding to the names of all possible craft weapon types.
* The list entries can be converted into numerical weapon types using the <code>stringToWeaponType</code> method.
*
* @return a list of all possible craft weapon names
*
* @see #leftType
* @see #rightType
* @see #stringToWeaponType(java.lang.String)
*/
public static String[] getWeaponTypeNames() {
String[] types = new String[WEAPON_PLASMACANNON + 2];
for(int i = 0; i <= WEAPON_PLASMACANNON; i++) {
types[i] = weaponTypeToString((byte)i);
}
types[types.length - 1] = weaponTypeToString(TYPE_UNUSED);
return types;
}
/**
* Convenience method that returns the string representation of the given craft weapon type.
* Performs the reverse transformation to <code>stringToWeaponType</code>.
*
* @param weaponType the weapon type to return a string representation of
* @return the string name of the given weapon type
*
* @see #leftType
* @see #rightType
* @see #stringToWeaponType(java.lang.String)
*/
public static String weaponTypeToString(byte weaponType) {
switch(weaponType) {
case WEAPON_STINGRAY: return "Stingray Launcher";
case WEAPON_AVALANCHE: return "Avalance Launcher";
case WEAPON_CANNON: return "Cannon";
case WEAPON_FUSIONBALL: return "Fusion Ball Launcher";
case WEAPON_LASERCANNON: return "Laser Cannon";
case WEAPON_PLASMACANNON: return "Plasma Cannon";
case WEAPON_NONE: return "None";
default: return "unknown";
}
}
/**
* Sets this craft's fuel to its maximum value (human craft only).
*
* @see #fuel
*/
public void setMaxFuel() {
if (!isHuman()) {
return;
}
fuel = MAX_FUEL_LIST[type];
}
/**
* Sets the ammunition to maximum for the specified weapon of this craft (human craft only).
*
* @param left if true sets the left weapon ammunition to maximum, if false sets the right weapon
*
* @see #leftAmmo
* @see #rightAmmo
*/
public void setMaxAmmo(boolean left) {
if (left) {
leftAmmo = leftType == WEAPON_NONE ? 0 : MAX_AMMO_LIST[leftType];
}
else {
rightAmmo = rightType == WEAPON_NONE ? 0 : MAX_AMMO_LIST[rightType];
}
}
/**
* Sets the ammunition to maximum for both weapons of this craft (human craft only).
*/
public void setMaxAmmo() {
setMaxAmmo(true);
setMaxAmmo(false);
}
/**
* Convenience method that returns the craft weapon type corresponding to the given string.
* Performs the reverse transformation to the <code>weaponTypeToString</code> method.
*
* @param str the string for which to return the corresponding weapon type
* @return a numerical weapon type corresponding to the given string.
*
* @see #leftType
* @see #rightType
* @see #weaponTypeToString(byte)
*/
public static byte stringToWeaponType(String str) {
if (str.equals("Stingray Launcher")) {
return WEAPON_STINGRAY;
}
else if(str.equals("Avalance Launcher")) {
return WEAPON_AVALANCHE;
}
else if(str.equals("Cannon")) {
return WEAPON_CANNON;
}
else if (str.equals("Fusion Ball Launcher")) {
return WEAPON_FUSIONBALL;
}
else if (str.equals("Laser Cannon")) {
return WEAPON_LASERCANNON;
}
else if (str.equals("Plasma Cannon")) {
return WEAPON_PLASMACANNON;
}
else if (str.equals("None")) {
return WEAPON_NONE;
}
else {
return WEAPON_NONE;
}
}
/**
* Convenience method that returns the string representation of the given craft status.
* Performs the reverse transformation to <code>stringToStatus</code>.
*
* @param status the status for which to return the corresponding string name
* @return the string name of the given status
*
* @see #status
* @see #stringToStatus(java.lang.String)
*/
public static String statusToString(short status) {
switch(status) {
case STATUS_READY: return "Ready";
case STATUS_OUT: return "Out";
case STATUS_REPAIRS: return "Repairing";
case STATUS_REFUELING: return "Refueling";
case STATUS_REARMING: return "Rearming";
default: return "Unknown";
}
}
/**
* Convenience method that returns the status corresponding to the given string.
* Performs the reverse transformation to the <code>statusToString</code> method.
*
* @param str the string for which to return the corresponding status
* @return a numerical weapon type corresponding to the given string.
*
* @see #status
* @see #statusToString(short)
*/
public static short stringToStatus(String str) {
if (str.equals("Ready")) {
return STATUS_READY;
}
else if(str.equals("Out")) {
return STATUS_OUT;
}
else if(str.equals("Repairing")) {
return STATUS_REPAIRS;
}
else if(str.equals("Refueling")) {
return STATUS_REFUELING;
}
else if(str.equals("Rearming")) {
return STATUS_REARMING;
}
else {
return STATUS_READY;
}
}
/**
* Sets the craft status to the 'correct' value depending on its current
* damage, fuel and ammunition values. This is intended to mimic the
* original game status order, which craft are first repaired, refueled and
* finally rearmed before being marked as ready.
*
* If the craft status is STATUS_OUT, no change is made.
*
* @see #status
* @see #damage
* @see #fuel
* @see #leftAmmo
* @see #rightAmmo
*/
public void setCorrectStatus() {
if (status == STATUS_OUT) {
return;
}
if (damage > 0) {
status = STATUS_REPAIRS;
}
else if(fuel < MAX_FUEL_LIST[type]) {
status = STATUS_REFUELING;
}
else if((leftType != WEAPON_NONE && leftAmmo < MAX_AMMO_LIST[leftType]) ||
(rightType != WEAPON_NONE && rightAmmo < MAX_AMMO_LIST[rightType])) {
status = STATUS_REARMING;
}
else {
status = STATUS_READY;
}
}
}