Search in sources :

Example 1 with Capacity

use of pcgen.cdom.helper.Capacity in project pcgen by PCGen.

the class Equipment method resizeItem.

/**
	 * Change the size of an item
	 * 
	 * @param pc
	 *            The PC carrying the item
	 * @param newSize
	 *            The new size for the item
	 */
public void resizeItem(final PlayerCharacter pc, SizeAdjustment newSize) {
    setBase();
    final int iOldSize = sizeInt();
    int iNewSize = newSize.get(IntegerKey.SIZEORDER);
    if (iNewSize != iOldSize) {
        put(ObjectKey.SIZE, CDOMDirectSingleRef.getRef(newSize));
        CDOMSingleRef<Equipment> baseItem = get(ObjectKey.BASE_ITEM);
        Equipment eq;
        if (baseItem == null) {
            eq = this;
        } else {
            eq = baseItem.get();
        }
        put(ObjectKey.CURRENT_COST, eq.getCostAdjustedForSize(pc, newSize));
        put(ObjectKey.WEIGHT, eq.getWeightAdjustedForSize(pc, newSize));
        adjustACForSize(pc, eq, newSize);
        String dam = eq.getDamageAdjustedForSize(newSize, true);
        if (dam != null && !dam.isEmpty()) {
            getEquipmentHead(1).put(StringKey.DAMAGE, dam);
        }
        String adam = eq.getDamageAdjustedForSize(newSize, false);
        if (adam != null && !adam.isEmpty()) {
            getEquipmentHead(2).put(StringKey.DAMAGE, adam);
        }
        //
        // Adjust the capacity of the container (if it is one)
        //
        BigDecimal weightCap = get(ObjectKey.CONTAINER_WEIGHT_CAPACITY);
        if (weightCap != null) {
            double mult = 1.0;
            if (newSize != null && pc != null) {
                mult = pc.getSizeBonusTo(newSize, "ITEMCAPACITY", eq.typeList(), 1.0);
            }
            BigDecimal multbd = new BigDecimal(mult);
            if (!Capacity.UNLIMITED.equals(weightCap)) {
                // CONSIDER ICK, ICK, direct access bad
                put(ObjectKey.CONTAINER_WEIGHT_CAPACITY, weightCap.multiply(multbd));
            }
            List<Capacity> capacity = removeListFor(ListKey.CAPACITY);
            if (capacity != null) {
                for (Capacity cap : capacity) {
                    BigDecimal content = cap.getCapacity();
                    if (!Capacity.UNLIMITED.equals(content)) {
                        content = content.multiply(multbd);
                    }
                    // CONSIDER ICK, ICK, direct access bad
                    addToListFor(ListKey.CAPACITY, new Capacity(cap.getType(), content));
                }
            }
            updateContainerCapacityString();
        }
    }
    //
    if (hasPrerequisites()) {
        AbstractReferenceContext ref = Globals.getContext().getReferenceContext();
        int maxIndex = ref.getConstructedObjectCount(SizeAdjustment.class);
        for (Prerequisite aBonus : getPrerequisiteList()) {
            if ("SIZE".equalsIgnoreCase(aBonus.getKind())) {
                SizeAdjustment sa = ref.silentlyGetConstructedCDOMObject(SizeAdjustment.class, aBonus.getOperand());
                final int iOldPre = sa.get(IntegerKey.SIZEORDER);
                iNewSize += (iOldPre - iOldSize);
                if ((iNewSize >= 0) && (iNewSize <= maxIndex)) {
                    // Note: This actually impacts the Prereq in this
                    // Equipment, since it is returned
                    // by reference from the get above ... thus no need to
                    // perform a set
                    SizeAdjustment size = ref.getSortedList(SizeAdjustment.class, IntegerKey.SIZEORDER).get(iNewSize);
                    aBonus.setOperand(size.getKeyName());
                }
            }
        }
    }
}
Also used : Capacity(pcgen.cdom.helper.Capacity) AbstractReferenceContext(pcgen.rules.context.AbstractReferenceContext) BigDecimal(java.math.BigDecimal) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 2 with Capacity

