Search in sources :

Example 1 with CNAbilitySelection

use of pcgen.cdom.helper.CNAbilitySelection in project pcgen by PCGen.

the class DirectAbilityFacet method removeAll.

public void removeAll(CharID id, Object source) {
    if (source == null) {
        throw new IllegalArgumentException("Attempt to remove object with null source from list");
    }
    List<List<SourcedCNAS>> list = getList(id);
    if (list == null) {
        return;
    }
    List<CNAbilitySelection> removed = new ArrayList<>();
    List<CNAbilitySelection> added = new ArrayList<>();
    for (Iterator<List<SourcedCNAS>> listIT = list.iterator(); listIT.hasNext(); ) {
        List<SourcedCNAS> array = listIT.next();
        int length = array.size();
        //Iterate backwards, so that we remove harmless items first
        for (int j = length - 1; j >= 0; j--) {
            SourcedCNAS sc = array.get(j);
            if (source.equals(sc.source)) {
                //fix the array here
                array.remove(j);
                boolean needRemove = true;
                if (!array.isEmpty()) {
                    CNAbilitySelection newPrimary = array.get(0).cnas;
                    //Only fire if the CNAS differs to avoid churn
                    if (!sc.cnas.equals(newPrimary) && (j == 0)) {
                        added.add(newPrimary);
                    } else {
                        needRemove = false;
                    }
                }
                if (needRemove) {
                    removed.add(sc.cnas);
                }
            }
        }
        if (array.isEmpty()) {
            listIT.remove();
        }
    }
    for (CNAbilitySelection cnas : removed) {
        fireDataFacetChangeEvent(id, cnas, DataFacetChangeEvent.DATA_REMOVED);
    }
    for (CNAbilitySelection cnas : added) {
        fireDataFacetChangeEvent(id, cnas, DataFacetChangeEvent.DATA_ADDED);
    }
}
Also used : ArrayList(java.util.ArrayList) CNAbilitySelection(pcgen.cdom.helper.CNAbilitySelection) List(java.util.List) ArrayList(java.util.ArrayList)

Example 2 with CNAbilitySelection

use of pcgen.cdom.helper.CNAbilitySelection in project pcgen by PCGen.

the class ConditionalAbilityFacet method getQualifiedSet.

/**
	 * Returns a non-null copy of the Set of objects the character qualifies for
	 * in this ConditionalAbilityFacet for the Player Character represented by
	 * the given CharID. This method returns an empty Collection if the Player
	 * Character identified by the given CharID qualifies for none of the
	 * objects in this ConditionalAbilityFacet.
	 * 
	 * This method is value-semantic in that ownership of the returned
	 * Collection is transferred to the class calling this method. Modification
	 * of the returned Collection will not modify this ConditionalAbilityFacet
	 * and modification of this ConditionalAbilityFacet will not modify the
	 * returned Collection. Modifications to the returned Collection will also
	 * not modify any future or previous objects returned by this (or other)
	 * methods on ConditionalAbilityFacet. If you wish to modify the information
	 * stored in this ConditionalAbilityFacet, you must use the add*() and
	 * remove*() methods of ConditionalAbilityFacet.
	 * 
	 * @param id
	 *            The CharID representing the Player Character for which the
	 *            items in this AbstractQualifiedListFacet should be returned.
	 * @return A non-null Set of objects the Player Character represented by the
	 *         given CharID qualifies for in this AbstractQualifiedListFacet
	 */
public Collection<CNAbilitySelection> getQualifiedSet(CharID id) {
    List<CNAbilitySelection> set = new ArrayList<>();
    Map<CNAbilitySelection, Object> cached = getCachedMap(id);
    if (cached != null) {
        for (Map.Entry<CNAbilitySelection, Object> me : cached.entrySet()) {
            CNAbilitySelection cnas = me.getKey();
            if (prerequisiteFacet.qualifies(id, cnas, me.getValue())) {
                set.add(cnas);
            }
        }
    }
    return set;
}
Also used : ArrayList(java.util.ArrayList) CNAbilitySelection(pcgen.cdom.helper.CNAbilitySelection) Map(java.util.Map)

Example 3 with CNAbilitySelection

use of pcgen.cdom.helper.CNAbilitySelection in project pcgen by PCGen.

the class AutoWeaponProfListTargetTest method testFromAbility.

@Test
public void testFromAbility() throws PersistenceLayerException {
    Ability source = create(Ability.class, "Source");
    context.getReferenceContext().reassociateCategory(AbilityCategory.FEAT, source);
    Ability granted = createGrantedObject();
    context.getReferenceContext().constructCDOMObject(Language.class, "English");
    ParseResult result = new MultToken().parseToken(context, granted, "YES");
    if (result != ParseResult.SUCCESS) {
        result.printMessages();
        fail("Test Setup Failed");
    }
    result = new MultToken().parseToken(context, source, "YES");
    if (result != ParseResult.SUCCESS) {
        result.printMessages();
        fail("Test Setup Failed");
    }
    result = new LangToken().parseToken(context, granted, "ALL");
    if (result != ParseResult.SUCCESS) {
        result.printMessages();
        fail("Test Setup Failed");
    }
    result = new LangToken().parseToken(context, source, "ALL");
    if (result != ParseResult.SUCCESS) {
        result.printMessages();
        fail("Test Setup Failed");
    }
    result = token.parseToken(context, source, "FEAT|Granted (%LIST)");
    if (result != ParseResult.SUCCESS) {
        result.printMessages();
        fail("Test Setup Failed");
    }
    finishLoad();
    assertEquals(0, directAbilityFacet.getCount(id));
    CNAbilitySelection cas = new CNAbilitySelection(CNAbilityFactory.getCNAbility(AbilityCategory.FEAT, Nature.AUTOMATIC, source), "English");
    directAbilityFacet.add(id, cas, UserSelection.getInstance());
    assertTrue(containsExpected());
    assertEquals(2, directAbilityFacet.getCount(id));
    directAbilityFacet.remove(id, cas, UserSelection.getInstance());
    assertEquals(0, directAbilityFacet.getCount(id));
}
Also used : Ability(pcgen.core.Ability) CNAbility(pcgen.cdom.content.CNAbility) ParseResult(pcgen.rules.persistence.token.ParseResult) CNAbilitySelection(pcgen.cdom.helper.CNAbilitySelection) MultToken(plugin.lsttokens.ability.MultToken) LangToken(plugin.lsttokens.choose.LangToken) Test(org.junit.Test) AbstractTokenModelTest(tokenmodel.testsupport.AbstractTokenModelTest)

