Search in sources :

Example 1 with ReadXML

use of gmgen.io.ReadXML in project pcgen by PCGen.

the class EncounterPlugin method generateXofYEL.

/**
	 * Generates creatures for an encounter based on a specified Encounter
	 * Level and number of creatures.
	 * @param size the number of creatures needed for encounter.
	 * @param totalEL total experience level.
	 */
private void generateXofYEL(String size, String totalEL) {
    File f = new File(getDataDirectory() + File.separator + DIR_ENCOUNTER + File.separator + "4_1.xml");
    ReadXML xml;
    VectorTable table41;
    Random roll = new Random(System.currentTimeMillis());
    List<Race> critters = new ArrayList<>();
    if (!f.exists()) {
        //$NON-NLS-1$
        Logging.errorPrintLocalised("in_plugin_encounter_error_missing", f);
        return;
    }
    xml = new ReadXML(f);
    if ((table41 = xml.getTable()) == null) {
        Logging.errorPrint("ACK! error getting table41! " + f.toString());
        return;
    }
    xml = null;
    f = null;
    // verrify values on the table.
    String crs = (String) table41.crossReference(totalEL, size);
    table41 = null;
    if (crs == null) {
        Logging.errorPrint("Tables do not match the given parameters (" + totalEL + ", " + size + ')');
        return;
    }
    Formula crFormula = FormulaFactory.getFormulaFor(crs);
    if (!crFormula.isValid()) {
        Logging.errorPrint("CR Formula " + crs + " was not valid: " + crFormula.toString());
    }
    ChallengeRating cr = new ChallengeRating(crFormula);
    // populate critters with a list of matching monsters with the right CR.
    for (final Race race : Globals.getContext().getReferenceContext().getConstructedCDOMObjects(Race.class)) {
        if (cr.equals(race.get(ObjectKey.CHALLENGE_RATING))) {
            critters.add(race);
        }
    }
    int i = roll.nextInt(critters.size());
    for (int x = 0; x < Integer.parseInt(size); x++) {
        theModel.addElement(critters.get(i).toString());
    }
}
Also used : Formula(pcgen.base.formula.Formula) ChallengeRating(pcgen.cdom.content.ChallengeRating) Random(java.util.Random) Race(pcgen.core.Race) ArrayList(java.util.ArrayList) File(java.io.File) VectorTable(gmgen.io.VectorTable) ReadXML(gmgen.io.ReadXML)

Example 2 with ReadXML

use of gmgen.io.ReadXML in project pcgen by PCGen.

the class EncounterPlugin method getMonsterFromTable.

/**
	 * Gets a monster from the table specified.
	 * @param table the table that the creature will come from.
	 * @return the creature(s).
	 * @throws FileNotFoundException an exception if there is a non-existant
	 *         file.
	 */
private Vector<?> getMonsterFromTable(String table) {
    String tablePath;
    String tableEntry;
    String numMonsters;
    Random roll = new Random(System.currentTimeMillis());
    if (table.startsWith("[")) {
        tablePath = getDataDirectory() + File.separator + DIR_ENCOUNTER + File.separator + table.substring(1, table.length() - 1);
        Logging.errorPrint("subfile " + tablePath);
    } else {
        tablePath = table;
    }
    tablePath = tablePath.concat(".xml");
    /*open file*/
    File monsterFile = new File(tablePath);
    if (!monsterFile.exists()) {
        Logging.errorPrint("could not open " + tablePath);
        return null;
    }
    ReadXML monsterTable = new ReadXML(monsterFile);
    String percent = monsterTable.findPercentageEntry(roll.nextInt(99) + 1);
    /*get item type*/
    tableEntry = monsterTable.getTable().crossReference(percent, "Monster").toString();
    /*get amount of items*/
    numMonsters = monsterTable.getTable().crossReference(percent, "Number").toString();
    /*create items and add to list*/
    if (tableEntry.startsWith("[")) {
        return getMonsterFromTable(tableEntry.substring(1, tableEntry.length() - 1));
    }
    //TODO This calculation should be done as int and convert to Integer at the end - better speed. thpr 10/19/06
    Integer num;
    try {
        num = Integer.valueOf(numMonsters);
    } catch (NumberFormatException e) {
        String[] dice = numMonsters.split("d");
        num = 0;
        for (int x = 0; x < Integer.parseInt(dice[0]); x++) {
            num += roll.nextInt(Integer.parseInt(dice[1])) + 1;
        }
    }
    Vector<Object> toReturn = new Vector<>();
    toReturn.addElement(num);
    toReturn.addElement(Globals.getContext().getReferenceContext().silentlyGetConstructedCDOMObject(Race.class, tableEntry));
    return toReturn;
}
Also used : Random(java.util.Random) Race(pcgen.core.Race) File(java.io.File) Vector(java.util.Vector) ReadXML(gmgen.io.ReadXML)