use of pcgen.cdom.helper.Capacity in project pcgen by PCGen.

the class Equipment method pickChildType.

/**
	 * Checks whether the Equipment can hold quantToAdd more of an item which
	 * has the types in aTypeList
	 *  
	 * @param aTypeList The types of the Equipment we want to add
	 *            
	 * @param quantToAdd how many to add
	 *            
	 * @return true if the Equipement can hold quantToAdd more of the item
	 */
private String pickChildType(final SortedSet<String> aTypeList, final Float quantToAdd) {
    Capacity totalCap = get(ObjectKey.TOTAL_CAPACITY);
    BigDecimal capValue = totalCap == null ? BigDecimal.ZERO : totalCap.getCapacity();
    if (getChildType("Total") == null) {
        setChildType("Total", 0.0f);
    }
    String canContain = "";
    if ((getChildType("Total") + quantToAdd) <= capValue.doubleValue()) {
        boolean anyContain = false;
        float childType = containsChildType("Any") ? getChildType("Any") : 0.0f;
        CAPFOR: for (Capacity c : getSafeListFor(ListKey.CAPACITY)) {
            String capType = c.getType();
            double val = c.getCapacity().doubleValue();
            for (String aType : aTypeList) {
                if (capType.equalsIgnoreCase(aType)) {
                    if (containsChildType(aType) && ((getChildType(aType) + quantToAdd) <= val) || quantToAdd <= val) {
                        canContain = aType;
                        break CAPFOR;
                    }
                } else if ("Any".equalsIgnoreCase(capType)) {
                    if ((childType + quantToAdd) <= val) {
                        anyContain = true;
                    }
                }
            }
        }
        if (("".equals(canContain)) && anyContain) {
            if (!containsChildType("Any")) {
                setChildType("Any", (float) 0);
            }
            canContain = "Any";
        }
    }
    return canContain;
}
Also used : Capacity(pcgen.cdom.helper.Capacity) BigDecimal(java.math.BigDecimal)

Example 3 with Capacity

use of pcgen.cdom.helper.Capacity in project pcgen by PCGen.

the class WeaponTokenTest method setUp.

/*
	 * @see TestCase#setUp()
	 */
