Search in sources :

Example 11 with CDOMSingleRef

use of pcgen.cdom.reference.CDOMSingleRef in project pcgen by PCGen.

the class KitDeity method testApply.

@Override
public boolean testApply(Kit aKit, PlayerCharacter aPC, List<String> warnings) {
    domainsToAdd = null;
    theDeity = theDeityRef.get();
    if (!aPC.canSelectDeity(theDeity)) {
        warnings.add("DEITY: Cannot select deity \"" + theDeity.getDisplayName() + "\"");
        return false;
    }
    aPC.setDeity(theDeity);
    if (theDomains == null || theDomains.isEmpty()) {
        // nothing else to do.
        return true;
    }
    if (aPC.getMaxCharacterDomains() <= 0) {
        warnings.add("DEITY: Not allowed to choose a domain");
        return true;
    }
    int numberOfChoices;
    if (choiceCount == null) {
        numberOfChoices = theDomains.size();
    } else {
        numberOfChoices = choiceCount.resolve(aPC, "").intValue();
    }
    //
    if (numberOfChoices > theDomains.size()) {
        numberOfChoices = theDomains.size();
    }
    if (numberOfChoices == 0) {
        // No choices allowed, we are done.
        return true;
    }
    List<CDOMSingleRef<Domain>> xs;
    if (numberOfChoices == theDomains.size()) {
        xs = theDomains;
    } else {
        //
        while (true) {
            xs = Globals.getChoiceFromList("Choose Domains", theDomains, new ArrayList<>(), numberOfChoices, aPC);
            if (!xs.isEmpty()) {
                break;
            }
        }
    }
    //
    for (CDOMSingleRef<Domain> ref : xs) {
        Domain domain = ref.get();
        if (!domain.qualifies(aPC, domain)) {
            warnings.add("DEITY: Not qualified for domain \"" + domain.getDisplayName() + "\"");
            continue;
        }
        if (aPC.getDomainCount() >= aPC.getMaxCharacterDomains()) {
            warnings.add("DEITY: No more allowed domains");
            return false;
        }
        if (!aPC.hasDefaultDomainSource()) {
            warnings.add("DEITY: Cannot add domain \"" + domain.getDisplayName() + "\" as the character does not have a domain " + "source yet.");
            return false;
        }
        if (domainsToAdd == null) {
            domainsToAdd = new ArrayList<>();
        }
        domainsToAdd.add(domain);
        aPC.addDomain(domain);
        DomainApplication.applyDomain(aPC, domain);
    }
    aPC.calcActiveBonuses();
    return true;
}
Also used : ArrayList(java.util.ArrayList) Domain(pcgen.core.Domain) CDOMSingleRef(pcgen.cdom.reference.CDOMSingleRef)

Example 12 with CDOMSingleRef

use of pcgen.cdom.reference.CDOMSingleRef in project pcgen by PCGen.

the class DomainToken method parseTokenWithSeparator.

