Item.java

jmud-0.11/src/Item.java · 11.5 KB

From the archive jmud-0_11.tgz

Download the archive   « Back to the archive contents

import java.util.NoSuchElementException;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;

import util.bit.Bit;
import string.StrUtil;
import misc.Separators;
import clone.Clone;
import util.nice.Niceable;
import util.nice.NiceKey;
import util.log.Log;
import util.file.InvalidFileFormatException;

class Item implements Sheetable, Clone, Niceable {

  static protected World theWorld = null;

  static void setWorld(World wld) {
    theWorld = wld;
  }

    static final int T_NONE      = 0;
    static final int T_WEAPON    = 1;
    static final int T_ARMOR     = 2;
    static final int T_CONTAINER = 3;
    static final int T_BOARD     = 4;
    static final int T_KEY       = 5;
    static final int T_CURRENCY  = 6;

    static final int F_NONE      = Bit.BIT0; // a
    static final int F_WEAPON    = Bit.BIT1; // b
    static final int F_ARMOR     = Bit.BIT2; // c
    static final int F_CONTAINER = Bit.BIT3; // d
    static final int F_BOARD     = Bit.BIT4; // e
    static final int F_KEY       = Bit.BIT5; // f
    static final int F_CURRENCY  = Bit.BIT6; // g
    
    static final String typeLabels[] = {
	"genérico",
	"arma",
	"armadura",
	"recipiente",
	"quadro",
	"chave",
	"moeda"
    };

    static final int P_CANTAKE   = 0; // a
    static final int P_WEARALL   = 1; // b
    static final int P_INVISIBLE = 2; // c
    static final int P_GLOWING   = 3; // d
    static final int P_HUMMING   = 4; // e
    static final int P_PHYSICAL  = 5; // f
    static final int P_MENTAL    = 6; // g
    static final int P_MYSTIC    = 7; // h
    static final int P_VOLATILE  = 8; // i  --  não pode ser salvo
    static final int P_STATIC    = 9; // j  --  não pode ser modificado

    static final int PM_CANTAKE   = 1 << P_CANTAKE;
    static final int PM_WEARALL   = 1 << P_WEARALL;
    static final int PM_INVISIBLE = 1 << P_INVISIBLE;
    static final int PM_GLOWING   = 1 << P_GLOWING;
    static final int PM_HUMMING   = 1 << P_HUMMING;
    static final int PM_PHYSICAL  = 1 << P_PHYSICAL;
    static final int PM_MENTAL    = 1 << P_MENTAL;
    static final int PM_MYSTIC    = 1 << P_MYSTIC;
    static final int PM_VOLATILE  = 1 << P_VOLATILE;
    static final int PM_STATIC    = 1 << P_STATIC;


    static final String propLabels[] = {
	"pegável",
	"multi-veste",
	"invisível",
	"brilho",
	"zumbido",
	"físico",
	"mental",
	"místico",
	"volátil",
	"estático"
    };

    static final int W_LHOLD     = 0; // a
    static final int W_RHOLD     = 1; // b
    static final int W_HEAD      = 2; // c
    static final int W_NECK      = 3; // d
    static final int W_BODY      = 4; // e
    static final int W_WAIST     = 5; // f
    static final int W_AROUND    = 6; // g
    static final int W_LSHOULDER = 7; // h
    static final int W_RSHOULDER = 8; // i
    static final int W_LARM      = 9; // j
    static final int W_RARM      = 10; // k 
    static final int W_LWRIST    = 11; // l
    static final int W_RWRIST    = 12; // m
    static final int W_LHAND     = 13; // n
    static final int W_RHAND     = 14; // o
    static final int W_LFINGER   = 15; // p
    static final int W_RFINGER   = 16; // q
    static final int W_LLEG      = 17; // r
    static final int W_RLEG      = 18; // s
    static final int W_LFOOT     = 19; // t
    static final int W_RFOOT     = 20; // u

    static final int WEAR_POSITIONS = 21;

