Search in sources :

Example 31 with CNAbility

use of pcgen.cdom.content.CNAbility in project pcgen by PCGen.

the class AbilitySelectionApplication method applySelection.

private <T> void applySelection(PlayerCharacter pc, ChooseInformation<T> chooseInfo, CNAbility cna, String selection) {
    Ability ability = cna.getAbility();
    T obj = chooseInfo.decodeChoice(Globals.getContext(), selection);
    if (obj == null) {
        Logging.errorPrint("Unable to apply Selection: '" + selection + "' to Ability " + ability + " (" + ability.getCDOMCategory() + ") because the given selection does not exist in the loaded data");
    } else {
        chooseInfo.getChoiceActor().applyChoice(cna, obj, pc);
    }
}
Also used : Ability(pcgen.core.Ability) CNAbility(pcgen.cdom.content.CNAbility)

Example 32 with CNAbility

use of pcgen.cdom.content.CNAbility in project pcgen by PCGen.

the class ModifyChoiceDecorator method getSet.

/**
	 * Returns a Set containing the Objects which this ModifyChoiceDecorator
	 * contains and which are also possessed by the PlayerCharacter.
	 * 
	 * It is intended that classes which implement ModifyChoiceDecorator will
	 * make this method value-semantic, meaning that ownership of the Set
	 * returned by this method will be transferred to the calling object.
	 * Modification of the returned Set will not result in modifying the
	 * ModifyChoiceDecorator (and vice versa since the ModifyChoiceDecorator is
	 * near immutable)
	 * 
	 * @param pc
	 *            The PlayerCharacter for which the choices in this
	 *            ModifyChoiceDecorator should be returned.
	 * @return A Set containing the Objects which this ModifyChoiceDecorator
	 *         contains and which are also possessed by the PlayerCharacter.
	 */
@Override
public Set<CNAbility> getSet(PlayerCharacter pc) {
    Collection<? extends Ability> collection = pcs.getSet(pc);
    List<CNAbility> pcfeats = pc.getPoolAbilities(AbilityCategory.FEAT);
    Set<CNAbility> returnSet = new HashSet<>();
    for (CNAbility cna : pcfeats) {
        Ability a = cna.getAbility();
        if (a.getSafe(ObjectKey.MULTIPLE_ALLOWED).booleanValue() && collection.contains(a)) {
            returnSet.add(cna);
        }
    }
    return returnSet;
}
Also used : CNAbility(pcgen.cdom.content.CNAbility) Ability(pcgen.core.Ability) CNAbility(pcgen.cdom.content.CNAbility) HashSet(java.util.HashSet)

Example 33 with CNAbility

use of pcgen.cdom.content.CNAbility in project pcgen by PCGen.

the class KitAbilities method testApply.

