Door.java

jmud-0.11/src/Door.java · 5.3 KB

From the archive jmud-0_11.tgz

Download the archive   « Back to the archive contents

import java.util.Enumeration;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;

import util.bit.Bit;
import util.nice.Niceable;
import util.nice.NiceKey;
import util.log.Log;
import string.StrUtil;
import string.InvalidFlagException;

class Door implements Niceable /*, Exit*/, Sheetable {

  static final int D_NORTH = 0;
  static final int D_SOUTH = 1;
  static final int D_EAST  = 2;
  static final int D_WEST  = 3;
  static final int D_UP    = 4;
  static final int D_DOWN  = 5;
  static final int D_ERROR = 6;

  static final String directions[] = { 
    "norte", 
    "sul", 
    "leste", 
    "oeste", 
    "cima", 
    "baixo",
    "*ERRO*"
  };

    static final int inverse[] = {
	D_SOUTH,
	D_NORTH,
	D_WEST,
	D_EAST,
	D_DOWN,
	D_UP,
	D_ERROR
    };

  private int  theDirection;
  private int  destinationId;
  private Room destinationRoom = null;


    static final int P_CLOSEABLE = 1;  // a
    static final int P_LOCKABLE  = 2;  // b

    static final String propLabels[] = {
	"fechável",
	"trancável"
    };

    private int properties = 0;

    static final int S_OPEN   = 0;
    static final int S_CLOSED = 1;
    static final int S_LOCKED = 2;

    static final String stateLabels[] = {
	"aberta",
	"fechada",
	"trancada"
    };

    private int state = S_OPEN;


  static String getDirectionName(int direction) 
    throws NoSuchElementException {
      try {
	return directions[direction];
      }
      catch(ArrayIndexOutOfBoundsException e) {
	throw new NoSuchElementException();
      }
  }

    static int getInverseDir(int dir) {
      try {
	return inverse[dir];
      }
      catch(ArrayIndexOutOfBoundsException e) {
	  return D_ERROR;
      }
    }
  
  static int getDirectionByName(String dirName) 
    throws NoSuchElementException {
      
      char c = dirName.charAt(0);
      for (int i = 0; i < directions.length; ++i)
	if (c == directions[i].charAt(0))
	  return i;
      
      throw new NoSuchElementException();
  }

  static String expandDirectionName(String dirName) 
    throws NoSuchElementException {
    return getDirectionName(getDirectionByName(dirName));
  }

  Door(int zoneBase, String door, int destId) throws NoSuchElementException {
    theDirection = getDirectionByName(door);

    destinationId =  destId + zoneBase;
  }

  Door(int zoneBase, String door) throws NoSuchElementException, InvalidFlagException {

      StringTokenizer toker = new StringTokenizer(door);

    theDirection = getDirectionByName(toker.nextToken());

    int id = Integer.parseInt(toker.nextToken());

    if (toker.hasMoreTokens())
	properties = StrUtil.parseFlags(toker.nextToken());

    destinationId =  id + zoneBase;
  }

  ////////////
  // Niceable:

  public boolean lessThan(Niceable n) {
    return theDirection < ((Door) n).theDirection;
  }

  public boolean hasKey(NiceKey key) {
    boolean result = false;
    switch(key.getKeyType()) {
    case NiceKey.K_INT:
      result = hasId(key.getIntKey());
      break;
    case NiceKey.K_STR: 
      result = hasName(key.getStrKey());
      break;
    default:
      Log.err("Tipo inválido de chave em Door.hasKey(...): " + key.getKeyType());
    }
    return result;
  }

  public boolean keyLessThan(NiceKey key) {
    boolean result = false;
    switch(key.getKeyType()) {
    case NiceKey.K_INT:
      result = theDirection < key.getIntKey();
      break;
    case NiceKey.K_STR: 
      result = getDirectionName().compareTo(key.getStrKey()) < 0;
      break;
    default:
Log.err("Tipo inválido de chave em Door.keyLessThan(...): " + key.getKeyType());
    }
    return result;
  }

  //
  ////////////

  boolean hasId(int id) {
    return theDirection == id;
  }

  boolean hasName(String name) {
    return getDirectionName(theDirection).startsWith(name);
  }

  void setError() {
    theDirection = D_ERROR;
  }

    int getDirection() {
	return theDirection;
    }
    
  String getDirectionName() {
    String name = null;
    try {
      name = getDirectionName(theDirection);
    }
    catch(NoSuchElementException e) {
      Log.err("Porta da sala " + destinationRoom.getId() + " com direção inválida: " + theDirection);
    }
    return name;
  }

  int getDestinationId() {
    return destinationId;
  }

  Room getDestinationRoom() {
    return destinationRoom;
  }


    Door getInverseDoor() {
	try {
	    return getDestinationRoom().findDoorByDir(Door.getInverseDir(getDirection()));
	}
	catch(NoSuchElementException e) {
	}
	return null;
    }


  boolean leadsTo(String directionName) 
    throws NoSuchElementException {
    return Door.getDirectionName(theDirection).startsWith(directionName);
  }

    void adjust(Room targetRoom) {
	destinationRoom = targetRoom;
    }
    
    boolean isCloseable() {
	return Bit.isSet(properties, P_CLOSEABLE);
    }

    boolean isLockable() {
	return Bit.isSet(properties, P_LOCKABLE);
    }

    int getStatus() {
	return state;
    }

    void setStatus(int st) {
	state = st;
    }

    void setBothStatus(int st, Room rm) {
	setStatus(st);
	Door invDoor = getInverseDoor();
	if (invDoor.getDestinationRoom() == rm)
	    invDoor.setStatus(st);
    }

    boolean isOpen() {
	return getStatus() == S_OPEN;
    }

    ////////////
    // Sheetable

    public String getSheet() {
	return getDirectionName() + " [" + getDestinationId() + "] " + stateLabels[state] + " (" + StrUtil.listFlags(properties, propLabels) + ")";
    }

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

Site Stats

Snippets: 1791
Codebases: 571