Package megamek.common.util
Class SimpleRandomLanceCreator<T>
java.lang.Object
megamek.common.util.SimpleRandomLanceCreator<T>
A simple random lance creator similar to MM's random army "BV" tab, but generalized to allow creating forces of
various classes (Entity, MekSummary, AlphaStrikeElement) and with any "strength" function, i.e. forces can also be
created to match a total tonnage or cost or even a total Alpha Strike IF value instead of PV/BV. This creator doesn't
create any force structure, therefore it is called "lance" creator but it can be used for any amount of units.
-
Constructor Summary
ConstructorsConstructorDescriptionSimpleRandomLanceCreator(ToDoubleFunction<T> strengthFunction) Creates a random army creator for the given class parameter of unit that uses the given strength function to balance created forces. -
Method Summary
Modifier and TypeMethodDescriptionstatic List<MekSummary> advancedFilterResult(ASAdvancedSearchPanel asFilter, MekSearchFilter twFilter, Predicate<MekSummary> manualFilter) This helper method filters all units present in the cache by the given filters and returns the results as a list.buildForce(List<? extends T> available, int unitCount, int targetStrength, int strengthMargin) Returns a list of units taken only from the given list of available units; the returned list has the given unit count and is close to the given target strength as determined by the strength function used in the constructor (e.g.static SimpleRandomLanceCreator<MekSummary> forCost()static SimpleRandomLanceCreator<MekSummary> static SimpleRandomLanceCreator<MekSummary> static SimpleRandomLanceCreator<MekSummary>
-
Constructor Details
-
SimpleRandomLanceCreator
Creates a random army creator for the given class parameter of unit that uses the given strength function to balance created forces. The class parameter gives the type of unit that will be selected from a given roster, e.g. MekSummary, Entity or AlphaStrikeElement. Other classes can be used but a roster of available units must be created somehow and the MekSummaryCache is the most easily available roster. The strength function must act on the given class of units and will typically be the PV or BV but other functions can be used as well, e.g. the cost or weight or even more exotic values like the sum of the Alpha Strike IF and ARTx values (MekSummaries contain both TW and AS values). Created forces will be chosen so that the sum of the function result for the units falls into a given range.- Parameters:
strengthFunction- A method reference that maps a unit to an int value, such as MekSummary::getBV
-
-
Method Details
-
forAlphaStrike
- Returns:
- A random army creator for AlphaStrikeElement units. It uses the point value to balance the force. This
army creator requires a list of AlphaStrikeElements as available units; to work with the cache, use
mekSummaryWithPv()instead.
-
mekSummaryWithPv
- Returns:
- A random army creator for MekSummary units. It uses the Alpha Strike point value to balance the force and requires a roster of MekSummary entries that can be generated e.g. from an advanced search panel. This is the standard generator for Alpha Strike units.
-
mekSummaryWithBv
- Returns:
- A random army creator for MekSummary units. It uses the Total Warfare battle value to balance the force and requires a roster of MekSummary entries that can be generated e.g. from an advanced search panel. This is the standard generator for TW units.
-
forCost
- Returns:
- A random army creator for MekSummary units. It uses the c-bill cost to balance the force (i.e., the strength of the units in battle value is not considered).
-
forTonnage
- Returns:
- A random army creator for MekSummary units. It uses the weight to balance the force (i.e., the strength of the units in battle value is not considered).
-
buildForce
public List<T> buildForce(List<? extends T> available, int unitCount, int targetStrength, int strengthMargin) Returns a list of units taken only from the given list of available units; the returned list has the given unit count and is close to the given target strength as determined by the strength function used in the constructor (e.g. MekSummary::getBV). The strengthMargin is the tolerance for the total strength; a unit list will be returned as soon as its total strength is not farther away from the target strength than the margin (both directions). A margin of 0 can be used, but will lead to longer searches. The search is always terminated after a number of tries and a force is then returned even if it falls outside the margin.- Parameters:
available- A list of units to choose from (when empty, an empty list is returned)unitCount- The number of units in the resulting listtargetStrength- The total strength that the resulting units should havestrengthMargin- The maximum deviation from the target to allow- Returns:
- A list of units matching the parameters
-
advancedFilterResult
public static List<MekSummary> advancedFilterResult(@Nullable ASAdvancedSearchPanel asFilter, @Nullable MekSearchFilter twFilter, @Nullable Predicate<MekSummary> manualFilter) This helper method filters all units present in the cache by the given filters and returns the results as a list. Any of the filters may be null. When all filters are null, the entire cache is returned. This can be used for any army creator that works on MekSummary entries (SimpleRandomLanceCreator<MekSummary>).- Parameters:
asFilter- The Alpha Strike advanced filtertwFilter- The TW advanced filtermanualFilter- A direct filter such asms -> ms.isBattleMek()- Returns:
- A list of units that match the given filters
-