  static final String wearPositions[] = {
    "segurando",         // 0
    "segurando",
    "cabeça",
    "pescoço",
    "tronco",         
    "cintura",           // 5
    "ao redor",
    "ombro esquerdo",
    "ombro direito",
    "braço esquerdo",    
    "braço direito",     // 10
    "pulso esquerdo",
    "pulso direito",
    "mão esquerda",
    "mão direita",       
    "dedo esquerdo",     // 15
    "dedo direito",
    "perna esquerda",
    "perna direita",
    "pé esquerdo",       
    "pé direito"         // 20
  };

  static final String wearKeywords[] = {
    "empunhadura",      // 0
    "empunhadura",
    "cabeca",
    "pescoco",
    "tronco",           
    "cintura",          // 5
    "aoredor",
    "ombroesquerdo",
    "ombrodireito",
    "bracoesquerdo",   
    "bracodireito",     // 10
    "pulsoesquerdo",
    "pulsodireito",
    "maoesquerda",
    "maodireita",      
    "dedoesquerdo",     // 15
    "dedodireito",
    "pernaesquerda",
    "pernadireita",
    "peesquerdo",       
    "pedireito"         // 20
  };

    static final String wearAction[] = {
	"segura",           // 0
	"segura",
	"coloca",
	"prende",
	"veste", 
	"prende",           // 5
	"veste",
	"prende",
	"prende",
	"veste",            
	"veste",            // 10
	"prende",
	"prende",
	"veste",
	"veste",          
	"põe",              // 15
	"põe",
	"veste",
	"veste",
	"calça",         
	"calça"             // 20
    };


  protected ItemProto  prototype      = null;
  private   int        theId;
  private   String     theName        = null;
  private   String     theAliases     = null;
  private   String     theBrief       = null;
  private   String     theDescription = null;

  private   int        theLevel;
  protected int        theLoad;
  private   int        theCost;

  private   int        properties;
  private   int        wearVector;

  private   Owner      theOwner       = null;
  private   Char       equipper       = null;

  Item(ItemProto pro) {
    prototype      = pro;
    theId          = pro.theId;
    theName        = pro.theName;
    theAliases     = pro.theAliases;
    theBrief       = pro.theBrief;
    theDescription = pro.theDescription;
    theLevel       = pro.theLevel;
    theLoad        = pro.theLoad;
    theCost        = pro.theCost;
    properties     = pro.properties;
    wearVector     = pro.wearVector;
    ++prototype.numberOfItems;
  }

  protected void finalize() {
    --prototype.numberOfItems;
  }

  int getType() {
    return T_NONE;
  }

    String getItemType() {
	return typeLabels[getType()]; 
    }

  public String getName() {
    return theName;
  }

  public String getBrief() {
    return theBrief;
  }

  String getDescription() {
    return theDescription;
  }

  String getFullDescription() {
    return theDescription;
  }

  int getId() {
    return theId;
  }

  boolean hasId(int itemId) {
    return itemId == theId;
  }

  boolean hasName(String name) {
    return StrUtil.findAlias(name, theAliases);
  }

  int getLoad() {
    return theLoad;
  }

    int getValue() {
	return theCost;
    }

    int getPrice(float tax) {
	return Math.round(tax * (float) getValue());
    }

  // Esta á a maneira certa de entregar um item que está no "ar"
  // (sem dono) para um Owner
  void giveTo(Owner aOwner) {
    theOwner = aOwner;
    theOwner.insertItem(this);
  }

  // Esta é a maneira certa de equipar um personagem com este item
  void equip(Char eq, int wearPos) {
    drop();
    equipper = eq;
    equipper.wearOn(this, wearPos);
  }

    void equip(Char eqpr) {
	drop();
	equipper = eqpr;
	equipper.wear(this);
    }

  // Esta é a maneira correta de transferir um equipamento para o inventário
  void unequip() {
    Owner ow = equipper;
    drop();
    giveTo(ow);
  }

