Search in sources :

Example 1 with TempBonusInfo

use of pcgen.core.BonusManager.TempBonusInfo 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 2 with TempBonusInfo

use of pcgen.core.BonusManager.TempBonusInfo in project pcgen by PCGen.

the class PCGVer2Creator method appendTempBonuses.

private void appendTempBonuses(StringBuilder buffer) {
    final List<String> trackList = new ArrayList<>();
    TreeSet<Map.Entry<BonusObj, BonusManager.TempBonusInfo>> sortedbonus = new TreeSet<>(new Comparator<Map.Entry<BonusObj, BonusManager.TempBonusInfo>>() {

        @Override
        public int compare(Map.Entry<BonusObj, BonusManager.TempBonusInfo> a, Map.Entry<BonusObj, BonusManager.TempBonusInfo> b) {
            BonusObj keyA = a.getKey();
            BonusObj keyB = b.getKey();
            if (!keyA.getBonusName().equals(keyB.getBonusName())) {
                return keyA.getBonusName().compareTo(keyB.getBonusName());
            }
            if (!keyA.getBonusInfo().equals(keyB.getBonusInfo())) {
                return keyA.getBonusInfo().compareTo(keyB.getBonusInfo());
            }
            return keyA.getPCCText().compareTo(keyB.getPCCText());
        }
    });
    sortedbonus.addAll(thePC.getTempBonusMap().entrySet());
    //for (BonusManager.TempBonusInfo tbi : thePC.getTempBonusMap().values())
    for (Map.Entry<BonusObj, BonusManager.TempBonusInfo> me : sortedbonus) {
        BonusObj bonus = me.getKey();
        TempBonusInfo tbi = me.getValue();
        Object creObj = tbi.source;
        Object tarObj = tbi.target;
        final String outString = tempBonusName(creObj, tarObj);
        if (trackList.contains(outString)) {
            continue;
        }
        trackList.add(outString);
        buffer.append(outString);
        String bonusName = BonusDisplay.getBonusDisplayName(tbi);
        if (thePC.getTempBonusFilters().contains(bonusName)) {
            buffer.append('|');
            buffer.append(IOConstants.TAG_TEMPBONUSACTIVE).append(":N");
        }
        /*
			 * Why do we loop through the bonuses again? It is looped through
			 * again so that only items associated with this source (e.g.
			 * Template and Target object) are written, but that ALL of the
			 * items are written on one line.
			 */
        for (Map.Entry<BonusObj, BonusManager.TempBonusInfo> subme : sortedbonus) {
            BonusObj subBonus = subme.getKey();
            TempBonusInfo subtbi = subme.getValue();
            Object cObj = subtbi.source;
            Object tObj = subtbi.target;
            final String inString = tempBonusName(cObj, tObj);
            if (inString.equals(outString)) {
                buffer.append('|');
                buffer.append(IOConstants.TAG_TEMPBONUSBONUS).append(':');
                buffer.append(EntityEncoder.encode(subBonus.getPCCText()));
            }
        }
        buffer.append(IOConstants.LINE_SEP);
    }
}
Also used : BonusObj(pcgen.core.bonus.BonusObj) ArrayList(java.util.ArrayList) TempBonusInfo(pcgen.core.BonusManager.TempBonusInfo) ChronicleEntry(pcgen.core.ChronicleEntry) TreeSet(java.util.TreeSet) BonusManager(pcgen.core.BonusManager) CDOMListObject(pcgen.cdom.base.CDOMListObject) CDOMObject(pcgen.cdom.base.CDOMObject) Map(java.util.Map)

Example 3 with TempBonusInfo

use of pcgen.core.BonusManager.TempBonusInfo in project pcgen by PCGen.

the class TempBonusHelper method removeBonusFromCharacter.

