Search in sources :

Example 16 with Ability

use of pcgen.core.Ability in project pcgen by PCGen.

the class PCGVer2Parser method parseTempBonusLine.

/**
	 * ###############################################################
	 * Temporary Bonuses
	 * ###############################################################
	 * @param line
	 **/
private void parseTempBonusLine(final String line) {
    PCGTokenizer tokens;
    try {
        tokens = new PCGTokenizer(line);
    } catch (PCGParseException pcgpex) {
        final String message = "Illegal TempBonus line ignored: " + line + Constants.LINE_SEPARATOR + "Error: " + pcgpex.getMessage();
        warnings.add(message);
        return;
    }
    String cTag = null;
    String tName = null;
    boolean active = true;
    for (PCGElement element : tokens.getElements()) {
        final String tag = element.getName();
        if (IOConstants.TAG_TEMPBONUS.equals(tag)) {
            cTag = EntityEncoder.decode(element.getText());
        } else if (IOConstants.TAG_TEMPBONUSTARGET.equals(tag)) {
            tName = EntityEncoder.decode(element.getText());
        } else if (IOConstants.TAG_TEMPBONUSACTIVE.equals(tag)) {
            active = element.getText().endsWith(IOConstants.VALUE_Y);
        }
    }
    if ((cTag == null) || (tName == null)) {
        warnings.add("Illegal TempBonus line ignored: " + line);
        return;
    }
    //$NON-NLS-1$
    final StringTokenizer aTok = new StringTokenizer(cTag, "=", false);
    if (aTok.countTokens() < 2) {
        return;
    }
    final String cType = aTok.nextToken();
    final String cKey = aTok.nextToken();
    Equipment aEq = null;
    if (!tName.equals(IOConstants.TAG_PC)) {
        // bonus is applied to an equipment item
        // so create a new one and add to PC
        final Equipment eq = thePC.getEquipmentNamed(tName);
        if (eq == null) {
            return;
        }
        aEq = eq.clone();
        //aEq.setWeight("0");
        aEq.resetTempBonusList();
    }
    for (PCGElement element : tokens.getElements()) {
        final String tag = element.getName();
        final String bonus;
        if (IOConstants.TAG_TEMPBONUSBONUS.equals(tag)) {
            bonus = EntityEncoder.decode(element.getText());
        } else {
            continue;
        }
        if ((bonus == null) || (bonus.length() <= 0)) {
            continue;
        }
        BonusObj newB = null;
        Object creator = null;
        LoadContext context = Globals.getContext();
        // type of object to set as the creator
        if (cType.equals(IOConstants.TAG_FEAT)) {
            for (AbilityCategory aCat : SettingsHandler.getGame().getAllAbilityCategories()) {
                Ability a = Globals.getContext().getReferenceContext().silentlyGetConstructedCDOMObject(Ability.class, aCat, cKey);
                if (a != null) {
                    newB = Bonus.newBonus(context, bonus);
                    creator = a;
                    break;
                }
            }
        } else if (cType.equals(IOConstants.TAG_EQUIPMENT)) {
            Equipment aEquip = thePC.getEquipmentNamed(cKey);
            if (aEquip == null) {
                aEquip = context.getReferenceContext().silentlyGetConstructedCDOMObject(Equipment.class, cKey);
            }
            if (aEquip != null) {
                newB = Bonus.newBonus(context, bonus);
                creator = aEquip;
            }
        } else if (cType.equals(IOConstants.TAG_CLASS)) {
            final PCClass aClass = thePC.getClassKeyed(cKey);
            if (aClass == null) {
                continue;
            }
            int idx = bonus.indexOf('|');
            newB = Bonus.newBonus(context, bonus.substring(idx + 1));
            creator = aClass;
        } else if (cType.equals(IOConstants.TAG_TEMPLATE)) {
            PCTemplate aTemplate = context.getReferenceContext().silentlyGetConstructedCDOMObject(PCTemplate.class, cKey);
            if (aTemplate != null) {
                newB = Bonus.newBonus(context, bonus);
                creator = aTemplate;
            }
        } else if (cType.equals(IOConstants.TAG_SKILL)) {
            Skill aSkill = context.getReferenceContext().silentlyGetConstructedCDOMObject(Skill.class, cKey);
            if (aSkill != null) {
                newB = Bonus.newBonus(context, bonus);
                creator = aSkill;
            }
        } else if (cType.equals(IOConstants.TAG_SPELL)) {
            final Spell aSpell = context.getReferenceContext().silentlyGetConstructedCDOMObject(Spell.class, cKey);
            if (aSpell != null) {
                newB = Bonus.newBonus(context, bonus);
                creator = aSpell;
            }
        } else if (cType.equals(IOConstants.TAG_NAME)) {
            newB = Bonus.newBonus(context, bonus);
        //newB.setCreatorObject(thePC);
        }
        if (newB == null) {
            return;
        }
        TempBonusInfo tempBonusInfo;
        // Check to see if the target was the PC or an Item
        if (tName.equals(IOConstants.TAG_PC)) {
            thePC.setApplied(newB, true);
            tempBonusInfo = thePC.addTempBonus(newB, creator, thePC);
        } else {
            thePC.setApplied(newB, true);
            aEq.addTempBonus(newB);
            tempBonusInfo = thePC.addTempBonus(newB, creator, aEq);
        }
        if (!active) {
            String bonusName = BonusDisplay.getBonusDisplayName(tempBonusInfo);
            thePC.setTempBonusFilter(bonusName);
        }
    }
    if (aEq != null) {
        aEq.setAppliedName(cKey);
        thePC.addTempBonusItemList(aEq);
    }
}
Also used : CNAbility(pcgen.cdom.content.CNAbility) Ability(pcgen.core.Ability) SpecialAbility(pcgen.core.SpecialAbility) BonusObj(pcgen.core.bonus.BonusObj) PCClass(pcgen.core.PCClass) Spell(pcgen.core.spell.Spell) CharacterSpell(pcgen.core.character.CharacterSpell) TempBonusInfo(pcgen.core.BonusManager.TempBonusInfo) StringTokenizer(java.util.StringTokenizer) Skill(pcgen.core.Skill) Equipment(pcgen.core.Equipment) LoadContext(pcgen.rules.context.LoadContext) CDOMObject(pcgen.cdom.base.CDOMObject) AssociatedPrereqObject(pcgen.cdom.base.AssociatedPrereqObject) PObject(pcgen.core.PObject) PCTemplate(pcgen.core.PCTemplate) AbilityCategory(pcgen.core.AbilityCategory)