@Override
protected void setUp() throws Exception {
    super.setUp();
    PlayerCharacter character = getCharacter();
    LoadContext context = Globals.getContext();
    //Stats
    setPCStat(character, str, 15);
    setPCStat(character, dex, 16);
    setPCStat(character, intel, 17);
    BonusObj aBonus = Bonus.newBonus(context, "COMBAT|TOHIT.Melee|STR|TYPE=Ability");
    if (aBonus != null) {
        str.addToListFor(ListKey.BONUS, aBonus);
    }
    aBonus = Bonus.newBonus(context, "COMBAT|DAMAGE.Melee,DAMAGE.Thrown|STR");
    if (aBonus != null) {
        str.addToListFor(ListKey.BONUS, aBonus);
    }
    aBonus = Bonus.newBonus(context, "COMBAT|DAMAGEMULT:0|0.5*(STR>=0)");
    if (aBonus != null) {
        str.addToListFor(ListKey.BONUS, aBonus);
    }
    aBonus = Bonus.newBonus(context, "COMBAT|DAMAGEMULT:1|1");
    if (aBonus != null) {
        str.addToListFor(ListKey.BONUS, aBonus);
    }
    aBonus = Bonus.newBonus(context, "COMBAT|DAMAGEMULT:2|1.5*(STR>=0)");
    if (aBonus != null) {
        str.addToListFor(ListKey.BONUS, aBonus);
    }
    aBonus = Bonus.newBonus(context, "MODSKILLPOINTS|NUMBER|INT");
    if (aBonus != null) {
        intel.addToListFor(ListKey.BONUS, aBonus);
    }
    // Race
    Race testRace = new Race();
    testRace.setName("TestRace");
    testRace.put(StringKey.KEY_NAME, "KEY_TEST_RACE");
    CDOMDirectSingleRef<SizeAdjustment> mediumRef = CDOMDirectSingleRef.getRef(medium);
    CDOMDirectSingleRef<SizeAdjustment> largeRef = CDOMDirectSingleRef.getRef(large);
    testRace.put(FormulaKey.SIZE, new FixedSizeFormula(mediumRef));
    character.setRace(testRace);
    // Class
    PCClass myClass = new PCClass();
    myClass.setName("My Class");
    myClass.put(StringKey.KEY_NAME, "KEY_MY_CLASS");
    myClass.put(FormulaKey.START_SKILL_POINTS, FormulaFactory.getFormulaFor(3));
    final BonusObj babClassBonus = Bonus.newBonus(context, "COMBAT|BASEAB|CL+15");
    myClass.getOriginalClassLevel(1).addToListFor(ListKey.BONUS, babClassBonus);
    context.getReferenceContext().importObject(myClass);
    character.incrementClassLevel(1, myClass, true);
    character.calcActiveBonuses();
    dblWpn = new Equipment();
    dblWpn.setName("DoubleWpn");
    dblWpn.put(StringKey.KEY_NAME, "KEY_DOUBLE_WPN");
    TestHelper.addType(dblWpn, "Weapon.Melee.Martial.Double.Standard.Bludgeoning.Flail");
    dblWpn.getEquipmentHead(1).put(StringKey.DAMAGE, "1d10");
    dblWpn.getEquipmentHead(2).put(StringKey.DAMAGE, "1d6");
    dblWpn.getEquipmentHead(1).put(IntegerKey.CRIT_MULT, 2);
    dblWpn.getEquipmentHead(2).put(IntegerKey.CRIT_MULT, 2);
    dblWpn.getEquipmentHead(1).put(IntegerKey.CRIT_RANGE, 1);
    dblWpn.getEquipmentHead(2).put(IntegerKey.CRIT_RANGE, 1);
    dblWpn.put(IntegerKey.SLOTS, 2);
    dblWpn.put(ObjectKey.WIELD, context.getReferenceContext().silentlyGetConstructedCDOMObject(WieldCategory.class, "TwoHanded"));
    dblWpn.put(ObjectKey.SIZE, mediumRef);
    dblWpn.put(ObjectKey.BASESIZE, mediumRef);
    character.addEquipment(dblWpn);
    EquipSet def = new EquipSet("0.1", "Default");
    character.addEquipSet(def);
    EquipSet es = new EquipSet("0.1.1", "Double Weapon", dblWpn.getKeyName(), dblWpn);
    character.addEquipSet(es);
    character.setCalcEquipmentList();
    WeaponProf wp = new WeaponProf();
    wp.setName("DoubleWpn");
    wp.put(IntegerKey.HANDS, Constants.HANDS_SIZE_DEPENDENT);
    context.getReferenceContext().importObject(wp);
    wp = new WeaponProf();
    wp.setName("Sword (Bastard)");
    wp.put(StringKey.KEY_NAME, "KEY_Sword (Bastard)");
    TestHelper.addType(wp, "MARTIAL.EXOTIC");
    context.getReferenceContext().importObject(wp);
    bastardSword = new Equipment();
    bastardSword.setName("Sword, Bastard");
    bastardSword.put(StringKey.KEY_NAME, "BASTARD_SWORD");
    bastardSword.put(ObjectKey.WEAPON_PROF, new CDOMDirectSingleRef<>(wp));
    TestHelper.addType(bastardSword, "Weapon.Melee.Martial.Exotic.Standard.Slashing.Sword");
    bastardSword.getEquipmentHead(1).put(StringKey.DAMAGE, "1d10");
    bastardSword.getEquipmentHead(1).put(IntegerKey.CRIT_MULT, 2);
    bastardSword.getEquipmentHead(1).put(IntegerKey.CRIT_RANGE, 2);
    bastardSword.put(ObjectKey.WIELD, context.getReferenceContext().silentlyGetConstructedCDOMObject(WieldCategory.class, "TwoHanded"));
    bastardSword.put(ObjectKey.SIZE, mediumRef);
    bastardSword.put(ObjectKey.BASESIZE, mediumRef);
    wp = new WeaponProf();
    wp.setName("Longsword");
    wp.put(StringKey.KEY_NAME, "KEY_LONGSWORD");
    wp.addToListFor(ListKey.TYPE, Type.getConstant("MARTIAL"));
    context.getReferenceContext().importObject(wp);
    largeSword = new Equipment();
    largeSword.setName("Longsword (Large)");
    largeSword.put(StringKey.KEY_NAME, "KEY_LONGSWORD_LARGE");
    largeSword.put(StringKey.OUTPUT_NAME, "Longsword (Large)");
    largeSword.put(ObjectKey.WEAPON_PROF, new CDOMDirectSingleRef<>(wp));
    TestHelper.addType(largeSword, "Weapon.Melee.Martial.Standard.Slashing.Sword");
    largeSword.getEquipmentHead(1).put(StringKey.DAMAGE, "1d10");
    largeSword.getEquipmentHead(1).put(IntegerKey.CRIT_MULT, 2);
    largeSword.getEquipmentHead(1).put(IntegerKey.CRIT_RANGE, 2);
    largeSword.put(ObjectKey.WIELD, context.getReferenceContext().silentlyGetConstructedCDOMObject(WieldCategory.class, "OneHanded"));
    largeSword.put(ObjectKey.SIZE, largeRef);
    largeSword.put(ObjectKey.BASESIZE, largeRef);
    fineSword = new Equipment();
    fineSword.setName("Longsword (Fine)");
    fineSword.put(StringKey.KEY_NAME, "KEY_LONGSWORD_FINE");
    fineSword.put(StringKey.OUTPUT_NAME, "Longsword (Fine)");
    fineSword.put(ObjectKey.WEAPON_PROF, new CDOMDirectSingleRef<>(wp));
    TestHelper.addType(fineSword, "Weapon.Melee.Martial.Standard.Slashing.Sword.Finesseable");
    fineSword.getEquipmentHead(1).put(StringKey.DAMAGE, "1d10");
    fineSword.getEquipmentHead(1).put(IntegerKey.CRIT_MULT, 2);
    fineSword.getEquipmentHead(1).put(IntegerKey.CRIT_RANGE, 2);
    fineSword.put(ObjectKey.WIELD, context.getReferenceContext().silentlyGetConstructedCDOMObject(WieldCategory.class, "OneHanded"));
    fineSword.put(ObjectKey.SIZE, mediumRef);
    fineSword.put(ObjectKey.BASESIZE, mediumRef);
    WeaponProf spearwp = new WeaponProf();
    spearwp.setName("Spear");
    spearwp.put(StringKey.KEY_NAME, "KEY_SPEAR");
    spearwp.addToListFor(ListKey.TYPE, Type.getConstant("MARTIAL"));
    context.getReferenceContext().importObject(spearwp);
    longSpear = new Equipment();
    longSpear.setName("Longspear");
    longSpear.put(StringKey.KEY_NAME, "KEY_LONGSPEAR");
    longSpear.put(StringKey.OUTPUT_NAME, "Longspear");
    longSpear.put(ObjectKey.WEAPON_PROF, new CDOMDirectSingleRef<>(spearwp));
    TestHelper.addType(longSpear, "Weapon.Melee.Martial.Standard.Piercing.Spear");
    longSpear.getEquipmentHead(1).put(StringKey.DAMAGE, "1d6");
    longSpear.getEquipmentHead(1).put(IntegerKey.CRIT_MULT, 2);
    longSpear.getEquipmentHead(1).put(IntegerKey.CRIT_RANGE, 1);
    longSpear.put(ObjectKey.WIELD, context.getReferenceContext().silentlyGetConstructedCDOMObject(WieldCategory.class, "TwoHanded"));
    longSpear.put(ObjectKey.SIZE, mediumRef);
    longSpear.put(ObjectKey.BASESIZE, mediumRef);
    longSpear.put(IntegerKey.REACH, 10);
    GameMode gm = SettingsHandler.getGame();
    RuleCheck rc = new RuleCheck();
    rc.setName("SIZECAT");
    gm.getModeContext().getReferenceContext().importObject(rc);
    SettingsHandler.setRuleCheck("SIZECAT", true);
    gm.setWCStepsFormula("EQUIP.SIZE.INT-PC.SIZE.INT");
    gm.setWeaponReachFormula("(RACEREACH+(max(0,REACH-5)))*REACHMULT");
    wp = new WeaponProf();
    wp.setName("Silly Bite");
    wp.put(StringKey.KEY_NAME, "SillyBite");
    //wp.setTypeInfo("Weapon.Natural.Melee.Finesseable.Bludgeoning.Piercing.Slashing");
    wp.addToListFor(ListKey.TYPE, Type.NATURAL);
    context.getReferenceContext().importObject(wp);
    bite = new Equipment();
    bite.setName("Silly Bite");
    bite.put(StringKey.KEY_NAME, "SillyBite");
    bite.put(StringKey.OUTPUT_NAME, "Silly Bite (For Test)");
    TestHelper.addType(bite, "Weapon.Natural.Melee.Finesseable.Bludgeoning.Piercing.Slashing");
    bite.put(ObjectKey.WEIGHT, BigDecimal.ZERO);
    bite.put(ObjectKey.SIZE, mediumRef);
    bite.put(ObjectKey.BASESIZE, mediumRef);
    aBonus = Bonus.newBonus(context, "WEAPON|ATTACKS|" + 7);
    if (aBonus != null) {
        bite.addToListFor(ListKey.BONUS, aBonus);
    }
    bite.put(IntegerKey.SLOTS, 0);
    bite.setQty(Float.valueOf(1));
    bite.setNumberCarried(1.0f);
    bite.put(ObjectKey.ATTACKS_PROGRESS, false);
    bite.getEquipmentHead(1).put(StringKey.DAMAGE, "1d10");
    bite.getEquipmentHead(1).put(IntegerKey.CRIT_MULT, 2);
    bite.getEquipmentHead(1).put(IntegerKey.CRIT_RANGE, 2);
    bite.put(ObjectKey.WIELD, context.getReferenceContext().silentlyGetConstructedCDOMObject(WieldCategory.class, "OneHanded"));
    bite.put(ObjectKey.WEAPON_PROF, new CDOMDirectSingleRef<>(wp));
    longbow = new Equipment();
    longbow.setName("Longbow");
    TestHelper.addType(longbow, "Weapon.Martial.Ranged.Standard.Piercing.Container.Projectile.Bow.Longbow");
    longbow.put(ObjectKey.TOTAL_CAPACITY, Capacity.ANY);
    longbow.put(ObjectKey.CONTAINER_WEIGHT_CAPACITY, BigDecimal.ONE);
    longbow.addToListFor(ListKey.CAPACITY, new Capacity("Arrow", BigDecimal.ONE));
    longbow.setQty(Float.valueOf(1));
    longbow.setNumberCarried(1.0f);
    arrow = new Equipment();
    arrow.setName("Arrow");
    TestHelper.addType(arrow, "Ammunition.Standard.Arrow.Individual");
    // Weild categories
    WieldCategory twoHanded = context.getReferenceContext().silentlyGetConstructedCDOMObject(WieldCategory.class, "TwoHanded");
    twoHanded.setSizeDifference(1);
    // Equip mods
    EquipmentModifier eqMod = new EquipmentModifier();
    eqMod.setName("Plus 1 Enhancement");
    eqMod.put(StringKey.KEY_NAME, "PLUS1W");
    TestHelper.addType(eqMod, "Ammunition.Weapon");
    eqMod.put(IntegerKey.PLUS, 1);
    eqMod.addToListFor(ListKey.ITEM_TYPES, "Enhancement");
    eqMod.addToListFor(ListKey.ITEM_TYPES, "Magic");
    eqMod.addToListFor(ListKey.ITEM_TYPES, "Plus1");
    aBonus = Bonus.newBonus(context, "WEAPON|DAMAGE,TOHIT|1|TYPE=Enhancement");
    if (aBonus != null) {
        eqMod.addToListFor(ListKey.BONUS, aBonus);
    }
    context.getReferenceContext().importObject(eqMod);
    eqMod = new EquipmentModifier();
    eqMod.setName("Plus 2 Enhancement");
    eqMod.put(StringKey.KEY_NAME, "PLUS2W");
    TestHelper.addType(eqMod, "Ammunition.Weapon");
    eqMod.put(IntegerKey.PLUS, 2);
    eqMod.addToListFor(ListKey.ITEM_TYPES, "Enhancement");
    eqMod.addToListFor(ListKey.ITEM_TYPES, "Magic");
    eqMod.addToListFor(ListKey.ITEM_TYPES, "Plus2");
    aBonus = Bonus.newBonus(context, "WEAPON|DAMAGE,TOHIT|2|TYPE=Enhancement");
    if (aBonus != null) {
        eqMod.addToListFor(ListKey.BONUS, aBonus);
    }
    context.getReferenceContext().importObject(eqMod);
    eqMod = new EquipmentModifier();
    eqMod.setName("Masterwork");
    eqMod.put(StringKey.KEY_NAME, "MWORKW");
    TestHelper.addType(eqMod, "Ammunition.Weapon");
    eqMod.addToListFor(ListKey.ITEM_TYPES, "Masterwork");
    aBonus = Bonus.newBonus(context, "WEAPON|TOHIT|1|TYPE=Enhancement");
    if (aBonus != null) {
        eqMod.addToListFor(ListKey.BONUS, aBonus);
    }
    context.getReferenceContext().importObject(eqMod);
    PCTemplate pct = new PCTemplate();
    context.unconditionallyProcess(pct, "AUTO", "WEAPONPROF|KEY_Sword (Bastard)|KEY_LONGSWORD|SillyBite");
    character.addTemplate(pct);
    wpnBonusPct = new PCTemplate();
    context.unconditionallyProcess(wpnBonusPct, "BONUS", "WEAPONPROF=DoubleWpn|DAMAGE|3");
    context.unconditionallyProcess(wpnBonusPct, "BONUS", "WEAPONPROF=DoubleWpn|TOHIT|4");
    wpnBonusAbility = TestHelper.makeAbility("FEAT_BONUS", AbilityCategory.FEAT, "General");
    context.unconditionallyProcess(wpnBonusAbility, "BONUS", "WEAPONPROF=DoubleWpn|DAMAGE|1");
    context.unconditionallyProcess(wpnBonusAbility, "BONUS", "WEAPONPROF=DoubleWpn|TOHIT|2");
    assertTrue(context.getReferenceContext().resolveReferences(null));
}
Also used : BonusObj(pcgen.core.bonus.BonusObj) EquipSet(pcgen.core.character.EquipSet) WeaponProf(pcgen.core.WeaponProf) PCClass(pcgen.core.PCClass) FixedSizeFormula(pcgen.cdom.formula.FixedSizeFormula) GameMode(pcgen.core.GameMode) EquipmentModifier(pcgen.core.EquipmentModifier) PlayerCharacter(pcgen.core.PlayerCharacter) Equipment(pcgen.core.Equipment) Capacity(pcgen.cdom.helper.Capacity) Race(pcgen.core.Race) LoadContext(pcgen.rules.context.LoadContext) WieldCategory(pcgen.core.character.WieldCategory) RuleCheck(pcgen.core.RuleCheck) PCTemplate(pcgen.core.PCTemplate) SizeAdjustment(pcgen.core.SizeAdjustment)

