Search in sources :

Example 61 with IntegerMap

use of games.strategy.util.IntegerMap in project triplea by triplea-game.

the class PlayerUnitsPanel method init.

/**
 * Sets up components to an initial state.
 */
public void init(final PlayerID id, final List<Unit> units, final boolean land) {
    isLand = land;
    categories = new ArrayList<>(categorize(id, units));
    categories.sort(Comparator.comparing(UnitCategory::getType, (ut1, ut2) -> {
        final UnitAttachment u1 = UnitAttachment.get(ut1);
        final UnitAttachment u2 = UnitAttachment.get(ut2);
        // For land battles, sort by land, air, can't combat move (AA), bombarding
        if (land) {
            if (u1.getIsSea() != u2.getIsSea()) {
                return u1.getIsSea() ? 1 : -1;
            }
            final boolean u1CanNotCombatMove = Matches.unitTypeCanNotMoveDuringCombatMove().test(ut1) || !Matches.unitTypeCanMove(id).test(ut1);
            final boolean u2CanNotCombatMove = Matches.unitTypeCanNotMoveDuringCombatMove().test(ut2) || !Matches.unitTypeCanMove(id).test(ut2);
            if (u1CanNotCombatMove != u2CanNotCombatMove) {
                return u1CanNotCombatMove ? 1 : -1;
            }
            if (u1.getIsAir() != u2.getIsAir()) {
                return u1.getIsAir() ? 1 : -1;
            }
        } else {
            if (u1.getIsSea() != u2.getIsSea()) {
                return u1.getIsSea() ? -1 : 1;
            }
        }
        return u1.getName().compareTo(u2.getName());
    }));
    removeAll();
    final Predicate<UnitType> predicate;
    if (land) {
        if (defender) {
            predicate = Matches.unitTypeIsNotSea();
        } else {
            predicate = Matches.unitTypeIsNotSea().or(Matches.unitTypeCanBombard(id));
        }
    } else {
        predicate = Matches.unitTypeIsSeaOrAir();
    }
    final IntegerMap<UnitType> costs;
    try {
        data.acquireReadLock();
        costs = TuvUtils.getCostsForTuv(id, data);
    } finally {
        data.releaseReadLock();
    }
    for (final UnitCategory category : categories) {
        if (predicate.test(category.getType())) {
            final UnitPanel upanel = new UnitPanel(uiContext, category, costs);
            upanel.addChangeListener(this::notifyListeners);
            add(upanel);
        }
    }
    // TODO: probably do not need to do this much revalidation.
    invalidate();
    validate();
    revalidate();
    getParent().invalidate();
}
Also used : UnitAttachment(games.strategy.triplea.attachments.UnitAttachment) NamedAttachable(games.strategy.engine.data.NamedAttachable) TuvUtils(games.strategy.triplea.util.TuvUtils) UnitSeperator(games.strategy.triplea.util.UnitSeperator) ArrayList(java.util.ArrayList) UnitCategory(games.strategy.triplea.util.UnitCategory) UiContext(games.strategy.triplea.ui.UiContext) UnitType(games.strategy.engine.data.UnitType) LinkedHashSet(java.util.LinkedHashSet) BoxLayout(javax.swing.BoxLayout) CollectionUtils(games.strategy.util.CollectionUtils) IntegerMap(games.strategy.util.IntegerMap) Unit(games.strategy.engine.data.Unit) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) Territory(games.strategy.engine.data.Territory) Component(java.awt.Component) GameData(games.strategy.engine.data.GameData) List(java.util.List) PlayerID(games.strategy.engine.data.PlayerID) ProductionFrontier(games.strategy.engine.data.ProductionFrontier) Matches(games.strategy.triplea.delegate.Matches) ProductionRule(games.strategy.engine.data.ProductionRule) Comparator(java.util.Comparator) JPanel(javax.swing.JPanel) UnitAttachment(games.strategy.triplea.attachments.UnitAttachment) UnitType(games.strategy.engine.data.UnitType) UnitCategory(games.strategy.triplea.util.UnitCategory)

Example 62 with IntegerMap

use of games.strategy.util.IntegerMap in project triplea by triplea-game.

the class MyFormatter method unitsToTextNoOwner.

public static String unitsToTextNoOwner(final Collection<Unit> units, final PlayerID owner) {
    final IntegerMap<UnitType> map = new IntegerMap<>();
    for (final Unit unit : units) {
        if (owner == null || owner.equals(unit.getOwner())) {
            map.add(unit.getType(), 1);
        }
    }
    final StringBuilder buf = new StringBuilder();
    // sort on unit name
    final List<UnitType> sortedList = new ArrayList<>(map.keySet());
    sortedList.sort(Comparator.comparing(UnitType::getName));
    int count = map.keySet().size();
    for (final UnitType type : sortedList) {
        final int quantity = map.getInt(type);
        buf.append(quantity);
        buf.append(" ");
        buf.append(quantity > 1 ? pluralize(type.getName()) : type.getName());
        count--;
        if (count > 1) {
            buf.append(", ");
        }
        if (count == 1) {
            buf.append(" and ");
        }
    }
    return buf.toString();
}
Also used : IntegerMap(games.strategy.util.IntegerMap) UnitType(games.strategy.engine.data.UnitType) ArrayList(java.util.ArrayList) Unit(games.strategy.engine.data.Unit)

