Creature.java

jmud-0.11/src/Creature.java · 5.0 KB

From the archive jmud-0_11.tgz

Download the archive   « Back to the archive contents

import java.io.IOException;
import java.util.Random;
import java.util.NoSuchElementException;

import string.StrUtil;
import misc.Separators;
import util.log.Log;
import util.nice.NiceEnumeration;
import util.bit.Bit;

class Creature extends Char {

    static final int P_WALKER = Bit.BIT0;

    static final String propLabels[] = {
	"andarilho"
    };

    static final int T_HUMAN = 0;
    static final int T_BEAST = 1;

    static final String typeLabels[] = {
	"humano",
	"animal"
    };

    private CreatureProto prototype = null;
    private int           theId;
    private int           theType;
    private String        theAliases = null;
    private String        theBrief   = null;
    private int           theProperties;
  
  Creature(CreatureProto cr) {
    prototype      = cr;
    theType        = cr.theType;
    theId          = cr.theId;
    theName        = cr.theName;
    theAliases     = cr.theAliases;
    theBrief       = cr.theBrief;
    theDescription = cr.theDescription;
    theLevel       = cr.theLevel;
    sex            = cr.sex;
    theProperties  = cr.theProperties;
    money          = cr.money;

    readAttr();
    randAttr();
    attrToReal();

    ++prototype.numberOfCreatures;
  }

    void readAttr() {
	for (int i = 0; i < realAttr.length; ++i)
	    attr[i] = prototype.attr[i];
	
	stam = getAttr(A_STA);
	will = getAttr(A_WIL);
	soul = getAttr(A_SOU);
	conf = getAttr(A_CON);
    }

    private int rndChange(int mode, int value) {
	int change = 0;

	int x = Rnd.nextInt();
	if (x < 0)
	    x = -x;
	switch(mode) {
	case RandAttr.M_CHANGE:
	    change = (x % ((value << 1) + 1)) - value;
	    break;
	case RandAttr.M_ADD:
	    change = x % (value + 1);
	    break;
	case RandAttr.M_SUB:
	    change = -(x % (value + 1));
	    break;
	default:
	    Log.err("Modo inválido de modificador aleatório: " + mode);
	}

	return change;
    }
    
    void randAttr() {
	for (NiceEnumeration enum = prototype.randModifiers.elements(); enum.hasMoreElements(); ) {
	    RandAttr ra = (RandAttr) enum.nextElement();
	    
	    int target = ra.getTarget();
	    int which = ra.getWhich();
	    int value = ra.getValue();
	    int mode = ra.getMode();

	    switch(target) {
	    case RandAttr.T_ALL:
		for (int i = 0; i < 20; ++i)
		    addAttr(i, rndChange(mode, value));
		break;
	    case RandAttr.T_ATTR:
		addAttr(which, rndChange(mode, value));
		break;
	    case RandAttr.T_ROW:
		for (int c = 0; c < 4; ++c)
		    addAttr(which, c, rndChange(mode, value));
		break;
	    case RandAttr.T_COL:
		for (int r = 0; r < 5; ++r)
		    addAttr(r, which, rndChange(mode, value));
		break;
	    default:
		Log.err("Tipo inválido de modificador aleatório: " + target);
	    }
	}
    }

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

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

  public String getBrief() {
    return theBrief;
  }

  String getPrompt() {
    return "creature [" + getId() + "] " + super.getPrompt();
  }

  boolean canExecute(Command cmd) {
    return Char.R_NEWBIE >= cmd.getMinRank();
  }

  boolean canCast(Magic mag) {
    return false;
  }

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

  boolean isFirstMonitor() {
    return false;
  }

  int getRank() {
    return Char.R_DISABLED;
  }

    String getColor(int color) {
	return "";
    }

    String buildColor(int colorVector) {
	return "";
    }

    String getNormalColor() {
	return "";
    }

  void promote(int aRank) {
    Log.err("Tentativa de promover uma criatura: " + theId + ", " + theName);
  }

    ////
    //

    private void doWalk() {

	if (getPosition() != P_STANDING)
	    return;

	Place plc = getPlace();
	if (plc.isRoom()) {
	    Room rm = (Room) plc;
	    int dir = DicePool.d6.roll() - 1;

	    try {
		Door dr = rm.findDoorByDir(dir);
		if (dr.isOpen()) {
		    walkTo(dr.getDestinationRoom(), dir);
		    Log.debug("Andou: '" + getName() + "'");
		}
	    }
	    catch (NoSuchElementException e) {
	    }
	}
    }

    // chamado a cada hora do mundo
    void doAction() {
	if (Bit.isSet(theProperties, P_WALKER))
	    doWalk();
    }

    //
    ////

  boolean isPlayer() {
    return false;
  }

  void save(int id, Config aConfig) throws IOException {
    Log.err("Tentativa de gravar uma criatura: " + theId + ", " + theName);
  }

  /////////
  // Owner:

  public int getId() {
    return theId;
  }

  public String getOwnerType() {
    return "criatura";
  }

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

  public String getSheet() {
    return "CRIATURA - ID: [" + theId + "]  Posto: " + rankNames[getRank()] + " [" + getRank() + "]  Nível: [" + theLevel  + "]  Tipo: " +  StrUtil.strTable(theType, typeLabels) + " [" + theType + "]"+ Separators.NL +
	"Nome: [" + theName + "]" + Separators.NL +
	"Sinônimos: " + theAliases + Separators.NL +
	"Propriedades: " + StrUtil.wrapList(StrUtil.listFlags(theProperties, propLabels), "Nenhuma") + Separators.NL +
	"Onde: " + thePlace.getTitle() + " (" + thePlace.getPlaceType() + " [" + thePlace.getId() + "])" + Separators.NL + super.getSheet();
  }

}

Site Stats

Snippets: 1791
Codebases: 571