Example 4 with Capacity

use of pcgen.cdom.helper.Capacity in project pcgen by PCGen.

the class ContainsToken method parseTokenWithSeparator.

@Override
protected ParseResult parseTokenWithSeparator(LoadContext context, Equipment eq, String value) {
    StringTokenizer pipeTok = new StringTokenizer(value, Constants.PIPE);
    context.getObjectContext().removeList(eq, ListKey.CAPACITY);
    String weightCapacity = pipeTok.nextToken();
    boolean hadAsterisk = false;
    if (weightCapacity.charAt(0) == Constants.CHAR_ASTERISK) {
        hadAsterisk = true;
        context.getObjectContext().put(eq, ObjectKey.CONTAINER_CONSTANT_WEIGHT, Boolean.TRUE);
        weightCapacity = weightCapacity.substring(1);
    }
    int percentLoc = weightCapacity.indexOf(Constants.PERCENT);
    if (percentLoc != weightCapacity.lastIndexOf(Constants.PERCENT)) {
        return new ParseResult.Fail("Cannot have two weight reduction " + "characters (indicated by %): " + value, context);
    }
    if (percentLoc != -1) {
        if (hadAsterisk) {
            return new ParseResult.Fail("Cannot have Constant Weight (indicated by *) " + "and weight reduction (indicated by %): " + value, context);
        }
        String redString = weightCapacity.substring(0, percentLoc);
        weightCapacity = weightCapacity.substring(percentLoc + 1);
        try {
            context.getObjectContext().put(eq, IntegerKey.CONTAINER_REDUCE_WEIGHT, Integer.valueOf(redString));
        } catch (NumberFormatException ex) {
            return new ParseResult.Fail("Weight Reduction (indicated by %) must be an integer: " + value, context);
        }
    }
    BigDecimal weightCap;
    if ("UNLIM".equals(weightCapacity)) {
        weightCap = Capacity.UNLIMITED;
    } else {
        try {
            weightCap = BigDecimalHelper.trimBigDecimal(new BigDecimal(weightCapacity));
            if (BigDecimal.ZERO.compareTo(weightCap) > 0) {
                return new ParseResult.Fail("Weight Capacity must be >= 0: " + weightCapacity + "\n  Use 'UNLIM' (not -1) for unlimited Count", context);
            }
        } catch (NumberFormatException ex) {
            return new ParseResult.Fail("Weight Capacity must be 'UNLIM or a number >= 0: " + weightCapacity, context);
        }
    }
    context.getObjectContext().put(eq, ObjectKey.CONTAINER_WEIGHT_CAPACITY, weightCap);
    Capacity totalCap = null;
    boolean limited = true;
    if (!pipeTok.hasMoreTokens()) {
        limited = false;
        totalCap = Capacity.ANY;
    }
    BigDecimal limitedCapacity = BigDecimal.ZERO;
    while (pipeTok.hasMoreTokens()) {
        String typeString = pipeTok.nextToken();
        int equalLoc = typeString.indexOf(Constants.EQUALS);
        if (equalLoc != typeString.lastIndexOf(Constants.EQUALS)) {
            return new ParseResult.Fail("Two many = signs", context);
        }
        if (equalLoc == -1) {
            limited = false;
            context.getObjectContext().addToList(eq, ListKey.CAPACITY, new Capacity(typeString, Capacity.UNLIMITED));
        } else {
            String itemType = typeString.substring(0, equalLoc);
            String itemNumString = typeString.substring(equalLoc + 1);
            BigDecimal itemNumber;
            if ("UNLIM".equals(itemNumString)) {
                limited = false;
                itemNumber = Capacity.UNLIMITED;
            } else {
                try {
                    itemNumber = BigDecimalHelper.trimBigDecimal(new BigDecimal(itemNumString));
                } catch (NumberFormatException ex) {
                    return new ParseResult.Fail("Item Number for " + itemType + " must be 'UNLIM' or a number > 0: " + itemNumString, context);
                }
                if (BigDecimal.ZERO.compareTo(itemNumber) >= 0) {
                    return new ParseResult.Fail("Cannot have negative quantity of " + itemType + ": " + value, context);
                }
            }
            if (limited) {
                limitedCapacity = limitedCapacity.add(itemNumber);
            }
            context.getObjectContext().addToList(eq, ListKey.CAPACITY, new Capacity(itemType, itemNumber));
        }
    }
    if (totalCap == null) {
        BigDecimal totalCapacity = limited ? limitedCapacity : Capacity.UNLIMITED;
        totalCap = Capacity.getTotalCapacity(totalCapacity);
    }
    context.getObjectContext().put(eq, ObjectKey.TOTAL_CAPACITY, totalCap);
    return ParseResult.SUCCESS;
}
Also used : StringTokenizer(java.util.StringTokenizer) ParseResult(pcgen.rules.persistence.token.ParseResult) Capacity(pcgen.cdom.helper.Capacity) BigDecimal(java.math.BigDecimal)

