Skip to content

Commit

Permalink
misc UnitRole changes
Browse files Browse the repository at this point in the history
  • Loading branch information
SJuliez committed Nov 18, 2023
1 parent 5d60bc5 commit 97dc918
Showing 1 changed file with 59 additions and 28 deletions.
87 changes: 59 additions & 28 deletions megamek/src/megamek/common/UnitRole.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
import org.apache.logging.log4j.LogManager;

import java.util.Arrays;
import java.util.function.Predicate;

import static megamek.common.UnitRole.Availability.*;

/**
* Unit roles as defined by Alpha Strike Companion, used in formation building rules
Expand All @@ -32,49 +35,50 @@
* @author Neoancient
*/
public enum UnitRole {
/** This is the default role; given to units where the role definition is missing from their file. */
UNDETERMINED (false),

AMBUSHER (true),
BRAWLER (true),
JUGGERNAUT (true),
MISSILE_BOAT (true),
SCOUT (true),
SKIRMISHER (true),
SNIPER (true),
STRIKER (true),
ATTACK_FIGHTER (false),
DOGFIGHTER (false),
FAST_DOGFIGHTER (false),
FIRE_SUPPORT (false),
INTERCEPTOR (false),
TRANSPORT (false),

/** This role is used for some large aerospace units that intentionally have none of the combat roles. */
NONE (false);
/** This is the default role; given to units where the role definition is missing. */
UNDETERMINED (ALL),

private final boolean ground;
AMBUSHER (GROUND),
BRAWLER (GROUND),
JUGGERNAUT (GROUND),
MISSILE_BOAT (GROUND),
SCOUT (GROUND),
SKIRMISHER (GROUND),
SNIPER (GROUND),
STRIKER (GROUND),

UnitRole(boolean ground) {
this.ground = ground;
}
ATTACK_FIGHTER (AERO),
DOGFIGHTER (AERO),
FAST_DOGFIGHTER (AERO),
FIRE_SUPPORT (AERO),
INTERCEPTOR (AERO),
TRANSPORT (AERO),

public boolean isGroundRole() {
return ground;
}
/** This role is used for some large aerospace units that intentionally have none of the combat roles. */
NONE (AERO);

/** @return True when the given unit may use this role. Used in MML. */
@SuppressWarnings("unused")
public boolean isAvailableTo(BTObject unit) {
return (ground == unit.isGround()) && !unit.isUnitGroup();
return availableTo.fits(unit) && !unit.isUnitGroup();
}

public boolean isAnyOf(UnitRole role, UnitRole... otherRoles) {
return (this == role) || Arrays.stream(otherRoles).anyMatch(otherRole -> this == otherRole);
}

/** @return True when this role is not UNDETERMINED. (Returns true for NONE.) */
public boolean hasRole() {
return this != UNDETERMINED;
}

/**
* Parses the given string into the UnitRole if possible and returns it. If it can't parse the string,
* logs an error and returns UNDETERMINED. Does not return null.
*
* @return The UnitRole given as a string or UNDETERMINED.
*/
public static UnitRole parseRole(String role) {
switch (role.toLowerCase()) {
case "ambusher":
Expand Down Expand Up @@ -114,7 +118,7 @@ public static UnitRole parseRole(String role) {
case "none":
return NONE;
default:
LogManager.getLogger().error("Could not parse AS Role " + role);
LogManager.getLogger().error("Could not parse role " + role);
return UNDETERMINED;
}
}
Expand Down Expand Up @@ -182,6 +186,9 @@ public boolean qualifiesForRole(AlphaStrikeElement unit) {
* @return Boolean value indicating whether the unit meets the qualifications for this role.
*/
public boolean qualifiesForRole(AlphaStrikeElement unit, double tolerance) {
if (!isAvailableTo(unit)) {
return false;
}
double score = 0;
int speed = unit.getPrimaryMovementValue();
switch (this) {
Expand Down Expand Up @@ -408,4 +415,28 @@ public String toString() {
}
return sb.toString().trim();
}

// PRIVATE

private final Availability availableTo;

UnitRole(Availability availableTo) {
this.availableTo = availableTo;
}

enum Availability {
GROUND(BTObject::isGround),
AERO(BTObject::isAerospace),
ALL(unit -> true);

private final Predicate<BTObject> fits;

Availability(Predicate<BTObject> fits) {
this.fits = fits;
}

boolean fits(BTObject unit) {
return fits.test(unit);
}
}
}

0 comments on commit 97dc918

Please sign in to comment.