/** Return a Comparator that will order the specified transports in preferred load order. */ public static Comparator<Unit> getLoadableTransportsComparator( final List<Unit> transports, final Route route, final PlayerID player) { final Comparator<Unit> decreasingCapacityComparator = getDecreasingCapacityComparator(transports); final Match<Unit> incapableTransportMatch = Matches.transportCannotUnload(route.getEnd()); return (u1, u2) -> { final TripleAUnit t1 = TripleAUnit.get(u1); final TripleAUnit t2 = TripleAUnit.get(u2); // Check if transport is incapable due to game state final boolean isIncapable1 = incapableTransportMatch.match(t1); final boolean isIncapable2 = incapableTransportMatch.match(t2); if (!isIncapable1 && isIncapable2) { return -1; } if (isIncapable1 && !isIncapable2) { return 1; } // Use allied transports as a last resort final boolean isAlliedTrn1 = !t1.getOwner().equals(player); final boolean isAlliedTrn2 = !t2.getOwner().equals(player); if (!isAlliedTrn1 && isAlliedTrn2) { return -1; } if (isAlliedTrn1 && !isAlliedTrn2) { return 1; } // Sort by decreasing transport capacity final int compareCapacity = decreasingCapacityComparator.compare(t1, t2); if (compareCapacity != 0) { return compareCapacity; } // Sort by decreasing movement final int left1 = t1.getMovementLeft(); final int left2 = t1.getMovementLeft(); if (left1 != left2) { return left2 - left1; } return Integer.compare(t1.hashCode(), t2.hashCode()); }; }
public static Collection<Unit> getUnitsLoadedOnAlliedTransportsThisTurn( final Collection<Unit> units) { final Collection<Unit> rVal = new ArrayList<Unit>(); for (final Unit u : units) { // a unit loaded onto an allied transport // cannot be unloaded in the same turn, so // if we check both wasLoadedThisTurn and // the transport that transports us, we can tell if // we were loaded onto an allied transport // if we are no longer being transported, // then we must have been transported on our own transport final TripleAUnit taUnit = (TripleAUnit) u; if (taUnit.getWasLoadedThisTurn() && taUnit.getTransportedBy() != null && // an allied transport if the owner of the transport is not the owner of the unit !taUnit.getTransportedBy().getOwner().equals(taUnit.getOwner())) { rVal.add(u); } } return rVal; }