Example 5 with Capacity

use of pcgen.cdom.helper.Capacity in project pcgen by PCGen.

the class ContainsToken method unparse.

@Override
public String[] unparse(LoadContext context, Equipment eq) {
    Changes<Capacity> changes = context.getObjectContext().getListChanges(eq, ListKey.CAPACITY);
    Capacity totalCapacity = context.getObjectContext().getObject(eq, ObjectKey.TOTAL_CAPACITY);
    if (totalCapacity == null && (changes == null || changes.isEmpty())) {
        return null;
    }
    StringBuilder sb = new StringBuilder();
    Boolean b = context.getObjectContext().getObject(eq, ObjectKey.CONTAINER_CONSTANT_WEIGHT);
    if (b != null && b.booleanValue()) {
        sb.append(Constants.CHAR_ASTERISK);
    }
    Integer reducePercent = context.getObjectContext().getInteger(eq, IntegerKey.CONTAINER_REDUCE_WEIGHT);
    if (reducePercent != null) {
        sb.append(reducePercent).append(Constants.PERCENT);
    }
    BigDecimal cap = context.getObjectContext().getObject(eq, ObjectKey.CONTAINER_WEIGHT_CAPACITY);
    if (cap == null) {
        // CONSIDER ERROR??
        return null;
    }
    if (Capacity.UNLIMITED.equals(cap)) {
        sb.append("UNLIM");
    } else {
        sb.append(cap);
    }
    Collection<Capacity> capacityList = changes.getAdded();
    if (capacityList == null) {
        if (Capacity.UNLIMITED.equals(totalCapacity.getCapacity())) {
            // Special Case: Nothing additional
            return new String[] { sb.toString() };
        }
    }
    BigDecimal limitedCapacity = BigDecimal.ZERO;
    boolean limited = true;
    for (Capacity c : capacityList) {
        String capType = c.getType();
        sb.append(Constants.PIPE);
        BigDecimal thisCap = c.getCapacity();
        sb.append(capType);
        if (Capacity.UNLIMITED.equals(thisCap)) {
            limited = false;
        } else {
            if (limited) {
                limitedCapacity = limitedCapacity.add(thisCap);
            }
            sb.append(Constants.EQUALS).append(thisCap);
        }
    }
    if (!limitedCapacity.equals(totalCapacity.getCapacity()) && !Capacity.UNLIMITED.equals(totalCapacity.getCapacity())) {
        // Need to write out total
        sb.append("Total").append(Constants.EQUALS).append(totalCapacity.getCapacity());
    }
    return new String[] { sb.toString() };
}
Also used : Capacity(pcgen.cdom.helper.Capacity) BigDecimal(java.math.BigDecimal)

Aggregations

Capacity (pcgen.cdom.helper.Capacity)6 BigDecimal (java.math.BigDecimal)5 StringTokenizer (java.util.StringTokenizer)1 FixedSizeFormula (pcgen.cdom.formula.FixedSizeFormula)1 Equipment (pcgen.core.Equipment)1 EquipmentModifier (pcgen.core.EquipmentModifier)1 GameMode (pcgen.core.GameMode)1 PCClass (pcgen.core.PCClass)1 PCTemplate (pcgen.core.PCTemplate)1 PlayerCharacter (pcgen.core.PlayerCharacter)1 Race (pcgen.core.Race)1 RuleCheck (pcgen.core.RuleCheck)1 SizeAdjustment (pcgen.core.SizeAdjustment)1 WeaponProf (pcgen.core.WeaponProf)1 BonusObj (pcgen.core.bonus.BonusObj)1 EquipSet (pcgen.core.character.EquipSet)1 WieldCategory (pcgen.core.character.WieldCategory)1 Prerequisite (pcgen.core.prereq.Prerequisite)1 AbstractReferenceContext (pcgen.rules.context.AbstractReferenceContext)1 LoadContext (pcgen.rules.context.LoadContext)1