From 7b7786b6b6c47385c5189eab1833d59490152142 Mon Sep 17 00:00:00 2001 From: sleet01 Date: Thu, 19 Dec 2024 12:00:55 -0800 Subject: [PATCH 1/3] Fix for THIS IS BAD spam --- megamek/src/megamek/client/bot/BotClient.java | 1 - megamek/src/megamek/client/bot/princess/Princess.java | 6 +++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/megamek/src/megamek/client/bot/BotClient.java b/megamek/src/megamek/client/bot/BotClient.java index 323c6cddfb5..43f7b254498 100644 --- a/megamek/src/megamek/client/bot/BotClient.java +++ b/megamek/src/megamek/client/bot/BotClient.java @@ -654,7 +654,6 @@ public double getMassOfAllInBuilding(final Game game, final Coords coords) { return dest; } - logger.error("Returning no deployment position; THIS IS BAD!"); // If NONE of them are acceptable, then just return null. return null; } diff --git a/megamek/src/megamek/client/bot/princess/Princess.java b/megamek/src/megamek/client/bot/princess/Princess.java index ecc1485a5c4..c750f9991fc 100644 --- a/megamek/src/megamek/client/bot/princess/Princess.java +++ b/megamek/src/megamek/client/bot/princess/Princess.java @@ -803,7 +803,11 @@ protected Coords rankDeploymentCoords(Entity deployedUnit, List possible logger.debug(sb.toString()); } // Fall back on old method - return super.getFirstValidCoords(deployedUnit, possibleDeployCoords); + Coords bestCandidate = super.getFirstValidCoords(deployedUnit, possibleDeployCoords); + if (bestCandidate == null) { + logger.error("Returning no deployment position; THIS IS BAD!"); + } + return bestCandidate; } @Override From 5323db2bc955af65f34a76cbb97d9e7dd65d3697 Mon Sep 17 00:00:00 2001 From: sleet01 Date: Thu, 19 Dec 2024 14:38:32 -0800 Subject: [PATCH 2/3] Add concurrency protections and fix unformatted log string --- megamek/src/megamek/client/bot/BotClient.java | 7 +++++-- megamek/src/megamek/common/EquipmentType.java | 8 +++++--- .../src/megamek/common/weapons/lasers/LaserWeapon.java | 4 +++- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/megamek/src/megamek/client/bot/BotClient.java b/megamek/src/megamek/client/bot/BotClient.java index 43f7b254498..2159f5f633d 100644 --- a/megamek/src/megamek/client/bot/BotClient.java +++ b/megamek/src/megamek/client/bot/BotClient.java @@ -140,9 +140,12 @@ public void gameTurnChange(GameTurnChangeEvent e) { public void gamePhaseChange(GamePhaseChangeEvent e) { calculatedTurnThisPhase = false; if (e.getOldPhase().isSimultaneous(getGame())) { - logger.info("%s: Calculated %d / %d turns for phase %s", + logger.info( + String.format("%s: Calculated %d / %d turns for phase %s", getName(), calculatedTurnsThisPhase, - getGame().getEntitiesOwnedBy(getLocalPlayer()), e.getOldPhase()); + getGame().getEntitiesOwnedBy(getLocalPlayer()), e.getOldPhase() + ) + ); } calculatedTurnsThisPhase = 0; } diff --git a/megamek/src/megamek/common/EquipmentType.java b/megamek/src/megamek/common/EquipmentType.java index 7c2cb86723b..da38aa2f65e 100644 --- a/megamek/src/megamek/common/EquipmentType.java +++ b/megamek/src/megamek/common/EquipmentType.java @@ -546,9 +546,11 @@ public boolean hasModeType(String modeType) { } // Avoid Concurrent Modification exception with this one simple trick! - for (Iterator iterator = modes.iterator(); iterator.hasNext();) { - if (iterator.next().getName().equals(modeType)) { - return true; + synchronized (modes) { + for (Iterator iterator = modes.iterator(); iterator.hasNext(); ) { + if (iterator.next().getName().equals(modeType)) { + return true; + } } } diff --git a/megamek/src/megamek/common/weapons/lasers/LaserWeapon.java b/megamek/src/megamek/common/weapons/lasers/LaserWeapon.java index ee046ac4d76..420c25659fa 100644 --- a/megamek/src/megamek/common/weapons/lasers/LaserWeapon.java +++ b/megamek/src/megamek/common/weapons/lasers/LaserWeapon.java @@ -72,7 +72,9 @@ public int getModesCount(Mounted mounted) { } //Only works if laser pulse module's "Pulse" modes are added last. - return (int) modes.stream().filter(mode -> !mode.getName().startsWith("Pulse")).count(); + synchronized (modes) { + return (int) modes.stream().filter(mode -> !mode.getName().startsWith("Pulse")).count(); + } } @Override From 5bcc422e5e05480ab7ae8a63db8c41ca2e0fdc48 Mon Sep 17 00:00:00 2001 From: sleet01 Date: Thu, 19 Dec 2024 15:08:25 -0800 Subject: [PATCH 3/3] Quiet false positive error message when determining deploymen hex scores for infantry --- megamek/src/megamek/common/Game.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/megamek/src/megamek/common/Game.java b/megamek/src/megamek/common/Game.java index a1be70299d6..c0c60d7e027 100644 --- a/megamek/src/megamek/common/Game.java +++ b/megamek/src/megamek/common/Game.java @@ -1475,6 +1475,9 @@ public synchronized List getEntitiesVector(Coords c, boolean ignore) { if (entityPosLookup.isEmpty() && !inGameTWEntities().isEmpty()) { resetEntityPositionLookup(); } + // For sanity check + GamePhase phase = getPhase(); + Set posEntities = entityPosLookup.get(c); List vector = new ArrayList<>(); if (posEntities != null) { @@ -1492,9 +1495,9 @@ public synchronized List getEntitiesVector(Coords c, boolean ignore) { if (e.isTargetable() || ignore) { vector.add(e); - // Sanity check + // Sanity check: report out-of-place entities if it's not the deployment phase HashSet positions = e.getOccupiedCoords(); - if (!positions.contains(c)) { + if (!phase.isDeployment() && !positions.contains(c)) { logger.error(e.getDisplayName() + " is not in " + c + "!"); } }