Search in sources :

Example 1 with EqModRef

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

the class KitGear method testApply.

@Override
public boolean testApply(Kit aKit, PlayerCharacter aPC, List<String> warnings) {
    actingQuantity = quantity;
    actingCost = maxCost;
    actingMods = mods == null ? null : new ArrayList<>(mods);
    actingLocation = theLocationStr;
    if (size != null) {
        actingSize = size.get();
    }
    theEquipment = null;
    theQty = 0;
    theLocation = "";
    theCost = BigDecimal.ZERO;
    processLookups(aKit, aPC);
    int aBuyRate = aKit.getBuyRate(aPC);
    final BigDecimal pcGold = aPC.getGold();
    final BigDecimal fixedTotalCost = aKit.getTotalCost(aPC);
    if (fixedTotalCost != null) {
        // We are going to charge fr the kit once, rather than for every piece of gear
        aBuyRate = 0;
    }
    List<Equipment> eqList = new ArrayList<>(equip.getContainedObjects());
    if (actingCost != null) {
        final BigDecimal bdMaxCost = new BigDecimal(Integer.toString(actingCost));
        for (Iterator<Equipment> i = eqList.iterator(); i.hasNext(); ) {
            if (i.next().getCost(aPC).compareTo(bdMaxCost) > 0) {
                i.remove();
            }
        }
    }
    if (eqList.size() == 1) {
        theEquipment = eqList.get(0);
    } else {
        List<Equipment> selected = new ArrayList<>(1);
        selected = Globals.getChoiceFromList("Choose equipment", eqList, selected, 1, aPC);
        if (selected.size() == 1) {
            theEquipment = selected.get(0);
        }
    }
    //
    // TODO: Check to see if the user has selected an item that
    // requires modification (MOD:R)
    theEquipment = theEquipment.clone();
    //
    // Resize item for character--never resize weapons or ammo, unless it's a
    // natural (weapon)
    boolean tryResize = false;
    SizeAdjustment sizeToSet = aPC.getSizeAdjustment();
    if (actingSize == null) {
        if (theEquipment.isType("Natural") || (sizeToPC != null && sizeToPC) || (!theEquipment.isWeapon() && !theEquipment.isAmmunition())) {
            tryResize = Globals.canResizeHaveEffect(theEquipment, null);
        }
    } else {
        if (sizeToPC != null && sizeToPC) {
            tryResize = Globals.canResizeHaveEffect(theEquipment, null);
        } else {
            sizeToSet = actingSize;
            tryResize = true;
        }
    }
    if (tryResize) {
        theEquipment.resizeItem(aPC, sizeToSet);
    } else {
        // We need setBase() called.  The only way to do that is to resize.
        // We will set the size to itself.
        theEquipment.resizeItem(aPC, theEquipment.getSafe(ObjectKey.SIZE).get());
    }
    //
    if (actingMods != null) {
        for (EqModRef modref : actingMods) {
            /*
				 * Going to do this the long way for now to avoid ugly entanglements
				 */
            StringBuilder sb = new StringBuilder(50);
            EquipmentModifier eqMod = modref.getRef().get();
            sb.append(eqMod.getKeyName());
            for (String assoc : modref.getChoices()) {
                sb.append(Constants.PIPE).append(eval(aPC, assoc));
            }
            theEquipment.addEqModifiers(sb.toString(), true);
        }
    }
    if (tryResize || (actingMods != null)) {
        theEquipment.nameItemFromModifiers(aPC);
    }
    if (actingQuantity == null) {
        theQty = 1;
    } else {
        theQty = actingQuantity.resolve(aPC, "").intValue();
    }
    int origQty = theQty;
    final BigDecimal eqCost = theEquipment.getCost(aPC);
    if (aBuyRate != 0) {
        if (fixedTotalCost == null) {
            final BigDecimal bdBuyRate = new BigDecimal(Integer.toString(aBuyRate)).multiply(new BigDecimal("0.01"));
            // Check to see if the PC can afford to buy this equipment. If
            // not, then decrement the quantity and try again.
            theCost = eqCost.multiply(new BigDecimal(Integer.toString(theQty))).multiply(bdBuyRate);
            while (theQty > 0) {
                if (// PC has enough?
                theCost.compareTo(pcGold) <= 0) {
                    break;
                }
                theCost = eqCost.multiply(new BigDecimal(Integer.toString(--theQty))).multiply(bdBuyRate);
            }
        }
        aPC.setGold(aPC.getGold().subtract(theCost));
    }
    boolean outOfFunds = false;
    if (theQty != origQty) {
        outOfFunds = true;
    }
    if (outOfFunds) {
        warnings.add("GEAR: Could not purchase " + (origQty - theQty) + " " + theEquipment.getName() + ". Not enough funds.");
    }
    //
    if (theQty == 0) {
        return false;
    }
    Equipment testApplyEquipment = theEquipment.clone();
    // Temporarily add the equipment so we can see if we can equip it.
    testApplyEquipment.setQty(new Float(theQty));
    aPC.addEquipment(testApplyEquipment);
    Equipment theTarget = null;
    if (actingLocation != null) {
        theLocation = actingLocation;
        if (!theLocation.equalsIgnoreCase("DEFAULT") && !theLocation.equalsIgnoreCase(Constants.EQUIP_LOCATION_CARRIED) && !theLocation.equalsIgnoreCase(Constants.EQUIP_LOCATION_NOTCARRIED) && !theLocation.equalsIgnoreCase(Constants.EQUIP_LOCATION_EQUIPPED)) {
            theTarget = EquipmentUtilities.findEquipmentByBaseKey(aPC.getEquipmentMasterList(), theLocation);
        } else if (theLocation.equalsIgnoreCase("DEFAULT")) {
            theLocation = "";
        }
        EquipSet eSet = null;
        if (theTarget != null) {
            eSet = aPC.getEquipSetForItem(aPC.getEquipSetByIdPath(EquipSet.DEFAULT_SET_PATH), theTarget);
        }
        if (eSet == null) {
            eSet = aPC.getEquipSetByIdPath(EquipSet.DEFAULT_SET_PATH);
        }
        if (eSet == null) {
            warnings.add("GEAR: Could not find location " + theLocation + " for gear " + testApplyEquipment.getName() + ".");
            return false;
        } else {
            EquipSet eqSet = aPC.addEquipToTarget(eSet, theTarget, theLocation, testApplyEquipment, new Float(-1.0f));
            if (eqSet == null) {
                warnings.add("GEAR: Could not equip " + testApplyEquipment.getName() + " to " + theLocation);
            }
        }
    }
    return true;
}
Also used : EquipSet(pcgen.core.character.EquipSet) ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal) EquipmentModifier(pcgen.core.EquipmentModifier) Equipment(pcgen.core.Equipment) EqModRef(pcgen.cdom.helper.EqModRef) SizeAdjustment(pcgen.core.SizeAdjustment)

