Search in sources :

Example 21 with CNAbilitySelection

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

the class AbstractCNASEnforcingFacet method remove.

public boolean remove(CharID id, CNAbilitySelection cnas, Object source) {
    if (cnas == null) {
        throw new IllegalArgumentException("Attempt to remove null from list");
    }
    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 false;
    }
    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 (cnas.equals(sc.cnas) && source.equals(sc.source)) {
                //fix the array here
                if ((j == 0) && (length == 1)) {
                    //There is no alternative, remove the array from the list;
                    listIT.remove();
                    fireDataFacetChangeEvent(id, cnas, DataFacetChangeEvent.DATA_REMOVED);
                    return true;
                } else {
                    array.remove(j);
                    CNAbilitySelection newPrimary = array.get(0).cnas;
                    //Only fire if the CNAS differs to avoid churn
                    if (!cnas.equals(newPrimary) && (j == 0)) {
                        fireDataFacetChangeEvent(id, cnas, DataFacetChangeEvent.DATA_REMOVED);
                        fireDataFacetChangeEvent(id, newPrimary, DataFacetChangeEvent.DATA_ADDED);
                        return true;
                    }
                    return false;
                }
            }
        }
    }
    return false;
}
Also used : CNAbilitySelection(pcgen.cdom.helper.CNAbilitySelection) List(java.util.List) ArrayList(java.util.ArrayList)

Example 22 with CNAbilitySelection

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

the class AbstractCharacterTestCase method applyAbility.

public static CNAbility applyAbility(PlayerCharacter character, AbilityCategory cat, Ability a, String assoc) {
    if (a.getCDOMCategory() == null) {
        fail("Attempt to apply an Ability " + a.getKeyName() + " that never received a Category");
    }
    CNAbility cna = CNAbilityFactory.getCNAbility(cat, Nature.NORMAL, a);
    CNAbilitySelection cnas = new CNAbilitySelection(cna, assoc);
    character.addAbility(cnas, UserSelection.getInstance(), UserSelection.getInstance());
    return cna;
}
Also used : CNAbility(pcgen.cdom.content.CNAbility) CNAbilitySelection(pcgen.cdom.helper.CNAbilitySelection)

Example 23 with CNAbilitySelection

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

the class AbilityTokenTest method testDecodeChoice.

@Test
public void testDecodeChoice() {
    try {
        pca.decodeChoice(context, "CATEGORY=FEAT|NATURE=NORMAL|ItemName");
        fail();
    } catch (IllegalArgumentException e) {
    // OK
    }
    Ability item = construct("ItemName");
    CNAbilitySelection as = new CNAbilitySelection(CNAbilityFactory.getCNAbility(AbilityCategory.FEAT, Nature.NORMAL, item));
    assertEquals(as, pca.decodeChoice(context, "CATEGORY=FEAT|NATURE=NORMAL|ItemName"));
}
Also used : Ability(pcgen.core.Ability) CNAbilitySelection(pcgen.cdom.helper.CNAbilitySelection) Test(org.junit.Test)

Example 24 with CNAbilitySelection

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

the class AbilityTokenTest method testWithChoose.

