Skip to content

Commit

Permalink
feat: added movement mod for BasicPathRanker, added also fixed some c…
Browse files Browse the repository at this point in the history
…ases of exception oriented programming
  • Loading branch information
Scoppio committed Jan 22, 2025
1 parent 32be242 commit f47eeee
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 20 deletions.
24 changes: 8 additions & 16 deletions megamek/src/megamek/client/bot/princess/BasicPathRanker.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ public class BasicPathRanker extends PathRanker {
// what it's doing
private final int UNIT_DESTRUCTION_FACTOR = 1000;

private final static int MOVEMENT_FACTOR = 5;

protected final DecimalFormat LOG_DECIMAL = new DecimalFormat("0.00", DecimalFormatSymbols.getInstance());
private final NumberFormat LOG_INT = NumberFormat.getIntegerInstance();
protected final NumberFormat LOG_PERCENT = NumberFormat.getPercentInstance();
Expand Down Expand Up @@ -387,22 +385,17 @@ protected double calculateAggressionMod(Entity movingUnit, MovePath path, Game g
// Lower this path ranking if I am moving away from my friends (weighted by Herd Mentality).
protected double calculateHerdingMod(Coords friendsCoords, MovePath path, StringBuilder formula) {
if (friendsCoords == null) {
formula.append(" + herdingMod [0 no friends]");
formula.append(" - herdingMod [0 no friends]");
return 0;
}

double startingDistance = friendsCoords.distance(path.getStartCoords());
double finalDistance = friendsCoords.distance(path.getFinalCoords());
// If difference is positive => we moved closer => reward
// If difference is negative => we moved farther => penalize
double difference = (startingDistance - finalDistance);
double herding = getOwner().getBehaviorSettings().getHerdMentalityValue();
double herdingMod = difference * herding;
double herdingMod = finalDistance * herding;

formula.append(" + herdingMod [")
.append(LOG_DECIMAL.format(herdingMod)).append(" = (")
.append(LOG_DECIMAL.format(startingDistance)).append(" - ")
.append(LOG_DECIMAL.format(finalDistance)).append(") * ")
formula.append(" - herdingMod [")
.append(LOG_DECIMAL.format(herdingMod)).append(" = ")
.append(LOG_DECIMAL.format(finalDistance)).append(" * ")
.append(LOG_DECIMAL.format(herding))
.append("]");
return herdingMod;
Expand Down Expand Up @@ -599,8 +592,8 @@ protected RankedPath rankPath(MovePath path, Game game, int maxRange, double fal
// ranks (weighted by Herd Mentality).
utility -= calculateHerdingMod(friendsCoords, pathCopy, formula);
}

utility += calculateMovementMod(movingUnit, pathCopy, game, formula);
// Movement is good, it gives defense and extends a player power in the game.
utility += calculateMovementMod(pathCopy, game, formula);

// Try to face the enemy.
double facingMod = calculateFacingMod(movingUnit, game, pathCopy, formula);
Expand Down Expand Up @@ -639,8 +632,7 @@ private double getBraveryMod(double successProbability, FiringPhysicalDamage dam
return braveryMod;
}


private double calculateMovementMod(Entity movingUnit, MovePath pathCopy, Game game, StringBuilder formula) {
private double calculateMovementMod(MovePath pathCopy, Game game, StringBuilder formula) {
var hexMoved = (double) pathCopy.getHexesMoved();
var distanceMoved = pathCopy.getDistanceTravelled();
var tmm = Compute.getTargetMovementModifier(distanceMoved, pathCopy.isJumping(), pathCopy.isAirborne(), game);
Expand Down
5 changes: 1 addition & 4 deletions megamek/src/megamek/client/bot/princess/PathRanker.java
Original file line number Diff line number Diff line change
Expand Up @@ -535,11 +535,8 @@ private boolean willBuildingCollapse(MovePath path, Game game) {
}

public static @Nullable Coords calcAllyCenter(int myId, @Nullable List<Entity> friends, Game game) {
if ((friends == null) || friends.isEmpty()) {
if ((friends == null) || friends.size() <= 1) {
return null;
} else if (friends.size() == 1) {
// Nobody here but me...
return friends.get(0).getPosition();
}

int xTotal = 0;
Expand Down

0 comments on commit f47eeee

Please sign in to comment.