Search in sources :

Example 6 with WeightedCollection

use of pcgen.base.util.WeightedCollection in project pcgen by PCGen.

the class NPCGenerator method generate.

/**
	 * Generate a new NPC
	 * 
	 * @param aPC The PlayerCharacter to fill in options for
	 * @param align Alignment options to choose from
	 * @param aRace Race options to choose from
	 * @param aGender Gender options to choose from
	 * @param classList <tt>List</tt> of class options to choose from
	 * @param levels <tt>List</tt> of level choices
	 * @param aRollMethod the RollMethod to use for stats
	 */
public void generate(final PlayerCharacter aPC, final AlignGeneratorOption align, final RaceGeneratorOption aRace, final GenderGeneratorOption aGender, final List<ClassGeneratorOption> classList, final List<LevelGeneratorOption> levels, final RollMethod aRollMethod) {
    // Force a more quiet process
    ChooserFactory.pushChooserClassname(//$NON-NLS-1$
    "pcgen.util.chooser.RandomChooser");
    boolean tempShowHP = SettingsHandler.getShowHPDialogAtLevelUp();
    SettingsHandler.setShowHPDialogAtLevelUp(false);
    int tempChoicePref = UIPropertyContext.getSingleChoiceAction();
    UIPropertyContext.setSingleChoiceAction(Constants.CHOOSER_SINGLE_CHOICE_METHOD_SELECT_EXIT);
    try {
        final int MAX_RETRIES = 5;
        for (int i = 0; i < MAX_RETRIES; i++) {
            PCAlignment randAlign = getAlignment(align);
            if (randAlign != null) {
                Logging.debugPrint(//$NON-NLS-1$//$NON-NLS-2$
                "NPCGenerator: Selected " + randAlign + " for alignment " + align);
                aPC.setAlignment(randAlign);
            }
            final Race r = getRace(aRace);
            if (r == null) {
                //$NON-NLS-1$
                Logging.debugPrint("NPCGenerator: Got null race.  Retrying.");
                continue;
            }
            //$NON-NLS-1$ //$NON-NLS-2$
            Logging.debugPrint("NPCGenerator: Selected " + r + " for race " + aRace);
            if (r.qualifies(aPC, r)) {
                //$NON-NLS-1$
                Logging.debugPrint("NPCGenerator: PC qualifies for race " + r);
                aPC.setRace(r);
                break;
            }
        }
        if (aPC.getRace() == Globals.s_EMPTYRACE) {
            Logging.errorPrint("Unable to select race");
            return;
        }
        final Gender gender = getGender(aGender);
        //$NON-NLS-1$ //$NON-NLS-2$
        Logging.debugPrint("NPCGenerator: Selecting " + gender + " for gender " + aGender);
        aPC.setGender(gender);
        boolean doneRacialClasses = false;
        for (int i = 0; i < classList.size(); i++) {
            int numLevels = getLevel(levels.get(i));
            //$NON-NLS-1$ //$NON-NLS-2$
            Logging.debugPrint("NPCGenerator: Selecting " + numLevels + " for level " + levels.get(i));
            PCClass aClass = null;
            if (!doneRacialClasses && aPC.hasClass()) {
                aClass = aPC.getClassList().get(0);
                numLevels = aPC.getLevel(aClass);
                doneRacialClasses = true;
                i--;
            } else {
                doneRacialClasses = true;
                for (; ; ) {
                    aClass = getClass(classList.get(i));
                    if (aClass == null) {
                        break;
                    }
                    if (aClass.getSafe(ObjectKey.VISIBILITY).equals(Visibility.DEFAULT) && aClass.qualifies(aPC, aClass)) {
                        //$NON-NLS-1$ //$NON-NLS-2$
                        Logging.debugPrint("NPCGenerator: Selecting " + aClass + " for class " + classList.get(i));
                        break;
                    }
                    // TODO Remove a failed class from the list.
                    Logging.errorPrint("Counld not add a level of " + aClass);
                    aClass = null;
                    break;
                }
            }
            if (aClass == null) {
                continue;
            }
            final PCClass classCopy = aClass.clone();
            if (classCopy.containsListFor(ListKey.SUB_CLASS)) {
                selectSubClass(aPC, classCopy);
            }
            if (i == 0) {
                generateStats(aPC, classCopy, aRollMethod);
                selectDeity(aPC, classCopy);
            }
            int highestSpellLevel = aPC.getSpellSupport(aClass).getHighestLevelSpell(aPC);
            final int[] selectedSpells = new int[highestSpellLevel + 1];
            for (int k = 0; k < highestSpellLevel; k++) {
                selectedSpells[k] = 0;
            }
            final int[] bonusSpells = new int[highestSpellLevel + 1];
            for (int k = 0; k < highestSpellLevel; k++) {
                bonusSpells[k] = 0;
            }
            // Make a copy of the list because we are going to modify it.
            WeightedCollection<SkillChoice> skillList = new WeightedCollection<>(getSkillWeights(classCopy, aPC));
            WeightedCollection<Ability> featList = new WeightedCollection<>(getFeatWeights(classCopy));
            for (int j = 0; j < numLevels; j++) {
                if (i >= 0) {
                    aPC.incrementClassLevel(1, classCopy, true);
                }
                final PCClass pcClass = aPC.getClassKeyed(classCopy.getKeyName());
                selectSkills(aPC, skillList, pcClass, j + 1);
                selectFeats(aPC, featList);
                selectDomains(aPC, pcClass);
                if (pcClass.get(FactKey.valueOf("SpellType")) != null) {
                    // spells of some sort (known or prepared).
                    if (aPC.getSpellSupport(pcClass).hasKnownList() || aPC.getSpellSupport(pcClass).hasKnownSpells(aPC)) {
                        //$NON-NLS-1$
                        Logging.debugPrint("NPCGenerator: known spells to select");
                        for (int lvl = 0; lvl <= highestSpellLevel; ++lvl) {
                            if (aPC.availableSpells(lvl, pcClass, Globals.getDefaultSpellBook(), true, true)) {
                                final int a = aPC.getSpellSupport(pcClass).getKnownForLevel(lvl, aPC);
                                //final int bonus = aPC.getSpellSupport(pcClass).getSpecialtyKnownForLevel(lvl, aPC);
                                //$NON-NLS-1$ //$NON-NLS-2$
                                Logging.debugPrint("NPCGenerator: " + a + "known spells to select");
                                final WeightedCollection<Spell> spellChoices = getKnownSpellWeights(aPC, pcClass, lvl);
                                final int numToSelect = a - selectedSpells[lvl];
                                for (int sp = 0; sp < numToSelect; sp++) {
                                    selectSpell(aPC, pcClass, null, Globals.getDefaultSpellBook(), spellChoices, lvl);
                                    selectedSpells[lvl]++;
                                }
                            }
                        }
                    } else {
                        // Prepared spells?
                        //$NON-NLS-1$
                        Logging.debugPrint("NPCGenerator: prepared spells to select");
                        aPC.addSpellBook("Prepared Spells");
                        for (int lvl = 0; lvl <= highestSpellLevel; ++lvl) {
                            final int castTot = aPC.getSpellSupport(pcClass).getCastForLevel(lvl, "Prepared Spells", true, true, aPC);
                            final int castNon = aPC.getSpellSupport(pcClass).getCastForLevel(lvl, "Prepared Spells", false, true, aPC);
                            final int castSpec = castTot - castNon;
                            //$NON-NLS-1$ //$NON-NLS-2$
                            Logging.debugPrint("NPCGenerator: " + castTot + "+" + castSpec + " prepared spells to select");
                            if (castSpec - bonusSpells[lvl] > 0) {
                                selectDomainSpell(aPC, pcClass, lvl);
                                bonusSpells[lvl]++;
                            }
                            if (castTot > 0) {
                                final WeightedCollection<Spell> spellChoices = getPreparedSpellWeights(aPC, pcClass, lvl);
                                final int numToSelect = castNon - selectedSpells[lvl];
                                for (int sp = 0; sp < numToSelect; sp++) {
                                    selectSpell(aPC, pcClass, null, "Prepared Spells", spellChoices, lvl);
                                    selectedSpells[lvl]++;
                                }
                            }
                        }
                    }
                }
            }
        }
        //$NON-NLS-1$
        final String randBioString = "EYES.HAIR.SKIN.HT.WT.AGE.";
        aPC.getBioSet().randomize(randBioString, aPC);
        final List<String> globalHairStyleList = SystemCollections.getUnmodifiableHairStyleList();
        aPC.setPCAttribute(PCAttribute.HAIRSTYLE, globalHairStyleList.get(RandomUtil.getRandomInt(globalHairStyleList.size())));
        final List<String> speechList = SystemCollections.getUnmodifiableSpeechList();
        aPC.setPCAttribute(PCAttribute.SPEECHTENDENCY, speechList.get(RandomUtil.getRandomInt(speechList.size())));
        final List<String> globalPhobiaList = SystemCollections.getUnmodifiablePhobiaList();
        aPC.setPCAttribute(PCAttribute.PHOBIAS, globalPhobiaList.get(RandomUtil.getRandomInt(globalPhobiaList.size())));
        final List<String> globalInterestsList = SystemCollections.getUnmodifiableInterestsList();
        aPC.setPCAttribute(PCAttribute.INTERESTS, globalInterestsList.get(RandomUtil.getRandomInt(globalInterestsList.size())));
        final List<String> globalPhraseList = SystemCollections.getUnmodifiablePhraseList();
        aPC.setPCAttribute(PCAttribute.CATCHPHRASE, globalPhraseList.get(RandomUtil.getRandomInt(globalPhraseList.size())));
        final List<String> globalTraitList = SystemCollections.getUnmodifiableTraitList();
        // TODO: it is possible for trait1 == trait2
        aPC.setPCAttribute(PCAttribute.PERSONALITY1, globalTraitList.get(RandomUtil.getRandomInt(globalTraitList.size())));
        aPC.setPCAttribute(PCAttribute.PERSONALITY2, globalTraitList.get(RandomUtil.getRandomInt(globalTraitList.size())));
        final List<String> globalCityList = SystemCollections.getUnmodifiableCityList();
        aPC.setPCAttribute(PCAttribute.RESIDENCE, globalCityList.get(RandomUtil.getRandomInt(globalCityList.size())));
        final List<String> globalLocationList = SystemCollections.getUnmodifiableLocationList();
        aPC.setPCAttribute(PCAttribute.LOCATION, globalLocationList.get(RandomUtil.getRandomInt(globalLocationList.size())));
        final List<String> globalBirthplaceList = SystemCollections.getUnmodifiableBirthplaceList();
        aPC.setPCAttribute(PCAttribute.BIRTHPLACE, globalBirthplaceList.get(RandomUtil.getRandomInt(globalBirthplaceList.size())));
    //TODO: Link in with the doomsday book name generator
    //			final Names nameGen = Names.getInstance();
    //			nameGen.init(aNameChoice, aPC);
    //			aPC.setName(nameGen.getRandomName());
    } catch (Exception e) {
        Logging.errorPrint("Problem generation NPC", e);
    } finally {
        SettingsHandler.setShowHPDialogAtLevelUp(tempShowHP);
        UIPropertyContext.setSingleChoiceAction(tempChoicePref);
        ChooserFactory.popChooserClassname();
    }
}
Also used : Ability(pcgen.core.Ability) Gender(pcgen.cdom.enumeration.Gender) PCClass(pcgen.core.PCClass) Spell(pcgen.core.spell.Spell) CharacterSpell(pcgen.core.character.CharacterSpell) PCAlignment(pcgen.core.PCAlignment) WeightedCollection(pcgen.base.util.WeightedCollection) Race(pcgen.core.Race)

