Room.java
jmud-0.11/src/Room.java · 11.9 KB
From the archive jmud-0_11.tgz
Download the archive « Back to the archive contents
import java.util.NoSuchElementException;
import java.util.Enumeration;
import java.io.IOException;
import java.io.BufferedReader;
import misc.Separators;
import util.bit.Bit;
import util.log.Log;
import util.list.List;
import util.list.Iterator;
import util.file.InvalidFileFormatException;
import util.nice.Searcher;
import util.nice.Niceable;
import util.nice.NiceEnumeration;
import util.nice.NiceKey;
import util.nice.NiceQueue;
import util.nice.NiceVector;
import string.StrUtil;
import string.InvalidFlagException;
import clone.CloneList;
import clone.CloneAlgo;
import util.color.Color;
class Room implements Place, Niceable {
static final int P_OPEN = Bit.BIT0; // lugar aberto (nao sala? campo?)
static final String propLabels[] = {
"aberta"
};
static private World theWorld = null;
static void setWorld(World wld) {
theWorld = wld;
}
private int theId;
private String theTitle = null;
private String theAliases = null;
private String theDescription = null;
private int properties = 0;
private NiceVector theDoors = new NiceVector();
protected CloneList theChars = new CloneList();
protected CloneList theItems = new CloneList();
private Zone theZone = null;
Room(Zone zn, int zoneBase, int roomId, String title, String aliases, String desc) {
theId = roomId + zoneBase;
theZone = zn;
theTitle = title;
theAliases = aliases;
theDescription = desc;
}
Room(Zone zn, int zoneBase, int roomId, BufferedReader roomFile)
throws InvalidFileFormatException {
theId = roomId + zoneBase;
theZone = zn;
try {
theTitle = roomFile.readLine();
Log.log("Sala: " + theTitle);
theAliases = StrUtil.loadAliases(roomFile);
if (theAliases.startsWith("-"))
theAliases = null;
theDescription = StrUtil.readDescription(roomFile);
try {
properties = StrUtil.parseFlags(roomFile.readLine());
}
catch(InvalidFlagException e) {
throw new InvalidFileFormatException();
}
for (; ; ) {
if (!roomFile.ready())
throw new InvalidFileFormatException();
String door = roomFile.readLine();
if (door.startsWith(Separators.EOR))
break;
try {
if (doorExists(door))
Log.err("Redefinição de porta: " + door);
else
theDoors.insert(new Door(zoneBase, door));
}
catch (NoSuchElementException e) {
Log.err("Direção inválida: " + door);
}
catch (InvalidFlagException e) {
Log.err("Propriedade inválida para porta: " + door);
}
}
// comprime o vetor de portas
theDoors.trim();
}
catch(IOException e) {
throw new InvalidFileFormatException();
}
}
boolean hasId(int roomId) {
return roomId == getId();
}
boolean hasName(String name) {
// uma sala pode não ter aliases
return theAliases == null ? false : StrUtil.findAlias(name, theAliases);
}
boolean doorExists(String door) {
try {
Door foo = findDoorByName(door);
return true;
}
catch (NoSuchElementException e) {
return false;
}
}
String getExitNames() {
String exitNames = "";
for (NiceEnumeration doorEnum = theDoors.elements(); doorEnum.hasMoreElements(); ) {
Door currDoor = (Door) doorEnum.nextElement();
String name = currDoor.getDirectionName();
exitNames += (currDoor.isOpen() ? name : "(" + name + ")") + " ";
}
return ((exitNames.length() > 0) ? exitNames : "nenhuma");
}
void insertDoor(Door dr) {
theDoors.insert(dr);
}
//////////////////////////
// Implementação de Place:
public String getTitle() {
return theTitle;
}
public String getPlaceType() {
return "sala";
}
public Zone getZone() {
return theZone;
}
public boolean isRoom() {
return true;
}
public boolean isOpen() {
return Bit.isSet(properties, P_OPEN);
}
public NiceEnumeration getExits() {
return theDoors.elements();
}
public String getFullDescription(Char aChar) {
return aChar.buildColor(ColorMap.ROOM_TITLE) + theTitle + aChar.getNormalColor() + Separators.NL + theDescription + Separators.NL + "Saídas: " + getExitNames() + getItemBriefList() + getCharBriefList(aChar);
;
}
public void insertChar(Char ch) {
CloneAlgo.insert(theChars, ch);
}
public void removeChar(Char aChar)
throws NoSuchElementException {
theChars.remove(aChar);
}
public Char findCharByName(String name, int ind) throws NoSuchElementException {
return (Char) Searcher.linearSearch(theChars, new NiceKey(name), ind);
}
public Char findCharByName(IndexParser ip) throws NoSuchElementException {
return (Char) Searcher.linearSearch(theChars, new NiceKey(ip.getWord()), ip.getIndex());
}
public ShopCreature findShopKeeper() {
for (NiceEnumeration enum = theChars.elements(); enum.hasMoreElements(); ) {
Char ch = (Char) enum.nextElement();
if (ch instanceof ShopCreature)
return (ShopCreature) ch;
}
return null;
}
public String getItemBriefList() {
return CloneAlgo.getList(theItems, null, CloneAlgo.F_BRIEF);
}
public String getCharBriefList(Char except) {
return CloneAlgo.getList(theChars, except, CloneAlgo.F_BRIEF);
}
public Door findDoorByName(String door)
throws NoSuchElementException {
for (NiceEnumeration enum = theDoors.elements();
enum.hasMoreElements(); ) {
Door currDoor = (Door) enum.nextElement();
if (currDoor.leadsTo(door.substring(0,1)))
return currDoor;
}
throw new NoSuchElementException();
}
public Door findDoorByDir(int dir)
throws NoSuchElementException {
for (NiceEnumeration enum = theDoors.elements();
enum.hasMoreElements(); ) {
Door currDoor = (Door) enum.nextElement();
if (currDoor.getDirection() == dir)
return currDoor;
}
throw new NoSuchElementException();
}
NiceEnumeration getDoorEnum() {
return theDoors.elements();
}
// 2 personagens e 2 itens
public void action(String act, boolean hide,
Char ch, Char vict,
Item it1, Item it2) {
for (NiceEnumeration charEnum = theChars.elements(); charEnum.hasMoreElements(); ) {
Char currChar = (Char) charEnum.nextElement();
currChar.action(act, hide, ch, vict, it1, it2);
}
}
// 2 personagens e um 1 item
public void action(String act, boolean hide,
Char ch, Char vict,
Item it) {
action(act, hide, ch, vict, it, null);
}
// 2 personagens e nenhum item
public void action(String act, boolean hide,
Char ch, Char vict) {
action(act, hide, ch, vict, null, null);
}
// 1 personagem e 2 itens
public void action(String act, boolean hide,
Char ch,
Item it1, Item it2) {
action(act, hide, ch, null, it1, it2);
}
// 1 personagem e 1 item
public void action(String act, boolean hide,
Char ch,
Item it) {
action(act, hide, ch, null, it, null);
}
// 1 personagem e nenhum item
public void action(String act, boolean hide,
Char ch) {
action(act, hide, ch, null, null, null);
}
// 2 personagens e 2 itens
public void actionNotToChar(String act, boolean hide,
Char ch, Char vict,
Item it1, Item it2) {
for (NiceEnumeration charEnum = theChars.elements(); charEnum.hasMoreElements(); ) {
Char currChar = (Char) charEnum.nextElement();
if (currChar != ch)
currChar.action(act, hide, ch, vict, it1, it2);
}
}
// 2 personagens e 1 item
public void actionNotToChar(String act, boolean hide,
Char ch, Char vict,
Item it) {
actionNotToChar(act, hide, ch, vict, it, null);
}
// 2 personagens e nenhum item
public void actionNotToChar(String act, boolean hide,
Char ch, Char vict) {
actionNotToChar(act, hide, ch, vict, null, null);
}
// 1 personagem e 2 itens
public void actionNotToChar(String act, boolean hide,
Char ch,
Item it1, Item it2) {
actionNotToChar(act, hide, ch, null, it1, it2);
}
// 1 personagem e 1 item
public void actionNotToChar(String act, boolean hide,
Char ch, Item it) {
actionNotToChar(act, hide, ch, null, it, null);
}
// 1 personagem e nenhum item
public void actionNotToChar(String act, boolean hide,
Char ch) {
actionNotToChar(act, hide, ch, null, null, null);
}
//
//////////////////////////
//////////////////////////
// Implementação de Owner:
public int getId() {
return theId;
}
public String getOwnerType() {
return "sala";
}
private void addLoadOf(Item it) {
}
private void subLoadOf(Item it) {
}
public void insertItem(Item it) {
CloneAlgo.insert(theItems, it);
addLoadOf(it);
}
public void removeItem(Item it)
throws NoSuchElementException {
theItems.remove(it);
subLoadOf(it);
}
public Item findItemById(int itemId) throws NoSuchElementException {
return (Item) Searcher.linearSearch(theItems, new NiceKey(itemId));
}
public Item findItemByName(String name, int ind) throws NoSuchElementException {
return (Item) Searcher.linearSearch(theItems, new NiceKey(name), ind);
}
public NiceQueue findItemsByName(IndexParser ip) {
return SearcherAdaptor.findItemsByName(theItems, ip);
}
public String getItemList() {
return CloneAlgo.getList(theItems, null, CloneAlgo.F_NAME);
}
public boolean hasFreeLoad(int load) {
return true;
}
public void extractItems() {
for (NiceEnumeration items = theItems.elements(); items.hasMoreElements(); ) {
Item curr = (Item) items.nextElement();
Owner ow = curr.getContainer();
if (ow != null)
ow.extractItems();
theWorld.removeItem(curr);
}
}
public Owner getOwner() {
return null;
}
public BoardItem findBoard() throws NoSuchElementException {
for (NiceEnumeration enum = theItems.elements(); enum.hasMoreElements(); ) {
Item it = (Item) enum.nextElement();
if (it instanceof BoardItem)
return (BoardItem) it;
}
throw new NoSuchElementException();
}
public NiceEnumeration getContents() {
return theItems.elements();
}
//
//////////////////////////
////////////
// Niceable:
public boolean lessThan(Niceable n) {
return getId() < ((Room) n).getId();
}
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 Room.hasKey(...): " + key.getKeyType());
}
return result;
}
public boolean keyLessThan(NiceKey key) {
boolean result = false;
switch(key.getKeyType()) {
case NiceKey.K_INT:
result = getId() < key.getIntKey();
break;
default:
Log.err("Tipo inválido de chave em Room.keyLessThan(...): " + key.getKeyType());
}
return result;
}
//
////////////
String getItemNameCommaList() {
return StrUtil.wrapList(CloneAlgo.getHeadedList(theItems, null, CloneAlgo.F_NAME, ", "), "Nenhum");
}
String getCharNameCommaList() {
return StrUtil.wrapList(CloneAlgo.getHeadedList(theChars, null, CloneAlgo.F_NAME, ", "), "Nenhum");
}
String getDoorList() {
String doorList = "";
for (NiceEnumeration doorEnum = theDoors.elements(); doorEnum.hasMoreElements(); ) {
Door currDoor = (Door) doorEnum.nextElement();
doorList += Separators.NL + currDoor.getSheet();
}
return doorList.length() == 0 ? "Nenhuma" : doorList;
}
public String getSheet() {
return "SALA - ID: [" + getId() + "] " + "Título: [" + getTitle() + "]" + Separators.NL +
"Sinônimos: " + theAliases + Separators.NL +
"Propriedades: " + StrUtil.niceListFlags(properties, propLabels, "Nenhuma") + Separators.NL +
"Itens: " + getItemNameCommaList() + Separators.NL +
"Personagens: " + getCharNameCommaList() + Separators.NL +
"Portas:" + getDoorList();
}
}