jmud-0.11/
jmud-0.11/bin/
jmud-0.11/doc/
jmud-0.11/rec/
jmud-0.11/rec/mun/
jmud-0.11/rec/mun/grecia/
jmud-0.11/rec/mun/gunnar/
jmud-0.11/rec/qua/
jmud-0.11/src/bool/
jmud-0.11/src/clone/
jmud-0.11/src/integer/
jmud-0.11/src/misc/
jmud-0.11/src/string/
jmud-0.11/src/util/bit/
jmud-0.11/src/util/color/
jmud-0.11/src/util/file/
jmud-0.11/src/util/jgp/adaptor/
jmud-0.11/src/util/jgp/algorithm/
jmud-0.11/src/util/jgp/container/
jmud-0.11/src/util/jgp/functor/
jmud-0.11/src/util/jgp/interfaces/
jmud-0.11/src/util/jgp/predicate/
jmud-0.11/src/util/log/
jmud-0.11/src/util/state/
jmud-0.11/trash/
import java.io.BufferedReader;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.Enumeration;

import util.list.Vector;
import util.file.InvalidFileFormatException;
import util.log.Log;
import misc.Separators;
import integer.IntPair;
import string.StrUtil;


class KeyProto extends ItemProto {

    Vector doorReferences = new Vector();

    // salva base da zona para permitir salvar os identificadores
    // de porta corretos (obtidos via subtracao deste valor)
    int savedZoneBase;  

    KeyProto(int zoneBase, int itemId, BufferedReader itemFile)
	throws InvalidFileFormatException {

	super(zoneBase, itemId, itemFile);
	Log.debug("Chave: " + getName());

	savedZoneBase = zoneBase;

	try {
	    for (; ; ) {
		if (!itemFile.ready())
		    throw new InvalidFileFormatException();
		String line = itemFile.readLine();
		if (line.startsWith(Separators.EOR))
		    break;
		StringTokenizer toker = new StringTokenizer(line);
		if (!toker.hasMoreTokens())
		    throw new InvalidFileFormatException();
		int room = Integer.parseInt(toker.nextToken());
		if (!toker.hasMoreTokens())
		    throw new InvalidFileFormatException();
		int dir  = Door.getDirectionByName(toker.nextToken());
		doorReferences.insert(new IntPair(room + zoneBase, dir));
	    }
	    doorReferences.trim();
	}
	catch(IOException e) {
	    throw new InvalidFileFormatException();
	}
	catch(NumberFormatException e) {
	    throw new InvalidFileFormatException();
	}
    }

    protected void finalize() {
	super.finalize();
    }

    int getProtoType() {
	return Item.T_KEY;
    }

    Item create() {
	return new Key(this);
    }
    
    /////////////
    // Sheetable:

    private String listDoors() {
	String list = "";
	for (Enumeration enum = doorReferences.elements(); enum.hasMoreElements(); ) {
	    IntPair pair = (IntPair) enum.nextElement();
	    list += Separators.NL + (pair.getValue1() - savedZoneBase) + " " + Door.getDirectionName(pair.getValue2());
	}
	return list;
    }

    public String getSheet() {
	return super.getSheet() + Separators.NL + "Portas:" + StrUtil.wrapList(listDoors(), "Nenhuma");
    }

    //
    /////////////
}