Example 7 with WeightedCollection

use of pcgen.base.util.WeightedCollection in project pcgen by PCGen.

the class EquipToken method unparse.

@Override
public String[] unparse(LoadContext context, CDOMObject obj) {
    List<String> list = new ArrayList<>();
    PrerequisiteWriter prereqWriter = new PrerequisiteWriter();
    Changes<ChooseSelectionActor<?>> listChanges = context.getObjectContext().getListChanges(obj, ListKey.NEW_CHOOSE_ACTOR);
    Changes<QualifiedObject<CDOMReference<Equipment>>> changes = context.getObjectContext().getListChanges(obj, ListKey.EQUIPMENT);
    Collection<QualifiedObject<CDOMReference<Equipment>>> added = changes.getAdded();
    HashMapToList<List<Prerequisite>, CDOMReference<Equipment>> m = new HashMapToList<>();
    if (added != null) {
        for (QualifiedObject<CDOMReference<Equipment>> qo : added) {
            m.addToListFor(qo.getPrerequisiteList(), qo.getRawObject());
        }
    }
    Collection<ChooseSelectionActor<?>> listAdded = listChanges.getAdded();
    if (listAdded != null && !listAdded.isEmpty()) {
        for (ChooseSelectionActor<?> cra : listAdded) {
            if (cra.getSource().equals(getTokenName())) {
                try {
                    list.add(cra.getLstFormat());
                } catch (PersistenceLayerException e) {
                    context.addWriteMessage("Error writing Prerequisite: " + e);
                    return null;
                }
            }
        }
    }
    for (List<Prerequisite> prereqs : m.getKeySet()) {
        List<CDOMReference<Equipment>> eq = m.getListFor(prereqs);
        WeightedCollection<CDOMReference<Equipment>> refs = new WeightedCollection<>(ReferenceUtilities.REFERENCE_SORTER);
        refs.addAll(eq);
        String ab = ReferenceUtilities.joinLstFormat(refs, Constants.PIPE);
        if (prereqs != null && !prereqs.isEmpty()) {
            if (prereqs.size() > 1) {
                context.addWriteMessage("Error: " + obj.getClass().getSimpleName() + " had more than one Prerequisite for " + getFullName());
                return null;
            }
            Prerequisite p = prereqs.get(0);
            StringWriter swriter = new StringWriter();
            try {
                prereqWriter.write(swriter, p);
            } catch (PersistenceLayerException e) {
                context.addWriteMessage("Error writing Prerequisite: " + e);
                return null;
            }
            ab = ab + '|' + swriter.toString();
        }
        list.add(ab);
    }
    if (list.isEmpty()) {
        // Empty indicates no Token
        return null;
    }
    return list.toArray(new String[list.size()]);
}
Also used : PrerequisiteWriter(pcgen.persistence.lst.output.prereq.PrerequisiteWriter) ArrayList(java.util.ArrayList) ChooseSelectionActor(pcgen.cdom.base.ChooseSelectionActor) PersistenceLayerException(pcgen.persistence.PersistenceLayerException) HashMapToList(pcgen.base.util.HashMapToList) Equipment(pcgen.core.Equipment) StringWriter(java.io.StringWriter) QualifiedObject(pcgen.core.QualifiedObject) WeightedCollection(pcgen.base.util.WeightedCollection) ArrayList(java.util.ArrayList) HashMapToList(pcgen.base.util.HashMapToList) List(java.util.List) CDOMReference(pcgen.cdom.base.CDOMReference) Prerequisite(pcgen.core.prereq.Prerequisite)

Aggregations

WeightedCollection (pcgen.base.util.WeightedCollection)7 ArrayList (java.util.ArrayList)2 CharacterSpell (pcgen.core.character.CharacterSpell)2 Spell (pcgen.core.spell.Spell)2 StringWriter (java.io.StringWriter)1 List (java.util.List)1 HashMapToList (pcgen.base.util.HashMapToList)1 CDOMReference (pcgen.cdom.base.CDOMReference)1 ChooseSelectionActor (pcgen.cdom.base.ChooseSelectionActor)1 Gender (pcgen.cdom.enumeration.Gender)1 Ability (pcgen.core.Ability)1 Deity (pcgen.core.Deity)1 Domain (pcgen.core.Domain)1 Equipment (pcgen.core.Equipment)1 Movement (pcgen.core.Movement)1 PCAlignment (pcgen.core.PCAlignment)1 PCClass (pcgen.core.PCClass)1 PCStat (pcgen.core.PCStat)1 QualifiedObject (pcgen.core.QualifiedObject)1 Race (pcgen.core.Race)1