Example 3 with ReadXML

use of gmgen.io.ReadXML in project pcgen by PCGen.

the class EncounterPlugin method handleGenerateEncounter.

/**
	 * Handles the <b>Generate Encounter</b> button.
	 * @param m the encounter model.
	 */
public void handleGenerateEncounter(EncounterModel m) {
    File f = new File(getDataDirectory() + File.separator + DIR_ENCOUNTER + File.separator + "environments.xml");
    ReadXML xml;
    if (f.exists()) {
        xml = new ReadXML(f);
    } else {
        //$NON-NLS-1$
        Logging.errorPrintLocalised("in_plugin_encounter_error_missing", f);
        return;
    }
    VectorTable environments = xml.getTable();
    theModel.clear();
    if (theView.getEnvironment().getSelectedIndex() == 0) {
        generateXofYEL(theView.getNumberOfCreatures().getText(), theView.getTargetEL());
    } else {
        generateXfromY(environments.crossReference(theView.getEnvironment().getSelectedItem().toString(), "File").toString());
    }
    updateUI();
}
Also used : File(java.io.File) VectorTable(gmgen.io.VectorTable) ReadXML(gmgen.io.ReadXML)

Example 4 with ReadXML

use of gmgen.io.ReadXML in project pcgen by PCGen.

the class EnvironmentModel method update.

/**
	 * Update the model
	 */
public void update() {
    VectorTable table;
    ReadXML reader;
    //$NON-NLS-1$
    File f = new File(dir, "environments.xml");
    this.removeAllElements();
    if (!f.exists()) {
        // TODO Make it so that the view also indicate that the file is missing.
        //$NON-NLS-1$
        Logging.errorPrintLocalised("in_plugin_encounter_error_missing", f);
        return;
    }
    reader = new ReadXML(f);
    table = reader.getTable();
    //$NON-NLS-1$
    this.addElement(LanguageBundle.getString("in_plugin_encounter_generic"));
    for (int x = 1; x < table.size(); x++) {
        try {
            this.addElement(((Vector) table.get(x)).firstElement());
        } catch (NoSuchElementException e) {
            break;
        }
    }
}
Also used : VectorTable(gmgen.io.VectorTable) File(java.io.File) NoSuchElementException(java.util.NoSuchElementException) ReadXML(gmgen.io.ReadXML)

Example 5 with ReadXML

use of gmgen.io.ReadXML in project pcgen by PCGen.

the class ExperienceAdjusterModel method getPartyTotalExperience.

/**
	 * Get party total experience
	 * @return party total experience
	 */
public int getPartyTotalExperience() {
    float enemyCR;
    int tableCR;
    int experience = 0;
    File experienceFolder = new File(dir, "experience_tables");
    File experienceFile = new File(experienceFolder, "7_1.xml");
    // Lets not load the massive XML file more than we have to
    if (experienceTable == null) {
        experienceTable = new ReadXML(experienceFile);
    }
    for (int i = 0; i < enemies.size(); i++) {
        ExperienceListItem item = (ExperienceListItem) enemies.get(i);
        enemyCR = item.getCombatant().getCR();
        if (enemyCR < 1) {
            tableCR = 1;
        } else {
            tableCR = (int) enemyCR;
        }
        String xp = (String) experienceTable.getTable().crossReference(Integer.toString(party.averageCR()), Integer.toString(tableCR));
        try {
            if (enemyCR < 1) {
                experience += (int) (Float.parseFloat(xp) * enemyCR);
            } else {
                experience += Integer.parseInt(xp);
            }
        } catch (Exception e) {
            Logging.errorPrint("Experience Value: '" + xp + "' Not a number");
            Logging.errorPrint(e.getMessage(), e);
        }
    }
    return new Double(experience * multiplier).intValue();
}
Also used : File(java.io.File) ReadXML(gmgen.io.ReadXML)

Aggregations

ReadXML (gmgen.io.ReadXML)6 File (java.io.File)6 VectorTable (gmgen.io.VectorTable)3 Random (java.util.Random)2 Race (pcgen.core.Race)2 ArrayList (java.util.ArrayList)1 NoSuchElementException (java.util.NoSuchElementException)1 Vector (java.util.Vector)1 Formula (pcgen.base.formula.Formula)1 ChallengeRating (pcgen.cdom.content.ChallengeRating)1