Example 63 with IntegerMap

use of games.strategy.util.IntegerMap in project triplea by triplea-game.

the class MyFormatter method defaultNamedToTextList.

public static String defaultNamedToTextList(final Collection<? extends DefaultNamed> list, final String seperator, final boolean showQuantity) {
    final IntegerMap<DefaultNamed> map = new IntegerMap<>();
    for (final DefaultNamed unit : list) {
        if (unit == null || unit.getName() == null) {
            throw new IllegalStateException("Unit or Resource no longer exists?!?");
        }
        map.add(unit, 1);
    }
    final StringBuilder buf = new StringBuilder();
    // sort on unit name
    final List<DefaultNamed> sortedList = new ArrayList<>(map.keySet());
    sortedList.sort(Comparator.comparing(DefaultNamed::getName));
    int count = map.keySet().size();
    for (final DefaultNamed type : sortedList) {
        if (showQuantity) {
            final int quantity = map.getInt(type);
            buf.append(quantity);
            buf.append(" ");
            buf.append(quantity > 1 ? pluralize(type.getName()) : type.getName());
        } else {
            buf.append(type.getName());
        }
        count--;
        if (count > 1) {
            buf.append(seperator);
        }
        if (count == 1) {
            buf.append(" and ");
        }
    }
    return buf.toString();
}
Also used : IntegerMap(games.strategy.util.IntegerMap) ArrayList(java.util.ArrayList) DefaultNamed(games.strategy.engine.data.DefaultNamed)

Example 64 with IntegerMap

use of games.strategy.util.IntegerMap in project triplea by triplea-game.

the class TransportUtils method mapTransportsToLoad.

/**
 * Returns a map of unit -> transport. Tries to load units evenly across all transports.
 */
