/* * 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; /** * * @author Chris Davoren */ public class Location { public static final byte TYPE_UNUSED = 0; public static final byte TYPE_ALIENSHIP = 1; public static final byte TYPE_XCOMSHIP = 2; public static final byte TYPE_XCOMBASE = 3; public static final byte TYPE_CRASHSITE = 4; public static final byte TYPE_LANDEDUFO = 5; public static final byte TYPE_WAYPOINT = 6; public static final byte TYPE_TERRORSITE = 7; public int index; public byte type; public byte reference; public short startHCoord; public short startVCoord; public short destHCoord; public short destVCoord; public short countSuffix; public short unknown1; public short unknown2; public byte visibility; public byte unknown3; public byte unknown4; public byte unknown5; protected Location() { } public boolean readFromFile(UFOInputStream in) throws IOException { try { type = in.readByte(); reference = in.readByte(); startHCoord = in.readUFOShort(); startVCoord = in.readUFOShort(); destHCoord = in.readUFOShort(); destVCoord = in.readUFOShort(); countSuffix = in.readUFOShort(); unknown1 = in.readUFOShort(); unknown2 = in.readUFOShort(); visibility = in.readByte(); unknown3 = in.readByte(); unknown4 = in.readByte(); unknown5 = in.readByte(); return true; } catch(EOFException e) { return false; } } public void writeToFile(UFOOutputStream out) throws IOException { out.writeByte(type); out.writeByte(reference); out.writeUFOShort(startHCoord); out.writeUFOShort(startVCoord); out.writeUFOShort(destHCoord); out.writeUFOShort(destVCoord); out.writeUFOShort(countSuffix); out.writeUFOShort(unknown1); out.writeUFOShort(unknown2); out.writeByte(visibility); out.writeByte(unknown3); out.writeByte(unknown4); out.writeByte(unknown5); } public static ArrayList<Location> readList(UFOInputStream in) throws IOException { ArrayList<Location> list = new ArrayList<Location>(); int index = 0; while(true) { Location l = new Location(); if(l.readFromFile(in)) { l.index = index++; list.add(l); } else { break; } } return list; } public static void writeList(UFOOutputStream out, ArrayList<Location> list) throws IOException { Iterator<Location> li = list.iterator(); while(li.hasNext()) { li.next().writeToFile(out); } } public static Location getLocationForCraft(SavedGame game, int craftIndex) { // System.out.println("LocationForCraft called w/: craftIndex: " + craftIndex); Iterator<Location> li = game.locations.iterator(); int index = 0; while(li.hasNext()) { Location l = li.next(); if (l.type == TYPE_XCOMSHIP && l.reference == craftIndex) { // System.out.println(" Craft found: returning l: " + l.index); return l; } index++; } return null; } }