Example 17 with Ability

use of pcgen.core.Ability in project pcgen by PCGen.

the class PCGVer2Creator method writeAbilityToBuffer.

private void writeAbilityToBuffer(StringBuilder buffer, CNAbility cna) {
    Category<Ability> cat = cna.getAbilityCategory();
    Nature nature = cna.getNature();
    Ability ability = cna.getAbility();
    buffer.append(IOConstants.TAG_ABILITY).append(IOConstants.TAG_END);
    buffer.append(EntityEncoder.encode(cat.getKeyName())).append(IOConstants.TAG_SEPARATOR);
    buffer.append(IOConstants.TAG_TYPE).append(IOConstants.TAG_END);
    buffer.append(EntityEncoder.encode(nature.toString())).append(IOConstants.TAG_SEPARATOR);
    buffer.append(IOConstants.TAG_CATEGORY).append(IOConstants.TAG_END);
    buffer.append(EntityEncoder.encode(ability.getCategory())).append(IOConstants.TAG_SEPARATOR);
    buffer.append(IOConstants.TAG_MAPKEY).append(IOConstants.TAG_END);
    buffer.append(EntityEncoder.encode(ability.getKeyName())).append(IOConstants.TAG_SEPARATOR);
    if (ability.getSafe(ObjectKey.MULTIPLE_ALLOWED)) {
        buffer.append(IOConstants.TAG_APPLIEDTO).append(IOConstants.TAG_END);
        List<String> assocList = thePC.getAssociationList(cna);
        boolean first = true;
        for (String assoc : assocList) {
            if (!first) {
                buffer.append(Constants.COMMA);
            }
            first = false;
            buffer.append(EntityEncoder.encode(assoc));
        }
        buffer.append(IOConstants.TAG_SEPARATOR);
    }
    buffer.append(IOConstants.TAG_TYPE).append(IOConstants.TAG_END);
    buffer.append(EntityEncoder.encode(ability.getType()));
    for (final BonusObj save : thePC.getSaveableBonusList(ability)) {
        buffer.append('|');
        buffer.append(IOConstants.TAG_SAVE).append(':');
        buffer.append(EntityEncoder.encode("BONUS|" + save));
    }
    for (final Description desc : ability.getSafeListFor(ListKey.DESCRIPTION)) {
        buffer.append(Constants.PIPE);
        buffer.append(IOConstants.TAG_DESC).append(':');
        buffer.append(EntityEncoder.encode(desc.getPCCText()));
    }
    buffer.append(IOConstants.LINE_SEP);
}
Also used : Ability(pcgen.core.Ability) CNAbility(pcgen.cdom.content.CNAbility) SpecialAbility(pcgen.core.SpecialAbility) Nature(pcgen.cdom.enumeration.Nature) Description(pcgen.core.Description) BonusObj(pcgen.core.bonus.BonusObj)