Example 2 with EqModRef

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

the class EqmodToken method unparse.

@Override
public String[] unparse(LoadContext context, KitGear kitGear) {
    if (!kitGear.hasEqMods()) {
        return null;
    }
    Set<String> set = new TreeSet<>();
    for (EqModRef modRef : kitGear.getEqMods()) {
        String key = modRef.getRef().getLSTformat(false);
        StringBuilder sb = new StringBuilder();
        sb.append(key);
        for (String s : modRef.getChoices()) {
            sb.append(Constants.PIPE).append(s);
        }
        set.add(sb.toString());
    }
    return new String[] { StringUtil.join(set, Constants.DOT) };
}
Also used : TreeSet(java.util.TreeSet) EqModRef(pcgen.cdom.helper.EqModRef)

Example 3 with EqModRef

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

the class EqmodToken method parseTokenWithSeparator.

@Override
protected ParseResult parseTokenWithSeparator(LoadContext context, KitGear kitGear, String value) {
    StringTokenizer dotTok = new StringTokenizer(value, Constants.DOT);
    while (dotTok.hasMoreTokens()) {
        String aEqModName = dotTok.nextToken();
        if (aEqModName.equalsIgnoreCase(Constants.LST_NONE)) {
            return new ParseResult.Fail("Embedded " + Constants.LST_NONE + " is prohibited in " + getTokenName(), context);
        }
        ParseResult pr = checkForIllegalSeparator('|', aEqModName);
        if (!pr.passed()) {
            return pr;
        }
        StringTokenizer pipeTok = new StringTokenizer(aEqModName, Constants.PIPE);
        // The type of EqMod, eg: ABILITYPLUS
        final String eqModKey = pipeTok.nextToken();
        CDOMSingleRef<EquipmentModifier> eqMod = context.getReferenceContext().getCDOMReference(EQUIPMENT_MODIFIER_CLASS, eqModKey);
        EqModRef modRef = new EqModRef(eqMod);
        while (pipeTok.hasMoreTokens()) {
            String assocTok = pipeTok.nextToken();
            if (assocTok.indexOf(']') != -1) {
                if (assocTok.indexOf("[]") != -1) {
                    return new ParseResult.Fail("Found empty assocation in " + getTokenName() + ": " + value, context);
                }
                StringTokenizer bracketTok = new StringTokenizer(assocTok, "]");
                while (bracketTok.hasMoreTokens()) {
                    String assoc = bracketTok.nextToken();
                    int openBracketLoc = assoc.indexOf('[');
                    if (openBracketLoc == -1) {
                        return new ParseResult.Fail("Found close bracket without open bracket " + "in assocation in " + getTokenName() + ": " + value, context);
                    }
                    if (openBracketLoc != assoc.lastIndexOf('[')) {
                        return new ParseResult.Fail("Found open bracket without close bracket " + "in assocation in " + getTokenName() + ": " + value, context);
                    }
                }
            }
            modRef.addChoice(assocTok);
        }
        kitGear.addModRef(modRef);
    }
    return ParseResult.SUCCESS;
}
Also used : StringTokenizer(java.util.StringTokenizer) EquipmentModifier(pcgen.core.EquipmentModifier) ParseResult(pcgen.rules.persistence.token.ParseResult) EqModRef(pcgen.cdom.helper.EqModRef)