public static Map<Unit, Unit> mapTransportsToLoad(final Collection<Unit> units, final Collection<Unit> transports) {
    List<Unit> canBeTransported = CollectionUtils.getMatches(units, Matches.unitCanBeTransported());
    canBeTransported = sortByTransportCostDescending(canBeTransported);
    List<Unit> canTransport = CollectionUtils.getMatches(transports, Matches.unitCanTransport());
    canTransport = sortByTransportCapacityDescendingThenMovesDescending(canTransport);
    // Add units to transports evenly
    final Map<Unit, Unit> mapping = new HashMap<>();
    final IntegerMap<Unit> addedLoad = new IntegerMap<>();
    for (final Unit unit : canBeTransported) {
        final Optional<Unit> transport = loadUnitIntoFirstAvailableTransport(unit, canTransport, mapping, addedLoad);
        // Move loaded transport to end of list
        if (transport.isPresent()) {
            canTransport.remove(transport.get());
            canTransport.add(transport.get());
        }
    }
    return mapping;
}
Also used : IntegerMap(games.strategy.util.IntegerMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Unit(games.strategy.engine.data.Unit) TripleAUnit(games.strategy.triplea.TripleAUnit)

Example 65 with IntegerMap

use of games.strategy.util.IntegerMap in project triplea by triplea-game.

the class TuvUtils method getResourceCostsForTuvForAllPlayersMergedAndAveraged.

/**
 * Return a map where key are unit types and values are the AVERAGED for all players.
 * Any production rule that produces multiple units
 * (like artillery in NWO, costs 7 but makes 2 artillery, meaning effective price is 3.5 each)
 * will have their costs rounded up on a per unit basis.
 * Therefore, this map should NOT be used for Purchasing information!
 */
private static Map<UnitType, ResourceCollection> getResourceCostsForTuvForAllPlayersMergedAndAveraged(final GameData data) {
    final Map<UnitType, ResourceCollection> average = new HashMap<>();
    final Resource pus;
    data.acquireReadLock();
    try {
        pus = data.getResourceList().getResource(Constants.PUS);
    } finally {
        data.releaseReadLock();
    }
    final IntegerMap<Resource> defaultMap = new IntegerMap<>();
    defaultMap.put(pus, 1);
    final ResourceCollection defaultResources = new ResourceCollection(data, defaultMap);
    final Map<UnitType, List<ResourceCollection>> backups = new HashMap<>();
    final Map<UnitType, ResourceCollection> backupAveraged = new HashMap<>();
    for (final ProductionRule rule : data.getProductionRuleList().getProductionRules()) {
        if (rule == null || rule.getResults() == null || rule.getResults().isEmpty() || rule.getCosts() == null || rule.getCosts().isEmpty()) {
            continue;
        }
        final IntegerMap<NamedAttachable> unitMap = rule.getResults();
        final ResourceCollection costPerGroup = new ResourceCollection(data, rule.getCosts());
        final Set<UnitType> units = new HashSet<>();
        for (final NamedAttachable resourceOrUnit : unitMap.keySet()) {
            if (!(resourceOrUnit instanceof UnitType)) {
                continue;
            }
            units.add((UnitType) resourceOrUnit);
        }
        if (units.isEmpty()) {
            continue;
        }
        final int totalProduced = unitMap.totalValues();
        if (totalProduced == 1) {
            final UnitType ut = units.iterator().next();
            final List<ResourceCollection> current = backups.computeIfAbsent(ut, k -> new ArrayList<>());
            current.add(costPerGroup);
        } else if (totalProduced > 1) {
            costPerGroup.discount((double) 1 / (double) totalProduced);
            for (final UnitType ut : units) {
                final List<ResourceCollection> current = backups.computeIfAbsent(ut, k -> new ArrayList<>());
                current.add(costPerGroup);
            }
        }
    }
    for (final Entry<UnitType, List<ResourceCollection>> entry : backups.entrySet()) {
        final ResourceCollection avgCost = new ResourceCollection(entry.getValue().toArray(new ResourceCollection[0]), data);
        if (entry.getValue().size() > 1) {
            avgCost.discount((double) 1 / (double) entry.getValue().size());
        }
        backupAveraged.put(entry.getKey(), avgCost);
    }
    final Map<PlayerID, Map<UnitType, ResourceCollection>> allPlayersCurrent = getResourceCostsForTuv(data, false);
    allPlayersCurrent.remove(null);
    for (final UnitType ut : data.getUnitTypeList().getAllUnitTypes()) {
        final List<ResourceCollection> costs = new ArrayList<>();
        for (final Map<UnitType, ResourceCollection> entry : allPlayersCurrent.values()) {
            if (entry.get(ut) != null) {
                costs.add(entry.get(ut));
            }
        }
        if (costs.isEmpty()) {
            final ResourceCollection backup = backupAveraged.get(ut);
            if (backup != null) {
                costs.add(backup);
            } else {
                costs.add(defaultResources);
            }
        }
        final ResourceCollection avgCost = new ResourceCollection(costs.toArray(new ResourceCollection[0]), data);
        if (costs.size() > 1) {
            avgCost.discount((double) 1 / (double) costs.size());
        }
        average.put(ut, avgCost);
    }
    return average;
}
Also used : IntegerMap(games.strategy.util.IntegerMap) Unit(games.strategy.engine.data.Unit) Collection(java.util.Collection) Constants(games.strategy.triplea.Constants) Set(java.util.Set) UnitAttachment(games.strategy.triplea.attachments.UnitAttachment) HashMap(java.util.HashMap) NamedAttachable(games.strategy.engine.data.NamedAttachable) Resource(games.strategy.engine.data.Resource) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) GameData(games.strategy.engine.data.GameData) List(java.util.List) PlayerID(games.strategy.engine.data.PlayerID) ResourceCollection(games.strategy.engine.data.ResourceCollection) ProductionFrontier(games.strategy.engine.data.ProductionFrontier) Matches(games.strategy.triplea.delegate.Matches) Map(java.util.Map) Entry(java.util.Map.Entry) UnitType(games.strategy.engine.data.UnitType) ProductionRule(games.strategy.engine.data.ProductionRule) CollectionUtils(games.strategy.util.CollectionUtils) IntegerMap(games.strategy.util.IntegerMap) PlayerID(games.strategy.engine.data.PlayerID) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) NamedAttachable(games.strategy.engine.data.NamedAttachable) Resource(games.strategy.engine.data.Resource) ArrayList(java.util.ArrayList) ProductionRule(games.strategy.engine.data.ProductionRule) UnitType(games.strategy.engine.data.UnitType) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) IntegerMap(games.strategy.util.IntegerMap) ResourceCollection(games.strategy.engine.data.ResourceCollection) HashSet(java.util.HashSet)

Aggregations

IntegerMap (games.strategy.util.IntegerMap)132 UnitType (games.strategy.engine.data.UnitType)87 Test (org.junit.jupiter.api.Test)73 Route (games.strategy.engine.data.Route)66 Unit (games.strategy.engine.data.Unit)53 Territory (games.strategy.engine.data.Territory)39 ArrayList (java.util.ArrayList)35 PlayerID (games.strategy.engine.data.PlayerID)26 TripleAUnit (games.strategy.triplea.TripleAUnit)24 HashMap (java.util.HashMap)23 HashSet (java.util.HashSet)19 Resource (games.strategy.engine.data.Resource)16 GameData (games.strategy.engine.data.GameData)15 ProductionRule (games.strategy.engine.data.ProductionRule)14 Collection (java.util.Collection)12 List (java.util.List)12 UnitAttachment (games.strategy.triplea.attachments.UnitAttachment)10 Set (java.util.Set)10 RepairRule (games.strategy.engine.data.RepairRule)9 NamedAttachable (games.strategy.engine.data.NamedAttachable)7