CreatureProto.java

jmud-0.11/src/CreatureProto.java · 8.8 KB

From the archive jmud-0_11.tgz

Download the archive   « Back to the archive contents

import java.io.IOException;
import java.io.BufferedReader;
import java.util.StringTokenizer;

import string.StrUtil;
import string.InvalidFlagException;
import util.file.InvalidFileFormatException;
import util.nice.Niceable;
import util.nice.NiceKey;
import util.log.Log;
import util.nice.NiceVector;
import util.nice.NiceEnumeration;
import misc.Separators;

class CreatureProto implements Sheetable, Niceable {

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

    int    theId;
    int    theType;
    String theName        = null;
    String theAliases     = null;
    String theBrief       = null;
    String theDescription = null;
    int    theLevel;
    int    sex;
    int    theProperties; 

    int    money[] = {
	0,  // 0 - ouro
	0,  // 1 - prata
	0   // 2 - bronze
    };

  int attr[] = {
    5, 5, 5, 5,
    5, 5, 5, 5, 
    5, 5, 5, 5, 
    5, 5, 5, 5,
    5, 5, 5, 5
  };

    NiceVector randModifiers = new NiceVector();

  int    numberOfCreatures = 0;

  static private int numberOfProtos = 0;

  CreatureProto(int zoneBase, int creatId, int creatType, BufferedReader creatFile) 
    throws InvalidFileFormatException {
    theId = creatId + zoneBase;

    theType = creatType;

    try {
      theName = creatFile.readLine();
      
      Log.log("Criatura: " + theName);
      
      theAliases = StrUtil.loadAliases(creatFile);
      theBrief   = creatFile.readLine();
      
      theDescription = StrUtil.readDescription(creatFile);

      StringTokenizer tok = new StringTokenizer(creatFile.readLine());

      try {
	  if (!tok.hasMoreTokens())
	      throw new InvalidFileFormatException();
	  theLevel = Integer.parseInt(tok.nextToken());

	  if (!tok.hasMoreTokens())
	      throw new InvalidFileFormatException();
	  sex = Integer.parseInt(tok.nextToken());
	  
	  if (!tok.hasMoreTokens())
	      throw new InvalidFileFormatException();
	  theProperties = StrUtil.parseFlags(tok.nextToken());
      }
      catch(InvalidFlagException e) {
	  throw new InvalidFileFormatException();
      }

      // moedas
      tok = new StringTokenizer(creatFile.readLine());
      for (int i = 0; i < money.length; ++i) {
	  if (!tok.hasMoreTokens())
	      break;
	  money[i] = Integer.parseInt(tok.nextToken());
      }

      // atributos
      for (; ; ) {
	  if (!creatFile.ready())
	      throw new InvalidFileFormatException();

	  String line = creatFile.readLine();
	  if (line.startsWith(Separators.EOR))
	      break;

	  tok = new StringTokenizer(line);
	  if (!tok.hasMoreTokens())
	      throw new InvalidFileFormatException();

	  String opt = tok.nextToken();

	  if (opt.equals("t")) {
	      setAllAttr(Integer.parseInt(tok.nextToken()));
	  }
	  else if (opt.equals("a")) {
	      int at = Integer.parseInt(tok.nextToken());
	      int v  = Integer.parseInt(tok.nextToken());
	      setAttr(at, v);
	  }
	  else if (opt.equals("l")) {
	      int li = Integer.parseInt(tok.nextToken());
	      int v  = Integer.parseInt(tok.nextToken());
	      setAttrRow(li, v);
	  }
	  else if (opt.equals("c")) {
	      int co = Integer.parseInt(tok.nextToken());
	      int v  = Integer.parseInt(tok.nextToken());
	      setAttrColumn(co, v);
	  }

	  else if (opt.equals("fis")) {
	      for (int i = 0; i < 5; ++i)
		  setAttr(i, Char.C_PHY, Integer.parseInt(tok.nextToken()));
	  }
	  else if (opt.equals("men")) {
	      for (int i = 0; i < 5; ++i)
		  setAttr(i, Char.C_MEN, Integer.parseInt(tok.nextToken()));
	  }
	  else if (opt.equals("mis")) {
	      for (int i = 0; i < 5; ++i)
		  setAttr(i, Char.C_MYS, Integer.parseInt(tok.nextToken()));
	  }
	  else if (opt.equals("soc")) {
	      for (int i = 0; i < 5; ++i)
		  setAttr(i, Char.C_SOC, Integer.parseInt(tok.nextToken()));
	  }

	  else if (opt.equals("per")) {
	      for (int i = 0; i < 4; ++i)
		  setAttr(Char.R_PER, i, Integer.parseInt(tok.nextToken()));
	  }
	  else if (opt.equals("agi")) {
	      for (int i = 0; i < 4; ++i)
		  setAttr(Char.R_AGI, i, Integer.parseInt(tok.nextToken()));
	  }
	  else if (opt.equals("aca")) {
	      for (int i = 0; i < 4; ++i)
		  setAttr(Char.R_ACT, i, Integer.parseInt(tok.nextToken()));
	  }
	  else if (opt.equals("res")) {
	      for (int i = 0; i < 4; ++i)
		  setAttr(Char.R_RES, i, Integer.parseInt(tok.nextToken()));
	  }
	  else if (opt.equals("vit")) {
	      for (int i = 0; i < 4; ++i)
		  setAttr(Char.R_VIT, i, Integer.parseInt(tok.nextToken()));
	  }

	  else
	      throw new InvalidFileFormatException();
      }

      // modificadores aleatórios
      for (; ; ) {
	  if (!creatFile.ready())
	      throw new InvalidFileFormatException();

	  String line = creatFile.readLine();
	  if (line.startsWith(Separators.EOR))
	      break;

	  tok = new StringTokenizer(line);
	  if (!tok.hasMoreTokens())
	      throw new InvalidFileFormatException();

	  String opt = tok.nextToken();

	  int target, which, value, mode;

	  if (opt.equals("t")) {
	      target = RandAttr.T_ALL;
	      which  = 0;
	  }
	  else if (opt.equals("a")) {
	      target = RandAttr.T_ATTR;
	      which  = Integer.parseInt(tok.nextToken());
	  }
	  else if (opt.equals("l")) {
	      target = RandAttr.T_ROW;
	      which  = Integer.parseInt(tok.nextToken());
	  }
	  else if (opt.equals("c")) {
	      target = RandAttr.T_COL;
	      which  = Integer.parseInt(tok.nextToken());
	  }

	  else
	      throw new InvalidFileFormatException();

	  value = Integer.parseInt(tok.nextToken());

	  mode = RandAttr.M_CHANGE;
	  if (tok.hasMoreTokens()) {
	      String m = tok.nextToken();
	      if (m.equals("+"))
		  mode = RandAttr.M_ADD;
	      else if (m.equals("-"))
		  mode = RandAttr.M_SUB;
	  }
	      
	  randModifiers.insert(new RandAttr(target, which, value, mode));
      }
      randModifiers.trim();

    }
    catch(IOException e) {
      throw new InvalidFileFormatException();
    }

    ++numberOfProtos;
  }

