From 0bdbeaa388c5febda4fb979347c016ee5b40fca2 Mon Sep 17 00:00:00 2001 From: Ron Date: Mon, 2 May 2016 23:48:29 -0500 Subject: [PATCH 01/10] Move transport methods from MoveValidator to TransportUtils --- .../strategy/triplea/delegate/MoveValidator.java | 13 ------------- .../triplea/delegate/TransportTracker.java | 16 +++++++++------- .../triplea/delegate/UnitComparator.java | 3 ++- src/games/strategy/triplea/ui/EditPanel.java | 5 +++-- src/games/strategy/triplea/ui/MovePanel.java | 2 +- .../strategy/triplea/util/TransportUtils.java | 13 +++++++++++++ 6 files changed, 28 insertions(+), 24 deletions(-) diff --git a/src/games/strategy/triplea/delegate/MoveValidator.java b/src/games/strategy/triplea/delegate/MoveValidator.java index d0abdd81e50..18ea52b7810 100644 --- a/src/games/strategy/triplea/delegate/MoveValidator.java +++ b/src/games/strategy/triplea/delegate/MoveValidator.java @@ -935,19 +935,6 @@ public static boolean hasNeutralBeforeEnd(final Route route) { return route.hasNeutralBeforeEnd(); } - public static int getTransportCost(final Collection units) { - if (units == null) { - return 0; - } - int cost = 0; - final Iterator iter = units.iterator(); - while (iter.hasNext()) { - final Unit item = iter.next(); - cost += UnitAttachment.get(item.getType()).getTransportCost(); - } - return cost; - } - private static Collection getUnitsThatCantGoOnWater(final Collection units) { final Collection retUnits = new ArrayList(); for (final Unit unit : units) { diff --git a/src/games/strategy/triplea/delegate/TransportTracker.java b/src/games/strategy/triplea/delegate/TransportTracker.java index cd002121bf6..fd77e9f97a3 100644 --- a/src/games/strategy/triplea/delegate/TransportTracker.java +++ b/src/games/strategy/triplea/delegate/TransportTracker.java @@ -1,11 +1,5 @@ package games.strategy.triplea.delegate; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; - import games.strategy.engine.data.Change; import games.strategy.engine.data.ChangeFactory; import games.strategy.engine.data.CompositeChange; @@ -16,14 +10,22 @@ import games.strategy.triplea.Constants; import games.strategy.triplea.TripleAUnit; import games.strategy.triplea.attachments.UnitAttachment; +import games.strategy.triplea.util.TransportUtils; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; /** * Tracks which transports are carrying which units. Also tracks the capacity * that has been unloaded. To reset the unloaded call clearUnloadedCapacity(). */ public class TransportTracker { + public static int getCost(final Collection units) { - return MoveValidator.getTransportCost(units); + return TransportUtils.getTransportCost(units); } private static void assertTransport(final Unit u) { diff --git a/src/games/strategy/triplea/delegate/UnitComparator.java b/src/games/strategy/triplea/delegate/UnitComparator.java index 7b234d11e45..e3e94a365ed 100644 --- a/src/games/strategy/triplea/delegate/UnitComparator.java +++ b/src/games/strategy/triplea/delegate/UnitComparator.java @@ -5,6 +5,7 @@ import games.strategy.engine.data.Unit; import games.strategy.triplea.TripleAUnit; import games.strategy.triplea.attachments.UnitAttachment; +import games.strategy.triplea.util.TransportUtils; import games.strategy.util.IntegerMap; import games.strategy.util.Match; @@ -60,7 +61,7 @@ private static Comparator getCapacityComparator(final List transport final IntegerMap capacityMap = new IntegerMap(transports.size() + 1, 1); for (final Unit transport : transports) { final Collection transporting = TripleAUnit.get(transport).getTransporting(); - capacityMap.add(transport, MoveValidator.getTransportCost(transporting)); + capacityMap.add(transport, TransportUtils.getTransportCost(transporting)); } return new Comparator() { @Override diff --git a/src/games/strategy/triplea/ui/EditPanel.java b/src/games/strategy/triplea/ui/EditPanel.java index 394c4051077..49dfed110be 100644 --- a/src/games/strategy/triplea/ui/EditPanel.java +++ b/src/games/strategy/triplea/ui/EditPanel.java @@ -23,6 +23,7 @@ import games.strategy.triplea.delegate.UnitBattleComparator; import games.strategy.triplea.delegate.dataObjects.MustMoveWithDetails; import games.strategy.triplea.formatter.MyFormatter; +import games.strategy.triplea.util.TransportUtils; import games.strategy.triplea.util.UnitSeperator; import games.strategy.util.IntegerMap; import games.strategy.util.Match; @@ -550,8 +551,8 @@ public int compare(final Unit unit1, final Unit unit2) { // Sort by decreasing transport capacity final Collection transporting1 = u1.getTransporting(); final Collection transporting2 = u2.getTransporting(); - final int cost1 = MoveValidator.getTransportCost(transporting1); - final int cost2 = MoveValidator.getTransportCost(transporting2); + final int cost1 = TransportUtils.getTransportCost(transporting1); + final int cost2 = TransportUtils.getTransportCost(transporting2); if (cost1 != cost2) { return cost2 - cost1; } diff --git a/src/games/strategy/triplea/ui/MovePanel.java b/src/games/strategy/triplea/ui/MovePanel.java index f5e415d54ac..173a0553d66 100644 --- a/src/games/strategy/triplea/ui/MovePanel.java +++ b/src/games/strategy/triplea/ui/MovePanel.java @@ -216,7 +216,7 @@ public boolean match(final Collection units) { final IntegerMap capacityMap = new IntegerMap(); for (final Unit transport : sortedTransports) { final Collection transporting = TripleAUnit.get(transport).getTransporting(); - capacityMap.add(transport, MoveValidator.getTransportCost(transporting)); + capacityMap.add(transport, TransportUtils.getTransportCost(transporting)); } boolean hasChanged = false; final Comparator increasingCapacityComparator = diff --git a/src/games/strategy/triplea/util/TransportUtils.java b/src/games/strategy/triplea/util/TransportUtils.java index 9b41404aee9..f24865e99f1 100644 --- a/src/games/strategy/triplea/util/TransportUtils.java +++ b/src/games/strategy/triplea/util/TransportUtils.java @@ -194,4 +194,17 @@ public int compare(final Unit o1, final Unit o2) { return totalLoad; } + public static int getTransportCost(final Collection units) { + if (units == null) { + return 0; + } + int cost = 0; + final Iterator iter = units.iterator(); + while (iter.hasNext()) { + final Unit item = iter.next(); + cost += UnitAttachment.get(item.getType()).getTransportCost(); + } + return cost; + } + } From 30a6d2ccc121bcbe3842ed0f969e1afe3b8a0c1f Mon Sep 17 00:00:00 2001 From: Ron Date: Tue, 3 May 2016 22:13:54 -0500 Subject: [PATCH 02/10] Clean up method comments --- .../strategy/triplea/util/TransportUtils.java | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/games/strategy/triplea/util/TransportUtils.java b/src/games/strategy/triplea/util/TransportUtils.java index f24865e99f1..497dccba8bb 100644 --- a/src/games/strategy/triplea/util/TransportUtils.java +++ b/src/games/strategy/triplea/util/TransportUtils.java @@ -22,11 +22,8 @@ public class TransportUtils { /** - * This method is static so it can be called from the client side. - * - * @return a map of unit -> transport (null if no mapping can be - * done either because there is not sufficient transport capacity or because - * a unit is not with its transport) + * Returns a map of unit -> transport (null if no mapping can be done either because there is not sufficient transport + * capacity or because a unit is not with its transport) */ public static Map mapTransports(final Route route, final Collection units, final Collection transportsToLoad) { @@ -40,9 +37,8 @@ public static Map mapTransports(final Route route, final Collection< } /** - * Returns a map of unit -> transport. Unit must already be loaded in the - * transport. If no units are loaded in the transports then an empty Map will - * be returned. + * Returns a map of unit -> transport. Unit must already be loaded in the transport. If no units are loaded in the + * transports then an empty Map will be returned. */ private static Map mapTransportsAlreadyLoaded(final Collection units, final Collection transports) { @@ -67,8 +63,8 @@ private static Map mapTransportsAlreadyLoaded(final Collection } /** - * Returns a map of unit -> transport. Tries to find transports to load all - * units. If it can't succeed returns an empty Map. + * Returns a map of unit -> transport. Tries to find transports to load all units. If it can't succeed returns an + * empty Map. */ public static Map mapTransportsToLoad(final Collection units, final Collection transports) { final List canBeTransported = Match.getMatches(units, Matches.UnitCanBeTransported); From e998519ea7d30188db60008033b985fe65b9e18d Mon Sep 17 00:00:00 2001 From: Ron Date: Wed, 4 May 2016 00:06:15 -0500 Subject: [PATCH 03/10] Improve mapTransportsToLoad to try and fill as many units into as few transports as possible --- .../strategy/triplea/util/TransportUtils.java | 63 ++++++------------- 1 file changed, 19 insertions(+), 44 deletions(-) diff --git a/src/games/strategy/triplea/util/TransportUtils.java b/src/games/strategy/triplea/util/TransportUtils.java index 497dccba8bb..a588ec402eb 100644 --- a/src/games/strategy/triplea/util/TransportUtils.java +++ b/src/games/strategy/triplea/util/TransportUtils.java @@ -5,9 +5,7 @@ import games.strategy.triplea.attachments.UnitAttachment; import games.strategy.triplea.delegate.Matches; import games.strategy.triplea.delegate.TransportTracker; -import games.strategy.util.IntegerMap; import games.strategy.util.Match; -import games.strategy.util.Util; import java.util.ArrayList; import java.util.Collection; @@ -67,8 +65,8 @@ private static Map mapTransportsAlreadyLoaded(final Collection * empty Map. */ public static Map mapTransportsToLoad(final Collection units, final Collection transports) { - final List canBeTransported = Match.getMatches(units, Matches.UnitCanBeTransported); - int transportIndex = 0; + + // Sort units with the highest transport cost first final Comparator transportCostComparator = new Comparator() { @Override public int compare(final Unit o1, final Unit o2) { @@ -77,58 +75,35 @@ public int compare(final Unit o1, final Unit o2) { return cost2 - cost1; } }; - - // fill the units with the highest cost first which allows easy loading of 2 infantry and 2 tanks on 2 transports in - // WW2V2 rules + final List canBeTransported = Match.getMatches(units, Matches.UnitCanBeTransported); Collections.sort(canBeTransported, transportCostComparator); - final List canTransport = Match.getMatches(transports, Matches.UnitCanTransport); + + // Sort transports with the lowest capacity first final Comparator transportCapacityComparator = new Comparator() { @Override public int compare(final Unit o1, final Unit o2) { final int capacityLeft1 = TransportTracker.getAvailableCapacity(o1); final int capacityLeft2 = TransportTracker.getAvailableCapacity(o1); - if (capacityLeft1 != capacityLeft2) { - return capacityLeft1 - capacityLeft2; - } - final int capacity1 = UnitAttachment.get((o1).getUnitType()).getTransportCapacity(); - final int capacity2 = UnitAttachment.get((o2).getUnitType()).getTransportCapacity(); - return capacity1 - capacity2; + return capacityLeft1 - capacityLeft2; } }; - - // fill transports with the lowest capacity first + final List canTransport = Match.getMatches(transports, Matches.UnitCanTransport); Collections.sort(canTransport, transportCapacityComparator); + + // Add max units to transports final Map mapping = new HashMap(); - final IntegerMap addedLoad = new IntegerMap(); - final Comparator previouslyLoadedToLast = transportsThatPreviouslyUnloadedComeLast(); - for (final Unit land : canBeTransported) { - final UnitAttachment landUA = UnitAttachment.get(land.getType()); - final int cost = landUA.getTransportCost(); - boolean loaded = false; - - // we want to try to distribute units evenly to all the transports - // if the user has 2 infantry, and selects two transports to load - // we should put 1 infantry in each transport. - // the algorithm below does not guarantee even distribution in all cases - // but it solves most of the cases - final List shiftedToEnd = Util.shiftElementsToEnd(canTransport, transportIndex); - - // review the following loop in light of bug ticket 2827064- previously unloaded trns perhaps shouldn't be - // included. - Collections.sort(shiftedToEnd, previouslyLoadedToLast); - final Iterator transportIter = shiftedToEnd.iterator(); - while (transportIter.hasNext() && !loaded) { - transportIndex++; - if (transportIndex >= canTransport.size()) { - transportIndex = 0; + for (final Unit transport : canTransport) { + int capacity = TransportTracker.getAvailableCapacity(transport); + for (final Iterator it = canBeTransported.iterator(); it.hasNext();) { + if (capacity <= 0) { + break; } - final Unit transport = transportIter.next(); - int capacity = TransportTracker.getAvailableCapacity(transport); - capacity -= addedLoad.getInt(transport); + final Unit unit = it.next(); + final int cost = UnitAttachment.get((unit).getType()).getTransportCost(); if (capacity >= cost) { - addedLoad.add(transport, cost); - mapping.put(land, transport); - loaded = true; + capacity -= cost; + mapping.put(unit, transport); + it.remove(); } } } From 6516b75151261d901436078cf539f01a50b80918 Mon Sep 17 00:00:00 2001 From: Ron Date: Wed, 4 May 2016 00:07:17 -0500 Subject: [PATCH 04/10] Remove unused method --- .../strategy/triplea/util/TransportUtils.java | 20 ------------------- 1 file changed, 20 deletions(-) diff --git a/src/games/strategy/triplea/util/TransportUtils.java b/src/games/strategy/triplea/util/TransportUtils.java index a588ec402eb..c1322710559 100644 --- a/src/games/strategy/triplea/util/TransportUtils.java +++ b/src/games/strategy/triplea/util/TransportUtils.java @@ -110,26 +110,6 @@ public int compare(final Unit o1, final Unit o2) { return mapping; } - private static Comparator transportsThatPreviouslyUnloadedComeLast() { - return new Comparator() { - @Override - public int compare(final Unit t1, final Unit t2) { - if (t1 == t2 || t1.equals(t2)) { - return 0; - } - final boolean t1previous = TransportTracker.hasTransportUnloadedInPreviousPhase(t1); - final boolean t2previous = TransportTracker.hasTransportUnloadedInPreviousPhase(t2); - if (t1previous == t2previous) { - return 0; - } - if (t1previous == false) { - return -1; - } - return 1; - } - }; - } - public static List findUnitsToLoadOnAirTransports(final Collection units, final Collection transports) { final Collection airTransports = Match.getMatches(transports, Matches.UnitIsAirTransport); From 76d0f91d1731c1fc7dd6950653c28d783ba3ba43 Mon Sep 17 00:00:00 2001 From: Ron Date: Wed, 4 May 2016 21:54:02 -0500 Subject: [PATCH 05/10] Extract sorting code into helper methods --- .../strategy/triplea/util/TransportUtils.java | 59 ++++++++++--------- 1 file changed, 32 insertions(+), 27 deletions(-) diff --git a/src/games/strategy/triplea/util/TransportUtils.java b/src/games/strategy/triplea/util/TransportUtils.java index c1322710559..53f96e008e6 100644 --- a/src/games/strategy/triplea/util/TransportUtils.java +++ b/src/games/strategy/triplea/util/TransportUtils.java @@ -35,8 +35,7 @@ public static Map mapTransports(final Route route, final Collection< } /** - * Returns a map of unit -> transport. Unit must already be loaded in the transport. If no units are loaded in the - * transports then an empty Map will be returned. + * Returns a map of unit -> transport. Unit must already be loaded in the transport. */ private static Map mapTransportsAlreadyLoaded(final Collection units, final Collection transports) { @@ -61,34 +60,12 @@ private static Map mapTransportsAlreadyLoaded(final Collection } /** - * Returns a map of unit -> transport. Tries to find transports to load all units. If it can't succeed returns an - * empty Map. + * Returns a map of unit -> transport. Tries to find transports to load all units. */ public static Map mapTransportsToLoad(final Collection units, final Collection transports) { - // Sort units with the highest transport cost first - final Comparator transportCostComparator = new Comparator() { - @Override - public int compare(final Unit o1, final Unit o2) { - final int cost1 = UnitAttachment.get((o1).getUnitType()).getTransportCost(); - final int cost2 = UnitAttachment.get((o2).getUnitType()).getTransportCost(); - return cost2 - cost1; - } - }; - final List canBeTransported = Match.getMatches(units, Matches.UnitCanBeTransported); - Collections.sort(canBeTransported, transportCostComparator); - - // Sort transports with the lowest capacity first - final Comparator transportCapacityComparator = new Comparator() { - @Override - public int compare(final Unit o1, final Unit o2) { - final int capacityLeft1 = TransportTracker.getAvailableCapacity(o1); - final int capacityLeft2 = TransportTracker.getAvailableCapacity(o1); - return capacityLeft1 - capacityLeft2; - } - }; - final List canTransport = Match.getMatches(transports, Matches.UnitCanTransport); - Collections.sort(canTransport, transportCapacityComparator); + final List canBeTransported = sortByTransportCostDescending(units); + final List canTransport = sortByTransportCapacityAscending(transports); // Add max units to transports final Map mapping = new HashMap(); @@ -158,4 +135,32 @@ public static int getTransportCost(final Collection units) { return cost; } + private static List sortByTransportCapacityAscending(final Collection transports) { + final Comparator transportCapacityComparator = new Comparator() { + @Override + public int compare(final Unit o1, final Unit o2) { + final int capacityLeft1 = TransportTracker.getAvailableCapacity(o1); + final int capacityLeft2 = TransportTracker.getAvailableCapacity(o1); + return capacityLeft1 - capacityLeft2; + } + }; + final List canTransport = Match.getMatches(transports, Matches.UnitCanTransport); + Collections.sort(canTransport, transportCapacityComparator); + return canTransport; + } + + private static List sortByTransportCostDescending(final Collection units) { + final Comparator transportCostComparator = new Comparator() { + @Override + public int compare(final Unit o1, final Unit o2) { + final int cost1 = UnitAttachment.get((o1).getUnitType()).getTransportCost(); + final int cost2 = UnitAttachment.get((o2).getUnitType()).getTransportCost(); + return cost2 - cost1; + } + }; + final List canBeTransported = Match.getMatches(units, Matches.UnitCanBeTransported); + Collections.sort(canBeTransported, transportCostComparator); + return canBeTransported; + } + } From 57233897ea0d48a9af698ced148b8d286d0361c8 Mon Sep 17 00:00:00 2001 From: Ron Date: Wed, 4 May 2016 23:31:56 -0500 Subject: [PATCH 06/10] Split mapTransportToLoad into 2 methods. One that focuses on finding the min number of transports needed to load from the list. One that focuses on distributing units evenly across the list of transports. The reason this is needed is we want to optimize the default to the min but after the user selects the number to load we want to put units in all of them. --- .../strategy/triplea/util/TransportUtils.java | 44 +++++++++++++++++-- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/src/games/strategy/triplea/util/TransportUtils.java b/src/games/strategy/triplea/util/TransportUtils.java index 53f96e008e6..72b42be1cd9 100644 --- a/src/games/strategy/triplea/util/TransportUtils.java +++ b/src/games/strategy/triplea/util/TransportUtils.java @@ -5,6 +5,7 @@ import games.strategy.triplea.attachments.UnitAttachment; import games.strategy.triplea.delegate.Matches; import games.strategy.triplea.delegate.TransportTracker; +import games.strategy.util.IntegerMap; import games.strategy.util.Match; import java.util.ArrayList; @@ -60,14 +61,51 @@ private static Map mapTransportsAlreadyLoaded(final Collection } /** - * Returns a map of unit -> transport. Tries to find transports to load all units. + * Returns a map of unit -> transport. Tries to load units evenly across all transports. */ - public static Map mapTransportsToLoad(final Collection units, final Collection transports) { + public static Map mapTransportsToLoad(final Collection units, + final Collection transports) { + + final List canBeTransported = sortByTransportCostDescending(units); + final List canTransport = sortByTransportCapacityAscending(transports); + + // Add units to transports evenly + final Map mapping = new HashMap(); + final IntegerMap addedLoad = new IntegerMap(); + for (final Unit unit : canBeTransported) { + + // Find first transport that has enough capacity + final int cost = UnitAttachment.get((unit).getType()).getTransportCost(); + Unit loadedTransport = null; + for (final Unit transport : canTransport) { + final int capacity = TransportTracker.getAvailableCapacity(transport) - addedLoad.getInt(transport); + if (capacity >= cost) { + addedLoad.add(transport, cost); + mapping.put(unit, transport); + loadedTransport = transport; + break; // skip to next unit after loaded + } + } + + // Move loaded transport to end of list + if (loadedTransport != null) { + canTransport.remove(loadedTransport); + canTransport.add(loadedTransport); + } + } + return mapping; + } + + /** + * Returns a map of unit -> transport. Tries load max units into each transport before moving to next. + */ + public static Map mapTransportsToLoadUsingMinTransports(final Collection units, + final Collection transports) { final List canBeTransported = sortByTransportCostDescending(units); final List canTransport = sortByTransportCapacityAscending(transports); - // Add max units to transports + // Add max units to each transport final Map mapping = new HashMap(); for (final Unit transport : canTransport) { int capacity = TransportTracker.getAvailableCapacity(transport); From be3d6859aa482f401a2d189e8bb8e5d0675fd218 Mon Sep 17 00:00:00 2001 From: Ron Date: Wed, 4 May 2016 23:32:49 -0500 Subject: [PATCH 07/10] Update MovePanel to call the new MinTransports loading method to improve the default number displayed to the user. --- src/games/strategy/triplea/ui/MovePanel.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/games/strategy/triplea/ui/MovePanel.java b/src/games/strategy/triplea/ui/MovePanel.java index 173a0553d66..ccb33cef766 100644 --- a/src/games/strategy/triplea/ui/MovePanel.java +++ b/src/games/strategy/triplea/ui/MovePanel.java @@ -637,7 +637,7 @@ public boolean match(final Unit transport) { capableTransports.removeAll(alliedTransports); // First, load capable transports final Map unitsToCapableTransports = - TransportUtils.mapTransports(route, availableUnits, capableTransports); + TransportUtils.mapTransportsToLoadUsingMinTransports(availableUnits, capableTransports); for (final Unit unit : unitsToCapableTransports.keySet()) { final Unit transport = unitsToCapableTransports.get(unit); final int unitCost = UnitAttachment.get(unit.getType()).getTransportCost(); @@ -647,7 +647,7 @@ public boolean match(final Unit transport) { availableUnits.removeAll(unitsToCapableTransports.keySet()); // Next, load allied transports final Map unitsToAlliedTransports = - TransportUtils.mapTransports(route, availableUnits, alliedTransports); + TransportUtils.mapTransportsToLoadUsingMinTransports(availableUnits, alliedTransports); for (final Unit unit : unitsToAlliedTransports.keySet()) { final Unit transport = unitsToAlliedTransports.get(unit); final int unitCost = UnitAttachment.get(unit.getType()).getTransportCost(); @@ -661,7 +661,7 @@ public boolean match(final Unit transport) { // are selected, since it may not be obvious if (getSelectedEndpointTerritory() == null) { final Map unitsToIncapableTransports = - TransportUtils.mapTransports(route, availableUnits, incapableTransports); + TransportUtils.mapTransportsToLoadUsingMinTransports(availableUnits, incapableTransports); for (final Unit unit : unitsToIncapableTransports.keySet()) { final Unit transport = unitsToIncapableTransports.get(unit); final int unitCost = UnitAttachment.get(unit.getType()).getTransportCost(); From f6339b3f5242bed55f90e97ed263963f757d88ec Mon Sep 17 00:00:00 2001 From: Ron Date: Wed, 4 May 2016 23:35:53 -0500 Subject: [PATCH 08/10] Organize methods --- .../strategy/triplea/util/TransportUtils.java | 53 +++++++++---------- 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/src/games/strategy/triplea/util/TransportUtils.java b/src/games/strategy/triplea/util/TransportUtils.java index 72b42be1cd9..b92b8ae5acf 100644 --- a/src/games/strategy/triplea/util/TransportUtils.java +++ b/src/games/strategy/triplea/util/TransportUtils.java @@ -21,8 +21,7 @@ public class TransportUtils { /** - * Returns a map of unit -> transport (null if no mapping can be done either because there is not sufficient transport - * capacity or because a unit is not with its transport) + * Returns a map of unit -> transport. */ public static Map mapTransports(final Route route, final Collection units, final Collection transportsToLoad) { @@ -35,31 +34,6 @@ public static Map mapTransports(final Route route, final Collection< return mapTransportsAlreadyLoaded(units, units); } - /** - * Returns a map of unit -> transport. Unit must already be loaded in the transport. - */ - private static Map mapTransportsAlreadyLoaded(final Collection units, - final Collection transports) { - final Collection canBeTransported = Match.getMatches(units, Matches.UnitCanBeTransported); - final Collection canTransport = Match.getMatches(transports, Matches.UnitCanTransport); - final Map mapping = new HashMap(); - final Iterator land = canBeTransported.iterator(); - while (land.hasNext()) { - final Unit currentTransported = land.next(); - final Unit transport = TransportTracker.transportedBy(currentTransported); - - // already being transported, make sure it is in transports - if (transport == null) { - continue; - } - if (!canTransport.contains(transport)) { - continue; - } - mapping.put(currentTransported, transport); - } - return mapping; - } - /** * Returns a map of unit -> transport. Tries to load units evenly across all transports. */ @@ -173,6 +147,31 @@ public static int getTransportCost(final Collection units) { return cost; } + /** + * Returns a map of unit -> transport. Unit must already be loaded in the transport. + */ + private static Map mapTransportsAlreadyLoaded(final Collection units, + final Collection transports) { + final Collection canBeTransported = Match.getMatches(units, Matches.UnitCanBeTransported); + final Collection canTransport = Match.getMatches(transports, Matches.UnitCanTransport); + final Map mapping = new HashMap(); + final Iterator land = canBeTransported.iterator(); + while (land.hasNext()) { + final Unit currentTransported = land.next(); + final Unit transport = TransportTracker.transportedBy(currentTransported); + + // already being transported, make sure it is in transports + if (transport == null) { + continue; + } + if (!canTransport.contains(transport)) { + continue; + } + mapping.put(currentTransported, transport); + } + return mapping; + } + private static List sortByTransportCapacityAscending(final Collection transports) { final Comparator transportCapacityComparator = new Comparator() { @Override From 93c0e5da7e4c81c39474409012700fbe18fc325c Mon Sep 17 00:00:00 2001 From: Ron Date: Wed, 4 May 2016 23:46:43 -0500 Subject: [PATCH 09/10] Fix small typo --- src/games/strategy/triplea/util/TransportUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/games/strategy/triplea/util/TransportUtils.java b/src/games/strategy/triplea/util/TransportUtils.java index b92b8ae5acf..4c2c8dcb4c0 100644 --- a/src/games/strategy/triplea/util/TransportUtils.java +++ b/src/games/strategy/triplea/util/TransportUtils.java @@ -177,7 +177,7 @@ private static List sortByTransportCapacityAscending(final Collection Date: Thu, 5 May 2016 23:45:49 -0500 Subject: [PATCH 10/10] Added helper method to improve readability --- .../strategy/triplea/util/TransportUtils.java | 38 +++++++++---------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/games/strategy/triplea/util/TransportUtils.java b/src/games/strategy/triplea/util/TransportUtils.java index 4c2c8dcb4c0..732d4f8533b 100644 --- a/src/games/strategy/triplea/util/TransportUtils.java +++ b/src/games/strategy/triplea/util/TransportUtils.java @@ -16,6 +16,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Optional; public class TransportUtils { @@ -47,24 +48,12 @@ public static Map mapTransportsToLoad(final Collection units, final Map mapping = new HashMap(); final IntegerMap addedLoad = new IntegerMap(); for (final Unit unit : canBeTransported) { - - // Find first transport that has enough capacity - final int cost = UnitAttachment.get((unit).getType()).getTransportCost(); - Unit loadedTransport = null; - for (final Unit transport : canTransport) { - final int capacity = TransportTracker.getAvailableCapacity(transport) - addedLoad.getInt(transport); - if (capacity >= cost) { - addedLoad.add(transport, cost); - mapping.put(unit, transport); - loadedTransport = transport; - break; // skip to next unit after loaded - } - } + final Optional transport = loadUnitIntoFirstAvailableTransport(unit, canTransport, mapping, addedLoad); // Move loaded transport to end of list - if (loadedTransport != null) { - canTransport.remove(loadedTransport); - canTransport.add(loadedTransport); + if (transport.isPresent()) { + canTransport.remove(transport.get()); + canTransport.add(transport.get()); } } return mapping; @@ -84,9 +73,6 @@ public static Map mapTransportsToLoadUsingMinTransports(final Collec for (final Unit transport : canTransport) { int capacity = TransportTracker.getAvailableCapacity(transport); for (final Iterator it = canBeTransported.iterator(); it.hasNext();) { - if (capacity <= 0) { - break; - } final Unit unit = it.next(); final int cost = UnitAttachment.get((unit).getType()).getTransportCost(); if (capacity >= cost) { @@ -200,4 +186,18 @@ public int compare(final Unit o1, final Unit o2) { return canBeTransported; } + private static Optional loadUnitIntoFirstAvailableTransport(final Unit unit, final List canTransport, + final Map mapping, final IntegerMap addedLoad) { + final int cost = UnitAttachment.get((unit).getType()).getTransportCost(); + for (final Unit transport : canTransport) { + final int capacity = TransportTracker.getAvailableCapacity(transport) - addedLoad.getInt(transport); + if (capacity >= cost) { + addedLoad.add(transport, cost); + mapping.put(unit, transport); + return Optional.of(transport); + } + } + return Optional.empty(); + } + }