@Test
public void testWithChoose() {
    try {
        setUpPC();
        //Need to make sure we use the character related context
        context = Globals.getContext();
        context.getReferenceContext().importObject(AbilityCategory.FEAT);
        TokenRegistration.register(ADD_TOKEN);
        TokenRegistration.register(ADD_ABILITY_TOKEN);
    } catch (PersistenceLayerException e1) {
        fail("Cannot set up PC");
    }
    Ability item = construct("ChooseAbility");
    Ability parent = construct("Parent");
    context.getReferenceContext().constructCDOMObject(Language.class, "Foo");
    context.getReferenceContext().constructCDOMObject(Language.class, "Bar");
    context.getReferenceContext().constructCDOMObject(Language.class, "Goo");
    context.getReferenceContext().constructCDOMObject(Language.class, "Wow");
    context.getReferenceContext().constructCDOMObject(Language.class, "Rev");
    AbilityCategory ff = context.getReferenceContext().constructCDOMObject(AbilityCategory.class, "Fighter Feat");
    ff.setAbilityCategory(CDOMDirectSingleRef.getRef(AbilityCategory.FEAT));
    AbilityCategory oc = context.getReferenceContext().constructCDOMObject(AbilityCategory.class, "Some Other Category");
    Ability badCA = context.getReferenceContext().constructCDOMObject(Ability.class, "ChooseAbility");
    context.getReferenceContext().reassociateCategory(oc, badCA);
    try {
        assertTrue(context.processToken(item, "CHOOSE", "LANG|Foo|Bar|Goo|Wow|Rev"));
        assertTrue(context.processToken(item, "MULT", "Yes"));
        assertTrue(context.processToken(badCA, "CHOOSE", "LANG|Foo|Bar|Goo|Wow|Rev"));
        assertTrue(context.processToken(badCA, "MULT", "Yes"));
        assertTrue(context.processToken(parent, "ADD", "ABILITY|FEAT|NORMAL|ChooseAbility"));
    } catch (PersistenceLayerException e) {
        e.printStackTrace();
        fail();
    }
    finishLoad(context);
    PlayerCharacter pc = new PlayerCharacter();
    Object source = UserSelection.getInstance();
    CNAbilitySelection badCACAS = new CNAbilitySelection(CNAbilityFactory.getCNAbility(oc, Nature.AUTOMATIC, badCA), "Foo");
    CNAbilitySelection fooCAS = new CNAbilitySelection(CNAbilityFactory.getCNAbility(AbilityCategory.FEAT, Nature.AUTOMATIC, item), "Foo");
    CNAbilitySelection barCAS = new CNAbilitySelection(CNAbilityFactory.getCNAbility(AbilityCategory.FEAT, Nature.VIRTUAL, item), "Bar");
    CNAbilitySelection gooCAS = new CNAbilitySelection(CNAbilityFactory.getCNAbility(AbilityCategory.FEAT, Nature.NORMAL, item), "Goo");
    CNAbilitySelection wowCAS = new CNAbilitySelection(CNAbilityFactory.getCNAbility(AbilityCategory.FEAT, Nature.NORMAL, item), "Wow");
    CNAbilitySelection wowFFCAS = new CNAbilitySelection(CNAbilityFactory.getCNAbility(ff, Nature.NORMAL, item), "Wow");
    CNAbilitySelection revCAS = new CNAbilitySelection(CNAbilityFactory.getCNAbility(AbilityCategory.FEAT, Nature.NORMAL, item), "Rev");
    CNAbilitySelection revFFCAS = new CNAbilitySelection(CNAbilityFactory.getCNAbility(ff, Nature.NORMAL, item), "Rev");
    assertTrue(pca.allow(fooCAS, pc, false));
    assertTrue(pca.allow(barCAS, pc, false));
    assertTrue(pca.allow(gooCAS, pc, false));
    assertTrue(pca.allow(wowCAS, pc, false));
    assertTrue(pca.allow(revFFCAS, pc, false));
    pc.applyAbility(badCACAS, source);
    //Should have had no effect
    assertTrue(pca.allow(fooCAS, pc, false));
    assertTrue(pca.allow(barCAS, pc, false));
    assertTrue(pca.allow(gooCAS, pc, false));
    assertTrue(pca.allow(wowCAS, pc, false));
    assertTrue(pca.allow(revFFCAS, pc, false));
    pc.applyAbility(fooCAS, source);
    assertFalse(pca.allow(fooCAS, pc, false));
    assertTrue(pca.allow(barCAS, pc, false));
    assertTrue(pca.allow(gooCAS, pc, false));
    assertTrue(pca.allow(wowCAS, pc, false));
    assertTrue(pca.allow(revFFCAS, pc, false));
    pc.applyAbility(barCAS, source);
    assertFalse(pca.allow(fooCAS, pc, false));
    assertFalse(pca.allow(barCAS, pc, false));
    assertTrue(pca.allow(gooCAS, pc, false));
    assertTrue(pca.allow(wowCAS, pc, false));
    assertTrue(pca.allow(revFFCAS, pc, false));
    pc.applyAbility(gooCAS, source);
    assertFalse(pca.allow(fooCAS, pc, false));
    assertFalse(pca.allow(barCAS, pc, false));
    assertFalse(pca.allow(gooCAS, pc, false));
    assertTrue(pca.allow(wowCAS, pc, false));
    assertTrue(pca.allow(revFFCAS, pc, false));
    pc.applyAbility(wowFFCAS, source);
    assertFalse(pca.allow(fooCAS, pc, false));
    assertFalse(pca.allow(barCAS, pc, false));
    assertFalse(pca.allow(gooCAS, pc, false));
    assertFalse(pca.allow(wowCAS, pc, false));
    assertTrue(pca.allow(revFFCAS, pc, false));
    pc.applyAbility(revCAS, source);
    assertFalse(pca.allow(fooCAS, pc, false));
    assertFalse(pca.allow(barCAS, pc, false));
    assertFalse(pca.allow(gooCAS, pc, false));
    assertFalse(pca.allow(wowCAS, pc, false));
    assertFalse(pca.allow(revFFCAS, pc, false));
}
Also used : PersistenceLayerException(pcgen.persistence.PersistenceLayerException) Ability(pcgen.core.Ability) PlayerCharacter(pcgen.core.PlayerCharacter) CNAbilitySelection(pcgen.cdom.helper.CNAbilitySelection) AbilityCategory(pcgen.core.AbilityCategory) Test(org.junit.Test)

Example 25 with CNAbilitySelection

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

the class AbstractCNASEnforcingFacetTest method testTypeAddMultRemove.

@Test
public void testTypeAddMultRemove() {
    Object source1 = new Object();
    CNAbilitySelection t1 = getMultObject("English");
    CNAbilitySelection t2 = getMultObject("German");
    getFacet().add(id, t1, source1);
    getFacet().add(id, t2, source1);
    getFacet().remove(id, t1, source1);
    assertEquals(1, getFacet().getCount(id));
    assertFalse(getFacet().isEmpty(id));
    Collection<CNAbilitySelection> setofone = getFacet().getSet(id);
    assertNotNull(setofone);
    assertEquals(1, setofone.size());
    assertTrue(setofone.contains(t2));
    assertEventCount(2, 1);
}
Also used : CNAbilitySelection(pcgen.cdom.helper.CNAbilitySelection) Test(org.junit.Test)

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