@Override
protected ParseResult parseTokenWithSeparator(LoadContext context, PCClass pcc, String value) {
    StringTokenizer pipeTok = new StringTokenizer(value, Constants.PIPE);
    boolean first = true;
    String tok = pipeTok.nextToken();
    if (looksLikeAPrerequisite(tok)) {
        return new ParseResult.Fail("Cannot have only PRExxx subtoken in " + getTokenName() + ": " + value, context);
    }
    List<QualifiedObject<CDOMSingleRef<Domain>>> toAdd = new ArrayList<>();
    boolean foundClear = false;
    while (true) {
        if (Constants.LST_DOT_CLEAR.equals(tok)) {
            if (!first) {
                return new ParseResult.Fail("  Non-sensical " + getTokenName() + ": .CLEAR was not the first list item", context);
            }
            context.getObjectContext().removeList(pcc, ListKey.DOMAIN);
            foundClear = true;
        } else {
            CDOMSingleRef<Domain> domain = context.getReferenceContext().getCDOMReference(DOMAIN_CLASS, tok);
            QualifiedObject<CDOMSingleRef<Domain>> qo = new QualifiedObject<>(domain);
            toAdd.add(qo);
            context.getObjectContext().addToList(pcc, ListKey.DOMAIN, qo);
        }
        first = false;
        if (!pipeTok.hasMoreTokens()) {
            // No prereqs, so we're done
            return ParseResult.SUCCESS;
        }
        tok = pipeTok.nextToken();
        if (looksLikeAPrerequisite(tok)) {
            break;
        }
    }
    if (foundClear) {
        return new ParseResult.Fail("Cannot use PREREQs when using .CLEAR in " + getTokenName(), context);
    }
    while (true) {
        Prerequisite prereq = getPrerequisite(tok);
        if (prereq == null) {
            return new ParseResult.Fail("   (Did you put feats after the " + "PRExxx tags in " + getTokenName() + ":?)", context);
        }
        for (PrereqObject pro : toAdd) {
            pro.addPrerequisite(prereq);
        }
        if (!pipeTok.hasMoreTokens()) {
            break;
        }
        tok = pipeTok.nextToken();
    }
    return ParseResult.SUCCESS;
}
Also used : ArrayList(java.util.ArrayList) CDOMSingleRef(pcgen.cdom.reference.CDOMSingleRef) PrereqObject(pcgen.cdom.base.PrereqObject) StringTokenizer(java.util.StringTokenizer) QualifiedObject(pcgen.core.QualifiedObject) Domain(pcgen.core.Domain) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 13 with CDOMSingleRef

use of pcgen.cdom.reference.CDOMSingleRef in project pcgen by PCGen.

the class DomainTokenTest method testUnparseSinglePre.

@Test
public void testUnparseSinglePre() throws PersistenceLayerException {
    Domain wp1 = construct(primaryContext, "TestWP1");
    CDOMDirectSingleRef<Domain> ref = CDOMDirectSingleRef.getRef(wp1);
    PreParserFactory prereqParser = PreParserFactory.getInstance();
    Prerequisite prereq = prereqParser.parse("PRERACE:1,Dwarf");
    assertNotNull(prereq);
    QualifiedObject<CDOMSingleRef<Domain>> qo = new QualifiedObject<>(ref, prereq);
    primaryProf.addToListFor(ListKey.DOMAIN, qo);
    String[] unparsed = getToken().unparse(primaryContext, primaryProf);
    expectSingle(unparsed, getLegalValue() + "|PRERACE:1,Dwarf");
}
Also used : PreParserFactory(pcgen.persistence.lst.prereq.PreParserFactory) QualifiedObject(pcgen.core.QualifiedObject) Domain(pcgen.core.Domain) CDOMSingleRef(pcgen.cdom.reference.CDOMSingleRef) Prerequisite(pcgen.core.prereq.Prerequisite) Test(org.junit.Test)

Example 14 with CDOMSingleRef

use of pcgen.cdom.reference.CDOMSingleRef in project pcgen by PCGen.

the class QualifyToken method unparse.

@Override
public String[] unparse(LoadContext context, CDOMObject obj) {
    Changes<Qualifier> changes = context.getObjectContext().getListChanges(obj, ListKey.QUALIFY);
    if (changes == null || changes.isEmpty()) {
        return null;
    }
    Collection<Qualifier> quals = changes.getAdded();
    HashMapToList<String, CDOMSingleRef<?>> map = new HashMapToList<>();
    for (Qualifier qual : quals) {
        Class<? extends Loadable> cl = qual.getQualifiedClass();
        String s = StringPClassUtil.getStringFor(cl);
        CDOMSingleRef<?> ref = qual.getQualifiedReference();
        String key = s;
        if (ref instanceof CategorizedCDOMReference) {
            Category<?> cat = ((CategorizedCDOMReference<?>) ref).getCDOMCategory();
            key += '=' + cat.getKeyName();
        }
        map.addToListFor(key, ref);
    }
    Set<CDOMSingleRef<?>> set = new TreeSet<>(ReferenceUtilities.REFERENCE_SORTER);
    Set<String> returnSet = new TreeSet<>();
    for (String key : map.getKeySet()) {
        set.clear();
        set.addAll(map.getListFor(key));
        StringBuilder sb = new StringBuilder();
        sb.append(key).append(Constants.PIPE).append(ReferenceUtilities.joinLstFormat(set, Constants.PIPE));
        returnSet.add(sb.toString());
    }
    return returnSet.toArray(new String[returnSet.size()]);
}
Also used : CDOMSingleRef(pcgen.cdom.reference.CDOMSingleRef) CategorizedCDOMReference(pcgen.cdom.reference.CategorizedCDOMReference) HashMapToList(pcgen.base.util.HashMapToList) TreeSet(java.util.TreeSet) Qualifier(pcgen.cdom.reference.Qualifier)