static void removeBonusFromCharacter(PlayerCharacter pc, Equipment aEq, CDOMObject aCreator) {
    for (Map.Entry<BonusObj, BonusManager.TempBonusInfo> me : pc.getTempBonusMap().entrySet()) {
        BonusObj aBonus = me.getKey();
        TempBonusInfo tbi = me.getValue();
        Object aC = tbi.source;
        if (aCreator != aC) {
            continue;
        }
        Object aT = tbi.target;
        if ((aT instanceof Equipment) && (aEq != null)) {
            if (aEq.equals(aT)) {
                pc.removeTempBonus(aBonus);
                pc.removeTempBonusItemList((Equipment) aT);
                ((Equipment) aT).removeTempBonus(aBonus);
                ((Equipment) aT).setAppliedName(EMPTY_STRING);
            }
        } else if ((aT instanceof PlayerCharacter) && (aEq == null)) {
            pc.removeTempBonus(aBonus);
        }
    }
}
Also used : TempBonusInfo(pcgen.core.BonusManager.TempBonusInfo) PlayerCharacter(pcgen.core.PlayerCharacter) BonusObj(pcgen.core.bonus.BonusObj) Equipment(pcgen.core.Equipment) CDOMObject(pcgen.cdom.base.CDOMObject) Map(java.util.Map)

Example 4 with TempBonusInfo

use of pcgen.core.BonusManager.TempBonusInfo in project pcgen by PCGen.

the class Gui2InfoFactory method getHTMLInfo.

@Override
public String getHTMLInfo(TempBonusFacade tempBonusFacade) {
    if (tempBonusFacade == null) {
        return EMPTY_STRING;
    }
    if (!(tempBonusFacade instanceof TempBonusFacadeImpl)) {
        final HtmlInfoBuilder infoText = new HtmlInfoBuilder();
        infoText.appendTitleElement(tempBonusFacade.toString());
        return infoText.toString();
    }
    TempBonusFacadeImpl tempBonus = (TempBonusFacadeImpl) tempBonusFacade;
    CDOMObject originObj = tempBonus.getOriginObj();
    final HtmlInfoBuilder infoText;
    if (originObj instanceof Equipment) {
        infoText = getEquipmentHtmlInfo((Equipment) originObj);
    } else {
        infoText = new HtmlInfoBuilder();
        infoText.appendTitleElement(OutputNameFormatting.piString(originObj, false));
        //$NON-NLS-1$ //$NON-NLS-2$
        infoText.append(" (").append(tempBonus.getOriginType()).append(")");
    }
    if (tempBonus.getTarget() != null) {
        String targetName = charDisplay.getName();
        if (tempBonus.getTarget() instanceof CDOMObject) {
            targetName = ((CDOMObject) tempBonus.getTarget()).getKeyName();
        }
        infoText.appendLineBreak();
        //$NON-NLS-1$
        infoText.appendI18nElement("in_itmInfoLabelTextTarget", targetName);
        StringBuilder bonusValues = new StringBuilder(100);
        Map<BonusObj, TempBonusInfo> bonusMap = pc.getTempBonusMap(originObj.getKeyName(), targetName);
        boolean first = true;
        List<BonusObj> bonusList = new ArrayList<>(bonusMap.keySet());
        bonusList.sort(new BonusComparator());
        for (BonusObj bonusObj : bonusList) {
            if (!first) {
                //$NON-NLS-1$
                bonusValues.append(", ");
            }
            first = false;
            //$NON-NLS-1$
            String adj = ADJ_FMT.format(bonusObj.resolve(pc, ""));
            //$NON-NLS-1$
            bonusValues.append(adj + " " + bonusObj.getDescription());
        }
        if (bonusValues.length() > 0) {
            infoText.appendLineBreak();
            infoText.appendI18nElement(//$NON-NLS-1$
            "in_itmInfoLabelTextEffect", bonusValues.toString());
        }
    }
    if (originObj instanceof Spell) {
        Spell aSpell = (Spell) originObj;
        infoText.appendLineBreak();
        //$NON-NLS-1$
        infoText.appendI18nElement(//$NON-NLS-1$
        "in_spellDuration", aSpell.getListAsString(ListKey.DURATION));
        infoText.appendSpacer();
        //$NON-NLS-1$
        infoText.appendI18nElement(//$NON-NLS-1$
        "in_spellRange", aSpell.getListAsString(ListKey.RANGE));
        infoText.appendSpacer();
        //$NON-NLS-1$
        infoText.appendI18nElement(//$NON-NLS-1$
        "in_spellTarget", aSpell.getSafe(StringKey.TARGET_AREA));
    }
    String aString = originObj.getSafe(StringKey.TEMP_DESCRIPTION);
    if (StringUtils.isEmpty(aString) && originObj instanceof Spell) {
        Spell sp = (Spell) originObj;
        aString = DescriptionFormatting.piWrapDesc(sp, pc.getDescription(sp), false);
    } else if (StringUtils.isEmpty(aString) && originObj instanceof Ability) {
        Ability ab = (Ability) originObj;
        List<CNAbility> wrappedAbility = Collections.singletonList(CNAbilityFactory.getCNAbility(ab.getCDOMCategory(), Nature.NORMAL, ab));
        aString = DescriptionFormatting.piWrapDesc(ab, pc.getDescription(wrappedAbility), false);
    }
    if (!aString.isEmpty()) {
        infoText.appendLineBreak();
        //$NON-NLS-1$
        infoText.appendI18nElement("in_itmInfoLabelTextDesc", aString);
    }
    aString = PrerequisiteUtilities.preReqHTMLStringsForList(pc, null, originObj.getPrerequisiteList(), false);
    if (!aString.isEmpty()) {
        infoText.appendLineBreak();
        //$NON-NLS-1$
        infoText.appendI18nElement("in_requirements", aString);
    }
    infoText.appendLineBreak();
    infoText.appendI18nElement(//$NON-NLS-1$
    "in_itmInfoLabelTextSource", SourceFormat.getFormattedString(originObj, Globals.getSourceDisplay(), true));
    return infoText.toString();
}
Also used : CNAbility(pcgen.cdom.content.CNAbility) Ability(pcgen.core.Ability) BonusObj(pcgen.core.bonus.BonusObj) ArrayList(java.util.ArrayList) Spell(pcgen.core.spell.Spell) CharacterSpell(pcgen.core.character.CharacterSpell) TempBonusInfo(pcgen.core.BonusManager.TempBonusInfo) Equipment(pcgen.core.Equipment) CDOMObject(pcgen.cdom.base.CDOMObject) ArrayList(java.util.ArrayList) List(java.util.List) HtmlInfoBuilder(pcgen.gui2.util.HtmlInfoBuilder)