Example 18 with Ability

use of pcgen.core.Ability in project pcgen by PCGen.

the class PCGVer2Creator method appendSpellLines.

/*
	 * ###############################################################
	 * Character Spells Information methods
	 * ###############################################################
	 */
/*
	 * #Character Spells Information
	 * CLASS:Wizard|CANCASTPERDAY:2,4(Totals the levels all up + includes attribute bonuses)
	 * SPELLNAME:Blah|SCHOOL:blah|SUBSCHOOL:blah|Etc
	 *
	 * completely changed due to new Spell API
	 */
private void appendSpellLines(StringBuilder buffer) {
    for (PCClass pcClass : charDisplay.getClassSet()) {
        Collection<? extends CharacterSpell> sp = charDisplay.getCharacterSpells(pcClass);
        List<CharacterSpell> classSpells = new ArrayList<>(sp);
        // Add in the spells granted by objects
        thePC.addBonusKnownSpellsToList(pcClass, classSpells);
        Collections.sort(classSpells);
        for (CharacterSpell cSpell : classSpells) {
            for (SpellInfo spellInfo : cSpell.getInfoList()) {
                CDOMObject owner = cSpell.getOwner();
                List<? extends CDOMList<Spell>> lists = charDisplay.getSpellLists(owner);
                if (SpellLevel.getFirstLevelForKey(cSpell.getSpell(), lists, thePC) < 0) {
                    Logging.errorPrint("Ignoring unqualified spell " + cSpell.getSpell() + " in list for class " + pcClass + ".");
                    continue;
                }
                if (spellInfo.getBook().equals(Globals.getDefaultSpellBook()) && thePC.getSpellSupport(pcClass).isAutoKnownSpell(cSpell.getSpell(), SpellLevel.getFirstLevelForKey(cSpell.getSpell(), lists, thePC), false, thePC) && thePC.getAutoSpells()) {
                    continue;
                }
                buffer.append(IOConstants.TAG_SPELLNAME).append(':');
                buffer.append(EntityEncoder.encode(cSpell.getSpell().getKeyName()));
                buffer.append('|');
                buffer.append(IOConstants.TAG_TIMES).append(':');
                buffer.append(spellInfo.getTimes());
                buffer.append('|');
                buffer.append(IOConstants.TAG_CLASS).append(':');
                buffer.append(EntityEncoder.encode(pcClass.getKeyName()));
                buffer.append('|');
                buffer.append(IOConstants.TAG_SPELL_BOOK).append(':');
                buffer.append(EntityEncoder.encode(spellInfo.getBook()));
                buffer.append('|');
                buffer.append(IOConstants.TAG_SPELLLEVEL).append(':');
                buffer.append(spellInfo.getActualLevel());
                if (spellInfo.getNumPages() > 0) {
                    buffer.append('|');
                    buffer.append(IOConstants.TAG_SPELLNUMPAGES).append(':');
                    buffer.append(spellInfo.getNumPages());
                }
                final List<Ability> metaFeats = spellInfo.getFeatList();
                if ((metaFeats != null) && (!metaFeats.isEmpty())) {
                    buffer.append('|');
                    buffer.append(IOConstants.TAG_FEATLIST).append(':');
                    buffer.append('[');
                    String del = Constants.EMPTY_STRING;
                    for (Ability feat : metaFeats) {
                        buffer.append(del);
                        buffer.append(IOConstants.TAG_FEAT).append(':');
                        buffer.append(EntityEncoder.encode(feat.getKeyName()));
                        //$NON-NLS-1$
                        del = "|";
                    }
                    buffer.append(']');
                }
                buffer.append('|');
                appendSourceInTaggedFormat(buffer, StringPClassUtil.getStringFor(owner.getClass()) + "|" + owner.getKeyName());
                buffer.append(IOConstants.LINE_SEP);
            }
        }
    }
}
Also used : Ability(pcgen.core.Ability) CNAbility(pcgen.cdom.content.CNAbility) SpecialAbility(pcgen.core.SpecialAbility) CDOMObject(pcgen.cdom.base.CDOMObject) ArrayList(java.util.ArrayList) CharacterSpell(pcgen.core.character.CharacterSpell) PCClass(pcgen.core.PCClass) Spell(pcgen.core.spell.Spell) CharacterSpell(pcgen.core.character.CharacterSpell) SpellInfo(pcgen.core.character.SpellInfo)

