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);
}
}
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;
}
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;
}
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());
}
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();
}
Aggregations