Example 4 with EqModRef

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

the class KitGear method toString.

@Override
public String toString() {
    final StringBuilder info = new StringBuilder(100);
    if (quantity != null) {
        String qtyStr = String.valueOf(quantity);
        if (!"1".equals(qtyStr)) {
            info.append(quantity).append('x');
        }
    }
    info.append(equip == null ? "null" : equip.getLSTformat(false));
    if (mods != null) {
        info.append(" (");
        boolean needsSlash = false;
        for (EqModRef modRef : mods) {
            if (needsSlash) {
                info.append('/');
            }
            needsSlash = true;
            info.append(modRef.getRef().getLSTformat(false));
            for (String s : modRef.getChoices()) {
                info.append(Constants.PIPE).append(s);
            }
        }
        info.append(')');
    }
    return info.toString();
}
Also used : EqModRef(pcgen.cdom.helper.EqModRef)

Example 5 with EqModRef

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

the class EqmodToken method unparse.

@Override
public String[] unparse(LoadContext context, Equipment eq) {
    AbstractObjectContext obj = context.getObjectContext();
    String damage = obj.getString(eq, StringKey.DAMAGE_OVERRIDE);
    Set<String> set = new TreeSet<>();
    if (damage != null) {
        set.add(EQMOD_DAMAGE + Constants.PIPE + damage);
    }
    BigDecimal weight = obj.getObject(eq, ObjectKey.WEIGHT_MOD);
    if (weight != null) {
        set.add(EQMOD_WEIGHT + Constants.PIPE + weight.toString().replace('.', ','));
    }
    EquipmentHead head = eq.getEquipmentHeadReference(1);
    if (head != null) {
        Changes<EqModRef> changes = obj.getListChanges(head, ListKey.EQMOD_INFO);
        Collection<EqModRef> added = changes.getAdded();
        if (added != null) {
            for (EqModRef modRef : added) {
                String key = modRef.getRef().getLSTformat(false);
                StringBuilder sb = new StringBuilder();
                sb.append(key);
                for (String s : modRef.getChoices()) {
                    sb.append(Constants.PIPE);
                    sb.append(s.replace('|', '='));
                }
                set.add(sb.toString());
            }
        }
    }
    if (set.isEmpty()) {
        return null;
    }
    return new String[] { StringUtil.join(set, Constants.DOT) };
}
Also used : EquipmentHead(pcgen.cdom.inst.EquipmentHead) AbstractObjectContext(pcgen.rules.context.AbstractObjectContext) TreeSet(java.util.TreeSet) EqModRef(pcgen.cdom.helper.EqModRef) BigDecimal(java.math.BigDecimal)

Aggregations

EqModRef (pcgen.cdom.helper.EqModRef)9 BigDecimal (java.math.BigDecimal)5 EquipmentHead (pcgen.cdom.inst.EquipmentHead)5 EquipmentModifier (pcgen.core.EquipmentModifier)5 StringTokenizer (java.util.StringTokenizer)3 TreeSet (java.util.TreeSet)3 ParseResult (pcgen.rules.persistence.token.ParseResult)3 AbstractObjectContext (pcgen.rules.context.AbstractObjectContext)2 ArrayList (java.util.ArrayList)1 Equipment (pcgen.core.Equipment)1 SizeAdjustment (pcgen.core.SizeAdjustment)1 EquipSet (pcgen.core.character.EquipSet)1