Example 19 with Ability

use of pcgen.core.Ability in project pcgen by PCGen.

the class PCGVer2Parser method parseFeatLine.

/*
	 * ###############################################################
	 * Character Feats methods
	 * ###############################################################
	 */
private void parseFeatLine(final String line) {
    final PCGTokenizer tokens;
    try {
        tokens = new PCGTokenizer(line);
    } catch (PCGParseException pcgpex) {
        final String msg = LanguageBundle.getFormattedString(//$NON-NLS-1$
        "Warnings.PCGenParser.IllegalFeat", line, pcgpex.getMessage());
        warnings.add(msg);
        return;
    }
    final Iterator<PCGElement> it = tokens.getElements().iterator();
    // the first element defines the Ability key name
    if (it.hasNext()) {
        final PCGElement element = it.next();
        final String abilityKey = EntityEncoder.decode(element.getText());
        /* First, check to see if the PC already has this ability. If so,
			 * then we just need to mod it. Otherwise we need to create a new
			 * one and add it using non-aggregate (when using aggregate, we
			 * get clones of the PCs actual feats, which don't get saved or
			 * preserved) */
        Ability anAbility = Globals.getContext().getReferenceContext().silentlyGetConstructedCDOMObject(Ability.class, AbilityCategory.FEAT, abilityKey);
        if (anAbility == null) {
            final String msg = LanguageBundle.getFormattedString(//$NON-NLS-1$
            "Warnings.PCGenParser.CouldntAddAbility", abilityKey);
            warnings.add(msg);
            return;
        }
        CNAbility pcAbility = CNAbilityFactory.getCNAbility(AbilityCategory.FEAT, Nature.NORMAL, anAbility);
        if (!anAbility.getSafe(ObjectKey.MULTIPLE_ALLOWED)) {
            thePC.addAbility(new CNAbilitySelection(pcAbility), UserSelection.getInstance(), UserSelection.getInstance());
        }
        parseFeatsHandleAppliedToAndSaveTags(it, pcAbility);
        featsPresent = true;
    }
}
Also used : CNAbility(pcgen.cdom.content.CNAbility) Ability(pcgen.core.Ability) SpecialAbility(pcgen.core.SpecialAbility) CNAbility(pcgen.cdom.content.CNAbility) CNAbilitySelection(pcgen.cdom.helper.CNAbilitySelection)

