use of pcgen.cdom.helper.CNAbilitySelection 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.helper.CNAbilitySelection in project pcgen by PCGen.
the class KitAbilities method apply.
@Override
public void apply(PlayerCharacter aPC) {
for (CNAbilitySelection cnas : abilitiesToAdd) {
aPC.addAbility(cnas, UserSelection.getInstance(), UserSelection.getInstance());
if (isFree()) {
AbilityCategory category = catRef.get();
aPC.adjustAbilities(category, BigDecimal.ONE);
}
}
}
use of pcgen.cdom.helper.CNAbilitySelection 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.helper.CNAbilitySelection in project pcgen by PCGen.
the class AbilityUtilities method processSelection.
private static <T> void processSelection(PlayerCharacter pc, CNAbility cna, ChoiceManagerList<T> aMan, boolean toAdd) {
ArrayList<T> availableList = new ArrayList<>();
ArrayList<T> selectedList = new ArrayList<>();
aMan.getChoices(pc, availableList, selectedList);
if (availableList.isEmpty() && selectedList.isEmpty()) {
//TODO Log error? (ignored choice?)
return;
}
List<T> origSelections = new ArrayList<>(selectedList);
List<T> removedSelections = new ArrayList<>(selectedList);
ArrayList<String> reservedList = new ArrayList<>();
List<T> newSelections;
if (toAdd) {
newSelections = aMan.doChooser(pc, availableList, selectedList, reservedList);
} else {
newSelections = aMan.doChooserRemove(pc, availableList, selectedList, reservedList);
}
//Need to use only the new ones
for (T obj : newSelections) {
removedSelections.remove(obj);
}
//removedSelections.removeAll(newSelections);
for (T obj : origSelections) {
newSelections.remove(obj);
}
for (T sel : newSelections) {
String selection = aMan.encodeChoice(sel);
CNAbilitySelection cnas = new CNAbilitySelection(cna, selection);
pc.addAbility(cnas, UserSelection.getInstance(), UserSelection.getInstance());
}
for (T sel : removedSelections) {
String selection = aMan.encodeChoice(sel);
CNAbilitySelection cnas = new CNAbilitySelection(cna, selection);
pc.removeAbility(cnas, UserSelection.getInstance(), UserSelection.getInstance());
}
}
use of pcgen.cdom.helper.CNAbilitySelection in project pcgen by PCGen.
the class AbstractCNASEnforcingFacet method add.
public boolean add(CharID id, CNAbilitySelection cnas, Object source) {
if (cnas == null) {
throw new IllegalArgumentException("Attempt to add null to list");
}
if (source == null) {
throw new IllegalArgumentException("Attempt to add object with null source to list");
}
List<List<SourcedCNAS>> list = getConstructingList(id);
for (List<SourcedCNAS> slist : list) {
CNAbilitySelection main = slist.get(0).cnas;
if (!CNAbilitySelectionUtilities.canCoExist(main, cnas)) {
slist.add(new SourcedCNAS(cnas, source));
return false;
}
}
List<SourcedCNAS> newList = new ArrayList<>(1);
newList.add(new SourcedCNAS(cnas, source));
list.add(newList);
fireDataFacetChangeEvent(id, cnas, DataFacetChangeEvent.DATA_ADDED);
return true;
}
Aggregations