Search in sources :

Example 56 with UnitAttachment

use of games.strategy.triplea.attachments.UnitAttachment in project triplea by triplea-game.

the class AiUtils method strength.

/**
 * Get a quick and dirty estimate of the strength of some units in a battle.
 *
 * @param units
 *        - the units to measure
 * @param attacking
 *        - are the units on attack or defense
 * @param sea
 *        - calculate the strength of the units in a sea or land battle?
 */
public static float strength(final Collection<Unit> units, final boolean attacking, final boolean sea) {
    float strength = 0;
    for (final Unit u : units) {
        final UnitAttachment unitAttachment = UnitAttachment.get(u.getType());
        if (unitAttachment.getIsInfrastructure()) {
        // nothing
        } else if (unitAttachment.getIsSea() == sea) {
            // 2 points since we can absorb a hit
            strength += 2;
            // two hit
            strength += 1.5f * unitAttachment.getHitPoints();
            // the number of pips on the dice
            if (attacking) {
                strength += unitAttachment.getAttack(u.getOwner());
            } else {
                strength += unitAttachment.getDefense(u.getOwner());
            }
            if (attacking) {
                // we dont want transports to try and gang up on subs
                if (unitAttachment.getAttack(u.getOwner()) == 0) {
                    strength -= 1.2f;
                }
            }
        }
    }
    if (attacking) {
        final int art = CollectionUtils.countMatches(units, Matches.unitIsArtillery());
        final int artSupport = CollectionUtils.countMatches(units, Matches.unitIsArtillerySupportable());
        strength += Math.min(art, artSupport);
    }
    return strength;
}
Also used : UnitAttachment(games.strategy.triplea.attachments.UnitAttachment) Unit(games.strategy.engine.data.Unit)

Example 57 with UnitAttachment

use of games.strategy.triplea.attachments.UnitAttachment in project triplea by triplea-game.

the class Route method getFuelCostsAndIfChargedFlatFuelCost.

private Tuple<ResourceCollection, Boolean> getFuelCostsAndIfChargedFlatFuelCost(final Unit unit, final GameData data, final boolean ignoreFlat) {
    final ResourceCollection resources = new ResourceCollection(data);
    boolean chargedFlatFuelCost = false;
    if (Matches.unitIsBeingTransported().test(unit)) {
        return Tuple.of(resources, chargedFlatFuelCost);
    }
    final UnitAttachment ua = UnitAttachment.get(unit.getType());
    resources.add(ua.getFuelCost());
    resources.multiply(getMovementCost(unit));
    if (!ignoreFlat && Matches.unitHasNotBeenChargedFlatFuelCost().test(unit)) {
        resources.add(ua.getFuelFlatCost());
        chargedFlatFuelCost = true;
    }
    return Tuple.of(resources, chargedFlatFuelCost);
}
Also used : UnitAttachment(games.strategy.triplea.attachments.UnitAttachment)

Example 58 with UnitAttachment

use of games.strategy.triplea.attachments.UnitAttachment in project triplea by triplea-game.

the class TabbedProductionPanel method getDefaultRuleLists.

private List<Tuple<String, List<Rule>>> getDefaultRuleLists() {
    final List<Tuple<String, List<Rule>>> ruleLists = new ArrayList<>();
    final ArrayList<Rule> allRules = new ArrayList<>();
    final ArrayList<Rule> landRules = new ArrayList<>();
    final ArrayList<Rule> airRules = new ArrayList<>();
    final ArrayList<Rule> seaRules = new ArrayList<>();
    final ArrayList<Rule> constructRules = new ArrayList<>();
    final ArrayList<Rule> upgradeConsumesRules = new ArrayList<>();
    final ArrayList<Rule> resourceRules = new ArrayList<>();
    for (final Rule rule : rules) {
        allRules.add(rule);
        final NamedAttachable resourceOrUnit = rule.getProductionRule().getResults().keySet().iterator().next();
        if (resourceOrUnit instanceof UnitType) {
            final UnitType type = (UnitType) resourceOrUnit;
            final UnitAttachment attach = UnitAttachment.get(type);
            if (attach.getConsumesUnits() != null && attach.getConsumesUnits().totalValues() >= 1) {
                upgradeConsumesRules.add(rule);
            }
            // anywhere (placed without needing a factory).
            if (attach.getIsConstruction()) {
                constructRules.add(rule);
            } else if (attach.getIsSea()) {
                seaRules.add(rule);
            } else if (attach.getIsAir()) {
                airRules.add(rule);
            } else {
                landRules.add(rule);
            }
        } else if (resourceOrUnit instanceof Resource) {
            resourceRules.add(rule);
        }
    }
    ruleLists.add(Tuple.of("All", allRules));
    ruleLists.add(Tuple.of("Land", landRules));
    ruleLists.add(Tuple.of("Air", airRules));
    ruleLists.add(Tuple.of("Sea", seaRules));
    ruleLists.add(Tuple.of("Construction", constructRules));
    ruleLists.add(Tuple.of("Upgrades/Consumes", upgradeConsumesRules));
    ruleLists.add(Tuple.of("Resources", resourceRules));
    return ruleLists;
}
Also used : UnitAttachment(games.strategy.triplea.attachments.UnitAttachment) NamedAttachable(games.strategy.engine.data.NamedAttachable) UnitType(games.strategy.engine.data.UnitType) ArrayList(java.util.ArrayList) Resource(games.strategy.engine.data.Resource) ProductionRule(games.strategy.engine.data.ProductionRule) Tuple(games.strategy.util.Tuple)

Example 59 with UnitAttachment

use of games.strategy.triplea.attachments.UnitAttachment in project triplea by triplea-game.

the class TripleAFrame method getArrowKeyListener.