@Override
public boolean testApply(Kit aKit, PlayerCharacter aPC, List<String> warnings) {
    abilitiesToAdd = new ArrayList<>();
    double minCost = Double.MAX_VALUE;
    List<AbilitySelection> available = new ArrayList<>();
    for (CDOMReference<Ability> ref : abilities) {
        String choice = ref.getChoice();
        for (Ability a : ref.getContainedObjects()) {
            if (a == null) {
                warnings.add("ABILITY: " + ref + " could not be found.");
                minCost = 0;
                continue;
            }
            if (a.getCost() < minCost) {
                minCost = a.getCost();
            }
            if ((choice == null) && a.getSafe(ObjectKey.MULTIPLE_ALLOWED)) {
                available.add(new AbilitySelection(a, ""));
            } else {
                available.add(new AbilitySelection(a, choice));
            }
        }
    }
    int numberOfChoices = getSafeCount();
    // TODO this fails if SELECT != 1
    if (numberOfChoices > available.size()) {
        numberOfChoices = available.size();
    }
    /*
		 * this section needs to be rewritten once we determine how
		 * the new Ability Pools are going to work
		 */
    AbilityCategory category = catRef.get();
    boolean tooManyAbilities = false;
    // Don't allow choosing of more than allotted number of abilities
    int maxChoices = minCost > 0.0d ? aPC.getAvailableAbilityPool(category).divide(new BigDecimal(minCost)).intValue() : numberOfChoices;
    if (!isFree() && numberOfChoices > maxChoices) {
        numberOfChoices = maxChoices;
        tooManyAbilities = true;
    }
    if (!isFree() && numberOfChoices == 0) {
        warnings.add("ABILITY: Not enough " + category.getPluralName() + " available to take \"" + this + "\"");
        return false;
    }
    List<AbilitySelection> selected;
    if (numberOfChoices == available.size()) {
        selected = available;
    } else {
        selected = new ArrayList<>();
        // Force user to make enough selections
        while (true) {
            selected = Globals.getChoiceFromList("Choose abilities", available, new ArrayList<>(), numberOfChoices, aPC);
            if (!selected.isEmpty()) {
                break;
            }
        }
    }
    // Add to list of things to add to the character
    for (AbilitySelection as : selected) {
        Ability ability = as.ability;
        if (isFree()) {
            // Need to pay for it first
            if (free) {
                aPC.adjustAbilities(category, BigDecimal.ONE);
            }
        }
        if (ability.getCost() > aPC.getAvailableAbilityPool(category).doubleValue()) {
            tooManyAbilities = true;
        } else {
            CNAbility cna = CNAbilityFactory.getCNAbility(category, Nature.NORMAL, ability);
            CNAbilitySelection cnas = new CNAbilitySelection(cna, as.selection);
            abilitiesToAdd.add(cnas);
            aPC.addAbility(cnas, UserSelection.getInstance(), this);
        }
    }
    if (tooManyAbilities) {
        warnings.add("ABILITY: Some Abilities were not granted -- not enough remaining feats");
        return false;
    }
    return true;
}
Also used : Ability(pcgen.core.Ability) CNAbility(pcgen.cdom.content.CNAbility) CNAbilitySelection(pcgen.cdom.helper.CNAbilitySelection) ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal) CNAbility(pcgen.cdom.content.CNAbility) CNAbilitySelection(pcgen.cdom.helper.CNAbilitySelection) AbilityCategory(pcgen.core.AbilityCategory)

Example 34 with CNAbility

use of pcgen.cdom.content.CNAbility in project pcgen by PCGen.

the class AbilityTargetSaveRestoreTest method applyObject.

@Override
protected void applyObject(Ability obj) {
    String assoc = null;
    if (ChooseActivation.hasNewChooseToken(obj)) {
        assoc = "Granted";
    }
    CNAbility cna = CNAbilityFactory.getCNAbility(AbilityCategory.FEAT, Nature.NORMAL, obj);
    CNAbilitySelection cnas = new CNAbilitySelection(cna, assoc);
    pc.addAbility(cnas, UserSelection.getInstance(), UserSelection.getInstance());
}
Also used : CNAbility(pcgen.cdom.content.CNAbility) CNAbilitySelection(pcgen.cdom.helper.CNAbilitySelection)

Example 35 with CNAbility

use of pcgen.cdom.content.CNAbility in project pcgen by PCGen.

the class Description method getDescription.

/**
	 * Gets the description string after having tested all prereqs and 
	 * substituting all variables.
	 * 
	 * @param aPC The PlayerCharacter used to evaluate formulas.
	 * 
	 * @return The fully substituted description string.
	 */