  int getAttr(int row, int column) {
    return attr[row * 4 + column];
  }

  int getAttr(int ind) {
    return attr[ind];
  }

  void setAttr(int row, int column, int val) {
    attr[row * 4 + column] = val;
  }

  void setAttr(int ind, int val) {
    attr[ind] = val;
  }

    void setAttrRow(int row, int val) {
	for (int i = 0; i < 5; ++i)
	    setAttr(row, i, val);
    }

    void setAttrColumn(int column, int val) {
	for (int i = 0; i < 4; ++i)
	    setAttr(i, column, val);
    }

    void setAllAttr(int v) {
	for (int i = 0; i < attr.length; ++i)
	    attr[i] = v;
    }

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

  public boolean lessThan(Niceable n) {
    return getId() < ((CreatureProto) 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 CreatureProto.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 CreatureProto.keyLessThan(...): " + key.getKeyType());
    }
    return result;
  }

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

  protected void finalize() {
    --numberOfProtos;
  }

  Creature create() {
      Shop sh = theWorld.findShopByKeeperId(theId);

      if (sh == null)
	  return new Creature(this);
      
      ShopCreature sc = new ShopCreature(this);

      sc.bindShop(sh);

      return sc;
  }

  int getNumberOfCreatures() {
    return numberOfCreatures;
  }

  public int getId() {
    return theId;
  }

  public String getName() {
    return theName;
  }

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

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

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

    private String randModList() {
	String list = "";
	for (NiceEnumeration enum = randModifiers.elements(); enum.hasMoreElements(); ) {
	    RandAttr ra = (RandAttr) enum.nextElement();
	    list += Separators.NL + ra.getSheet();
	}
	return list;
    }

    private String getSheetLine(int row) {
	String line = "";
	for (int c = 0; c < 4; ++c) {
	    int i = row * 4 + c;
	    line += StrUtil.leftWidth(Char.attrLabels[i], 3) + ": " + StrUtil.formatNumber(attr[i], 3) + "  ";
	}
	return line;
     }

    private String getSheetAttr() {
	String sh = "";
	for (int r = 0; r < 5; ++r)
	    sh += Separators.NL + getSheetLine(r);
	return sh;
    }

  public String getSheet() {
    return "CRIATURA - ID: [" + getId() + "]  Nível: [" + theLevel  + "]  Tipo: [" + theType + "]" + Separators.NL +
	"Nome: [" + theName + "]" + Separators.NL +
	"Sinônimos: " + theAliases + Separators.NL +
	"Propriedades: " + StrUtil.makeFlags(theProperties) + Separators.NL +
	"Quantidade: [" + numberOfCreatures + "]" + 
	getSheetAttr() + randModList();
  }

}

Site Stats

Snippets: 1791
Codebases: 571