Search in sources :

Example 1 with VectorTable

use of gmgen.io.VectorTable 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 VectorTable

use of gmgen.io.VectorTable 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 3 with VectorTable

use of gmgen.io.VectorTable 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)

Aggregations

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