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;
}
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);
}
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;
}
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();
}
};
}
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;
}
Aggregations