Example 20 with Ability

use of pcgen.core.Ability in project pcgen by PCGen.

the class AbilityListToken method getTokenForCategory.

/**
	 * Produce the ABILITY token output for a specific ability 
	 * category.
	 *  
	 * @param pc The character being processed.
	 * @param aTok The tokenised request, already past the category.
	 * @param tokenString The output token requested 
	 * @param aCategory The ability category being output.
	 * @return The token value.
	 */
protected String getTokenForCategory(PlayerCharacter pc, final StringTokenizer aTok, final String tokenString, final AbilityCategory aCategory) {
    if (aCategory == null) {
        return "";
    }
    StringBuilder retString = new StringBuilder();
    // once
    if (lastPC != pc || !aCategory.equals(lastCategory) || lastPCSerial != pc.getSerial() || !tokenString.equals(lastType)) {
        abilityMap = getAbilityList(pc, aCategory);
        lastPC = pc;
        lastCategory = aCategory;
        lastPCSerial = pc.getSerial();
        lastType = tokenString;
    }
    // Default values
    List<String> types = new ArrayList<>();
    List<String> negate = new ArrayList<>();
    String aspect = null;
    while (aTok.hasMoreTokens()) {
        final String typeStr = aTok.nextToken();
        int typeInd = typeStr.indexOf("TYPE=");
        if (typeInd != -1 && typeStr.length() > 5) {
            if (typeInd > 0) {
                negate.add(typeStr.substring(typeInd + 5));
            } else {
                types.add(typeStr.substring(typeInd + 5));
            }
        }
        int aspectInd = typeStr.indexOf("ASPECT=");
        if (aspectInd != -1 && typeStr.length() > 7) {
            aspect = typeStr.substring(aspectInd + 7);
        }
    }
    MapToList<Ability, CNAbility> aList = AbilityToken.buildAbilityList(types, negate, null, View.VISIBLE_EXPORT, aspect, abilityMap);
    boolean needComma = false;
    for (Ability ability : aList.getKeySet()) {
        if (needComma) {
            retString.append(DELIM);
        }
        needComma = true;
        retString.append(QualifiedName.qualifiedName(pc, aList.getListFor(ability)));
    }
    return retString.toString();
}
Also used : Ability(pcgen.core.Ability) CNAbility(pcgen.cdom.content.CNAbility) CNAbility(pcgen.cdom.content.CNAbility) ArrayList(java.util.ArrayList)

Aggregations

Ability (pcgen.core.Ability)279 CNAbility (pcgen.cdom.content.CNAbility)128 AbilityCategory (pcgen.core.AbilityCategory)60 PlayerCharacter (pcgen.core.PlayerCharacter)54 Test (org.junit.Test)46 ArrayList (java.util.ArrayList)43 CNAbilitySelection (pcgen.cdom.helper.CNAbilitySelection)25 ParseResult (pcgen.rules.persistence.token.ParseResult)21 HashMapToList (pcgen.base.util.HashMapToList)15 PCClass (pcgen.core.PCClass)15 Spell (pcgen.core.spell.Spell)15 StringTokenizer (java.util.StringTokenizer)14 TestContext (plugin.lsttokens.editcontext.testsupport.TestContext)13 LoadContext (pcgen.rules.context.LoadContext)12 SpecialAbility (pcgen.core.SpecialAbility)11 CharacterSpell (pcgen.core.character.CharacterSpell)11 PreParserFactory (pcgen.persistence.lst.prereq.PreParserFactory)11 List (java.util.List)10 CDOMObject (pcgen.cdom.base.CDOMObject)10 CDOMSingleRef (pcgen.cdom.reference.CDOMSingleRef)10