public String getDescription(final PlayerCharacter aPC, List<? extends Object> objList) {
    if (objList.isEmpty()) {
        return Constants.EMPTY_STRING;
    }
    PObject sampleObject;
    Object b = objList.get(0);
    if (b instanceof PObject) {
        sampleObject = (PObject) b;
    } else if (b instanceof CNAbility) {
        sampleObject = ((CNAbility) b).getAbility();
    } else {
        Logging.errorPrint("Unable to resolve Description with object of type: " + b.getClass().getName());
        return Constants.EMPTY_STRING;
    }
    final StringBuilder buf = new StringBuilder(250);
    if (this.qualifies(aPC, sampleObject)) {
        for (final String comp : theComponents) {
            if (comp.startsWith(VAR_MARKER)) {
                final int ind = Integer.parseInt(comp.substring(VAR_MARKER.length()));
                if (theVariables == null || ind > theVariables.size()) {
                    continue;
                }
                final String var = theVariables.get(ind - 1);
                if (var.equals(VAR_NAME)) {
                    if (sampleObject != null) {
                        buf.append(sampleObject.getOutputName());
                    }
                } else if (var.equals(VAR_CHOICE)) {
                    if (b instanceof ChooseDriver) {
                        ChooseDriver object = (ChooseDriver) b;
                        if (aPC.hasAssociations(object)) {
                            //TODO This is ill defined
                            buf.append(aPC.getAssociationList(object).get(0));
                        }
                    } else {
                        Logging.errorPrint("In Description resolution, " + "Ignoring object of type: " + b.getClass().getName() + " because " + VAR_CHOICE + " was requested but the object does not support CHOOSE");
                        continue;
                    }
                } else if (var.equals(VAR_LIST)) {
                    List<String> assocList = new ArrayList<>();
                    for (Object obj : objList) {
                        if (obj instanceof ChooseDriver) {
                            ChooseDriver object = (ChooseDriver) obj;
                            if (aPC.hasAssociations(object)) {
                                assocList.addAll(aPC.getAssociationList(object));
                            }
                        } else {
                            Logging.errorPrint("In Description resolution, " + "Ignoring object of type: " + b.getClass().getName() + " because " + VAR_CHOICE + " was requested but the object does not support CHOOSE");
                            continue;
                        }
                    }
                    String joinString;
                    if (assocList.size() == 2) {
                        joinString = " and ";
                    } else {
                        joinString = ", ";
                    }
                    Collections.sort(assocList);
                    buf.append(StringUtil.joinToStringBuilder(assocList, joinString));
                } else if (var.startsWith(VAR_FEATS)) {
                    final String featName = var.substring(VAR_FEATS.length());
                    List<CNAbility> feats;
                    if (featName.startsWith("TYPE=") || featName.startsWith("TYPE.")) {
                        feats = aPC.getCNAbilities(AbilityCategory.FEAT);
                    } else {
                        Ability feat = Globals.getContext().getReferenceContext().silentlyGetConstructedCDOMObject(Ability.class, AbilityCategory.FEAT, featName);
                        if (feat == null) {
                            Logging.errorPrint("Found invalid Feat reference in Description: " + featName);
                        }
                        feats = aPC.getMatchingCNAbilities(feat);
                    }
                    boolean needSpace = false;
                    for (final CNAbility cna : feats) {
                        if (cna.getAbility().isType(featName.substring(5))) {
                            if (needSpace) {
                                buf.append(' ');
                            }
                            buf.append(aPC.getDescription(Collections.singletonList(cna)));
                            needSpace = true;
                        }
                    }
                } else if (//$NON-NLS-1$
                var.startsWith("\"")) {
                    buf.append(var.substring(1, var.length() - 1));
                } else {
                    //$NON-NLS-1$
                    buf.append(aPC.getVariableValue(var, "Description").intValue());
                }
            } else {
                buf.append(comp);
            }
        }
    }
    return buf.toString();
}
Also used : CNAbility(pcgen.cdom.content.CNAbility) CNAbility(pcgen.cdom.content.CNAbility) ChooseDriver(pcgen.cdom.base.ChooseDriver) ArrayList(java.util.ArrayList) ConcretePrereqObject(pcgen.cdom.base.ConcretePrereqObject) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

CNAbility (pcgen.cdom.content.CNAbility)131 Ability (pcgen.core.Ability)77 ArrayList (java.util.ArrayList)37 CNAbilitySelection (pcgen.cdom.helper.CNAbilitySelection)28 AbilityCategory (pcgen.core.AbilityCategory)26 Test (org.junit.Test)21 HashMapToList (pcgen.base.util.HashMapToList)16 List (java.util.List)15 PlayerCharacter (pcgen.core.PlayerCharacter)9 Language (pcgen.core.Language)7 HashSet (java.util.HashSet)5 Nature (pcgen.cdom.enumeration.Nature)5 SpecialAbility (pcgen.core.SpecialAbility)5 BonusObj (pcgen.core.bonus.BonusObj)5 BigDecimal (java.math.BigDecimal)4 CDOMObject (pcgen.cdom.base.CDOMObject)4 LoadContext (pcgen.rules.context.LoadContext)4 StringTokenizer (java.util.StringTokenizer)3 GenericMapToList (pcgen.base.util.GenericMapToList)3 PCClass (pcgen.core.PCClass)3