  // Esta é a maneira certa de soltar um item/eq e deixá-lo no "ar"
  // (sem dono)
  void drop() {
    if (equipper != null) {
      try {
	equipper.takeOffEq(this);
	equipper = null;
      }
      catch(NoSuchElementException e) {
	Log.err("Equipamento '" + getName() + "' [" + getId() + "] não encontrado ao ser removido de: " + equipper.getName() + "[" + equipper.getId() + "]");
      }
    }
    if (theOwner != null) {
      try {
	theOwner.removeItem(this);
	theOwner = null;
      }
      catch(NoSuchElementException e) {
	Log.err("Item '" + getName() + "' [" + getId() + "] não encontrado ao ser tomado de: " + theOwner.getOwnerType() + " [" + theOwner.getId() + "]");
      }
    }
  }

  // Esta é a maneira certa de transferir itens entre Owners
  void transferTo(Owner target) {
    drop();
    giveTo(target);
  }

    boolean wearAll() {
	return Bit.isSet(properties, 1 << P_WEARALL);
    }

    int getWearVector() {
	return wearVector;
    }

  Owner getContainer() {
    return null;
  }

  /////////////
  // Sheetable:

  private String getOwnerList() {
    String owners = "";
    Owner ow = getOwner();
    if (ow != null) {
      owners += ow.getOwnerType() + " [" + ow.getId() + "]";
      while ((ow = ow.getOwner()) != null)
	owners += ", " + ow.getOwnerType() + " [" + ow.getId() +"]";
    }
    return owners;
  }

    private String getPropList() {
	return StrUtil.listFlags(properties, propLabels);
    }

    private String getPositionList() {
	return StrUtil.listFlags(wearVector, wearPositions);
    }

  public String getSheet() {
      String props     = getPropList();
      String positions = getPositionList();

    return "ITEM - ID: [" + theId + "]  Nome: [" + theName + "]  Tipo: [" + getItemType() + "]" + Separators.NL +
      "Sinônimos: " + theAliases + Separators.NL +
      "Nível: [" + theLevel + "]  Peso: [" + theLoad + "]  Valor: [" + theCost + "]" + Separators.NL +
	"Propriedades: " + (props == "" ? "Nenhuma" : props) + Separators.NL +
      "Posições: " + ((positions == "") ? "Nenhuma" : positions) + Separators.NL +
      "Dono: " + ((theOwner == null)? "Nenhum" : getOwnerList()) + Separators.NL +
      "Equipado por: " + ((equipper == null)? " Ninguém" : (equipper.getName() + " [" + equipper.getId() + "]"));
  }

  public Owner getOwner() {
    return theOwner;
  }

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

  //////////////
  // Clone:

  public boolean isCloneOf(Clone c) {
    return c instanceof Item && getId() == ((Item) c).getId();
  }

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

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

  public boolean lessThan(Niceable n) {
    return getId() < ((Item) 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 Item.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;
    case NiceKey.K_STR: 
      result = getName().compareTo(key.getStrKey()) < 0;
      break;
    default:
      Log.err("Tipo inválido de chave em Item.keyLessThan(...): " + key.getKeyType());
    }
    return result;
  }
  
  //
  //////////////

    void save(BufferedWriter writer) throws IOException {
	writer.write(Separators.BOR); writer.write(String.valueOf(theId)); writer.newLine();
	writer.write(String.valueOf(getType())); writer.newLine();
	writer.write(theName); writer.newLine();
	writer.write(theAliases); writer.newLine();
	writer.write(theBrief); writer.newLine();
	StrUtil.writeDescription(writer, theDescription);
	writer.write(theLevel + " " + theLoad + " " + theCost + " " + StrUtil.makeFlags(properties) + " " + StrUtil.makeFlags(wearVector)); writer.newLine();
    }

    static Item load(int itemId, BufferedReader itemFile, ItemProto itPro)
	throws InvalidFileFormatException {
	Item it = ItemProto.load(itemId, 0, itemFile).create();
	if (itPro != null) {
	    it.prototype = itPro;
	    ++itPro.numberOfItems;
	}
	return it;
    }

    ////////////////////////////////////
    // Ha metodos identicos em ItemProto

    boolean isVolatile() {
	return Bit.isSet(properties, PM_VOLATILE);
    }

    boolean isStatic() {
	return Bit.isSet(properties, PM_STATIC);
    }

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

    void affectEquipper(Char ch) {
    }

    void unnaffectEquipper(Char ch) {
    }
}

Site Stats

Snippets: 1791
Codebases: 571