Example 15 with CDOMSingleRef

use of pcgen.cdom.reference.CDOMSingleRef in project pcgen by PCGen.

the class ForwardRefToken method unparse.

@Override
public String[] unparse(LoadContext context, Campaign obj) {
    Changes<Qualifier> changes = context.getObjectContext().getListChanges(obj, ListKey.FORWARDREF);
    if (changes == null || changes.isEmpty()) {
        return null;
    }
    Collection<Qualifier> quals = changes.getAdded();
    HashMapToList<String, CDOMSingleRef<?>> map = new HashMapToList<>();
    for (Qualifier qual : quals) {
        Class<? extends Loadable> cl = qual.getQualifiedClass();
        String s = StringPClassUtil.getStringFor(cl);
        CDOMSingleRef<?> ref = qual.getQualifiedReference();
        String key = s;
        if (ref instanceof CategorizedCDOMReference) {
            String cat = ((CategorizedCDOMReference<?>) ref).getLSTCategory();
            if (Constants.FEAT_CATEGORY.equals(cat)) {
                key = Constants.FEAT_CATEGORY;
            } else {
                key += '=' + cat;
            }
        }
        map.addToListFor(key, ref);
    }
    Set<CDOMSingleRef<?>> set = new TreeSet<>(ReferenceUtilities.REFERENCE_SORTER);
    Set<String> returnSet = new TreeSet<>();
    for (String key : map.getKeySet()) {
        set.clear();
        set.addAll(map.getListFor(key));
        StringBuilder sb = new StringBuilder();
        sb.append(key).append(Constants.PIPE).append(ReferenceUtilities.joinLstFormat(set, Constants.COMMA));
        returnSet.add(sb.toString());
    }
    return returnSet.toArray(new String[returnSet.size()]);
}
Also used : CDOMSingleRef(pcgen.cdom.reference.CDOMSingleRef) CategorizedCDOMReference(pcgen.cdom.reference.CategorizedCDOMReference) HashMapToList(pcgen.base.util.HashMapToList) TreeSet(java.util.TreeSet) Qualifier(pcgen.cdom.reference.Qualifier)

Aggregations

CDOMSingleRef (pcgen.cdom.reference.CDOMSingleRef)38 ArrayList (java.util.ArrayList)13 Ability (pcgen.core.Ability)10 Domain (pcgen.core.Domain)10 Prerequisite (pcgen.core.prereq.Prerequisite)10 StringTokenizer (java.util.StringTokenizer)9 QualifiedObject (pcgen.core.QualifiedObject)9 PCStat (pcgen.core.PCStat)6 List (java.util.List)4 Map (java.util.Map)4 TreeSet (java.util.TreeSet)4 CDOMObject (pcgen.cdom.base.CDOMObject)4 CDOMReference (pcgen.cdom.base.CDOMReference)4 KnownSpellIdentifier (pcgen.cdom.content.KnownSpellIdentifier)4 CharID (pcgen.cdom.enumeration.CharID)4 AbilityCategory (pcgen.core.AbilityCategory)4 PCClass (pcgen.core.PCClass)4 ParseResult (pcgen.rules.persistence.token.ParseResult)4 HashMap (java.util.HashMap)3 Formula (pcgen.base.formula.Formula)3