From f47eeee265bb967b38a2b864ae74ce6fb39e7b25 Mon Sep 17 00:00:00 2001 From: Scoppio Date: Tue, 21 Jan 2025 23:59:00 -0300 Subject: [PATCH] feat: added movement mod for BasicPathRanker, added also fixed some cases of exception oriented programming --- .../client/bot/princess/BasicPathRanker.java | 24 +++++++------------ .../client/bot/princess/PathRanker.java | 5 +--- 2 files changed, 9 insertions(+), 20 deletions(-) diff --git a/megamek/src/megamek/client/bot/princess/BasicPathRanker.java b/megamek/src/megamek/client/bot/princess/BasicPathRanker.java index 8149752117b..7812aa6fbdf 100644 --- a/megamek/src/megamek/client/bot/princess/BasicPathRanker.java +++ b/megamek/src/megamek/client/bot/princess/BasicPathRanker.java @@ -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(); @@ -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; @@ -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); @@ -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); diff --git a/megamek/src/megamek/client/bot/princess/PathRanker.java b/megamek/src/megamek/client/bot/princess/PathRanker.java index 99144057d36..47681d40401 100644 --- a/megamek/src/megamek/client/bot/princess/PathRanker.java +++ b/megamek/src/megamek/client/bot/princess/PathRanker.java @@ -535,11 +535,8 @@ private boolean willBuildingCollapse(MovePath path, Game game) { } public static @Nullable Coords calcAllyCenter(int myId, @Nullable List 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;