Example 5 with TempBonusInfo

use of pcgen.core.BonusManager.TempBonusInfo in project pcgen by PCGen.

the class CharacterFacadeImpl method buildAppliedTempBonusList.

/**
	 * Build up the list of temporary bonuses which have been applied to this character.
	 */
private void buildAppliedTempBonusList() {
    Set<String> found = new HashSet<>();
    for (TempBonusInfo tbi : theCharacter.getTempBonusMap().values()) {
        Object aC = tbi.source;
        Object aT = tbi.target;
        String name = BonusDisplay.getBonusDisplayName(tbi);
        if (!found.contains(name)) {
            found.add(name);
            TempBonusFacadeImpl facade = new TempBonusFacadeImpl((CDOMObject) aC, aT, name);
            facade.setActive(!theCharacter.getTempBonusFilters().contains(name));
            appliedTempBonuses.addElement(facade);
        }
    }
}
Also used : TempBonusInfo(pcgen.core.BonusManager.TempBonusInfo) CDOMObject(pcgen.cdom.base.CDOMObject) QualifiedObject(pcgen.core.QualifiedObject) AssociatedPrereqObject(pcgen.cdom.base.AssociatedPrereqObject) PObject(pcgen.core.PObject) HashSet(java.util.HashSet)

Aggregations

TempBonusInfo (pcgen.core.BonusManager.TempBonusInfo)9 BonusObj (pcgen.core.bonus.BonusObj)7 CDOMObject (pcgen.cdom.base.CDOMObject)6 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 Equipment (pcgen.core.Equipment)3 AssociatedPrereqObject (pcgen.cdom.base.AssociatedPrereqObject)2 CDOMListObject (pcgen.cdom.base.CDOMListObject)2 CNAbility (pcgen.cdom.content.CNAbility)2 Ability (pcgen.core.Ability)2 PObject (pcgen.core.PObject)2 CharacterSpell (pcgen.core.character.CharacterSpell)2 Spell (pcgen.core.spell.Spell)2 HashSet (java.util.HashSet)1 List (java.util.List)1 StringTokenizer (java.util.StringTokenizer)1 TreeSet (java.util.TreeSet)1 AbilityCategory (pcgen.core.AbilityCategory)1 BonusManager (pcgen.core.BonusManager)1 ChronicleEntry (pcgen.core.ChronicleEntry)1