Example 4 with CNAbilitySelection

use of pcgen.cdom.helper.CNAbilitySelection in project pcgen by PCGen.

the class AbstractAbilityGrantCheckTest method applyParent.

private void applyParent(Ability parent) {
    CNAbility cna = CNAbilityFactory.getCNAbility(AbilityCategory.FEAT, Nature.NORMAL, parent);
    CNAbilitySelection cnas = new CNAbilitySelection(cna);
    pc.addAbility(cnas, UserSelection.getInstance(), this);
}
Also used : CNAbility(pcgen.cdom.content.CNAbility) CNAbilitySelection(pcgen.cdom.helper.CNAbilitySelection)

Example 5 with CNAbilitySelection

use of pcgen.cdom.helper.CNAbilitySelection in project pcgen by PCGen.

the class PlayerCharacter method processAbilityList.

private void processAbilityList(CDOMObject cdo, CDOMReference<AbilityList> ref) {
    Collection<CDOMReference<Ability>> mods = cdo.getListMods(ref);
    for (CDOMReference<Ability> objref : mods) {
        Collection<Ability> objs = objref.getContainedObjects();
        Collection<AssociatedPrereqObject> assoc = cdo.getListAssociations(ref, objref);
        for (Ability ab : objs) {
            if (ab == null) {
                Logging.log(Logging.LST_ERROR, "Missing object referenced in the ability list for '" + cdo + "' list is " + ref + ". Source " + cdo.getSourceURI());
                continue;
            }
            for (AssociatedPrereqObject apo : assoc) {
                Nature nature = apo.getAssociation(AssociationKey.NATURE);
                CDOMSingleRef<AbilityCategory> acRef = apo.getAssociation(AssociationKey.CATEGORY);
                AbilityCategory cat = acRef.get();
                if (ab.getSafe(ObjectKey.MULTIPLE_ALLOWED)) {
                    List<String> choices = apo.getAssociation(AssociationKey.ASSOC_CHOICES);
                    if (choices == null) {
                        //CHOOSE:NOCHOICE can be unconditionally applied (must be STACK:YES)
                        CNAbilitySelection cas = new CNAbilitySelection(CNAbilityFactory.getCNAbility(cat, nature, ab), "");
                        cas.addAllPrerequisites(apo.getPrerequisiteList());
                        applyAbility(cas, cdo);
                    } else {
                        for (final String choice : choices) {
                            CNAbilitySelection cas = new CNAbilitySelection(CNAbilityFactory.getCNAbility(cat, nature, ab), choice);
                            cas.addAllPrerequisites(apo.getPrerequisiteList());
                            applyAbility(cas, cdo);
                        }
                    }
                } else {
                    CNAbilitySelection cas = new CNAbilitySelection(CNAbilityFactory.getCNAbility(cat, nature, ab));
                    cas.addAllPrerequisites(apo.getPrerequisiteList());
                    applyAbility(cas, cdo);
                }
            }
        }
    }
    cabFacet.update(id);
}
Also used : CNAbility(pcgen.cdom.content.CNAbility) Nature(pcgen.cdom.enumeration.Nature) CNAbilitySelection(pcgen.cdom.helper.CNAbilitySelection) CDOMReference(pcgen.cdom.base.CDOMReference) AssociatedPrereqObject(pcgen.cdom.base.AssociatedPrereqObject)

Aggregations

CNAbilitySelection (pcgen.cdom.helper.CNAbilitySelection)70 CNAbility (pcgen.cdom.content.CNAbility)35 Test (org.junit.Test)28 Ability (pcgen.core.Ability)26 ArrayList (java.util.ArrayList)17 AbilityCategory (pcgen.core.AbilityCategory)7 CDOMReference (pcgen.cdom.base.CDOMReference)6 PlayerCharacter (pcgen.core.PlayerCharacter)6 ParseResult (pcgen.rules.persistence.token.ParseResult)6 ConcretePersistentTransitionChoice (pcgen.cdom.base.ConcretePersistentTransitionChoice)5 AbilityRefChoiceSet (pcgen.cdom.choiceset.AbilityRefChoiceSet)5 Nature (pcgen.cdom.enumeration.Nature)5 PCTemplate (pcgen.core.PCTemplate)5 CharID (pcgen.cdom.enumeration.CharID)4 List (java.util.List)3 AbilityChoiceSet (pcgen.cdom.base.ChoiceSet.AbilityChoiceSet)3 SpecialAbility (pcgen.core.SpecialAbility)3 BonusObj (pcgen.core.bonus.BonusObj)3 AbstractTokenModelTest (tokenmodel.testsupport.AbstractTokenModelTest)3 HashSet (java.util.HashSet)2