private KeyListener getArrowKeyListener() {
    return new KeyListener() {

        @Override
        public void keyPressed(final KeyEvent e) {
            isCtrlPressed = e.isControlDown();
            // scroll map according to wasd/arrowkeys
            final int diffPixel = computeScrollSpeed();
            final int x = mapPanel.getXOffset();
            final int y = mapPanel.getYOffset();
            final int keyCode = e.getKeyCode();
            if (keyCode == KeyEvent.VK_RIGHT) {
                getMapPanel().setTopLeft(x + diffPixel, y);
            } else if (keyCode == KeyEvent.VK_LEFT) {
                getMapPanel().setTopLeft(x - diffPixel, y);
            } else if (keyCode == KeyEvent.VK_DOWN) {
                getMapPanel().setTopLeft(x, y + diffPixel);
            } else if (keyCode == KeyEvent.VK_UP) {
                getMapPanel().setTopLeft(x, y - diffPixel);
            }
            // I for info
            if (keyCode == KeyEvent.VK_I || keyCode == KeyEvent.VK_V) {
                String unitInfo = "";
                if (unitsBeingMousedOver != null && !unitsBeingMousedOver.isEmpty()) {
                    final Unit unit = unitsBeingMousedOver.get(0);
                    final UnitAttachment ua = UnitAttachment.get(unit.getType());
                    if (ua != null) {
                        unitInfo = "<b>Unit:</b><br>" + unit.getType().getName() + ": " + ua.toStringShortAndOnlyImportantDifferences(unit.getOwner(), true, false);
                    }
                }
                String terrInfo = "";
                if (territoryLastEntered != null) {
                    final TerritoryAttachment ta = TerritoryAttachment.get(territoryLastEntered);
                    if (ta != null) {
                        terrInfo = "<b>Territory:</b><br>" + ta.toStringForInfo(true, true) + "<br>";
                    } else {
                        terrInfo = "<b>Territory:</b><br>" + territoryLastEntered.getName() + "<br>Water Territory";
                    }
                }
                String tipText = unitInfo;
                if (unitInfo.length() > 0 && terrInfo.length() > 0) {
                    tipText = tipText + "<br><br><br><br><br>";
                }
                tipText = tipText + terrInfo;
                if (tipText.length() > 0) {
                    final Point currentPoint = MouseInfo.getPointerInfo().getLocation();
                    final PopupFactory popupFactory = PopupFactory.getSharedInstance();
                    final JToolTip info = new JToolTip();
                    info.setTipText("<html>" + tipText + "</html>");
                    final Popup popup = popupFactory.getPopup(mapPanel, info, currentPoint.x, currentPoint.y);
                    popup.show();
                    new Thread(() -> {
                        Interruptibles.sleep(5000);
                        popup.hide();
                    }, "popup waiter").start();
                }
            }
        }

        @Override
        public void keyTyped(final KeyEvent e) {
        }

        @Override
        public void keyReleased(final KeyEvent e) {
            isCtrlPressed = e.isControlDown();
        }
    };
}
Also used : KeyEvent(java.awt.event.KeyEvent) UnitAttachment(games.strategy.triplea.attachments.UnitAttachment) PopupFactory(javax.swing.PopupFactory) JToolTip(javax.swing.JToolTip) TerritoryAttachment(games.strategy.triplea.attachments.TerritoryAttachment) Popup(javax.swing.Popup) KeyListener(java.awt.event.KeyListener) Point(java.awt.Point) Unit(games.strategy.engine.data.Unit) TripleAUnit(games.strategy.triplea.TripleAUnit) Point(java.awt.Point)

Example 60 with UnitAttachment

use of games.strategy.triplea.attachments.UnitAttachment in project triplea by triplea-game.

the class TuvUtils method getTotalTuv.

private static int getTotalTuv(final UnitType unitType, final IntegerMap<UnitType> costs, final Set<UnitType> alreadyAdded) {
    final UnitAttachment ua = UnitAttachment.get(unitType);
    if (ua != null && ua.getTuv() > 0) {
        return ua.getTuv();
    }
    int tuv = costs.getInt(unitType);
    if (ua == null || ua.getConsumesUnits().isEmpty() || alreadyAdded.contains(unitType)) {
        return tuv;
    }
    alreadyAdded.add(unitType);
    for (final UnitType ut : ua.getConsumesUnits().keySet()) {
        tuv += ua.getConsumesUnits().getInt(ut) * getTotalTuv(ut, costs, alreadyAdded);
    }
    alreadyAdded.remove(unitType);
    return tuv;
}
Also used : UnitAttachment(games.strategy.triplea.attachments.UnitAttachment) UnitType(games.strategy.engine.data.UnitType)

Aggregations

UnitAttachment (games.strategy.triplea.attachments.UnitAttachment)60 Unit (games.strategy.engine.data.Unit)45 ArrayList (java.util.ArrayList)25 TripleAUnit (games.strategy.triplea.TripleAUnit)23 UnitType (games.strategy.engine.data.UnitType)20 GameData (games.strategy.engine.data.GameData)19 Territory (games.strategy.engine.data.Territory)19 PlayerID (games.strategy.engine.data.PlayerID)12 CompositeChange (games.strategy.engine.data.CompositeChange)8 TerritoryAttachment (games.strategy.triplea.attachments.TerritoryAttachment)7 IntegerMap (games.strategy.util.IntegerMap)6 Collection (java.util.Collection)6 Set (java.util.Set)6 Change (games.strategy.engine.data.Change)5 Resource (games.strategy.engine.data.Resource)5 Tuple (games.strategy.util.Tuple)5 HashSet (java.util.HashSet)5 ITestDelegateBridge (games.strategy.engine.data.ITestDelegateBridge)4 Route (games.strategy.engine.data.Route)4 List (java.util.List)4