Skip to content

Commit a754177

Browse files
authored
Bridge collapse warning fix (#7992)
Disregards units not actually on a bridge for the collapse warning indicator Fixes #7878
2 parents 370e9ac + c044b66 commit a754177

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

megamek/src/megamek/client/ui/clientGUI/boardview/CollapseWarning.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,15 @@
4040
import megamek.client.ui.clientGUI.GUIPreferences;
4141
import megamek.client.ui.panels.phaseDisplay.DeploymentDisplay;
4242
import megamek.client.ui.panels.phaseDisplay.MovementDisplay;
43+
import megamek.common.Hex;
4344
import megamek.common.board.Board;
4445
import megamek.common.board.BoardLocation;
4546
import megamek.common.board.Coords;
4647
import megamek.common.enums.GamePhase;
4748
import megamek.common.game.Game;
4849
import megamek.common.units.Entity;
4950
import megamek.common.units.IBuilding;
51+
import megamek.common.units.Terrains;
5052
import megamek.logging.MMLogger;
5153

5254
/**
@@ -225,11 +227,14 @@ public static List<Coords> findCFWarningsDeployment(Game game, Entity entity, Bo
225227
* at the hex location that could cause a building to collapse.
226228
*/
227229
public static double calculateTotalTonnage(Game g, Entity selected, Coords c) {
228-
// Calculate total weight of entity and all entities at the location.
230+
Hex hex = g.getBoard().getHex(c);
229231
double totalWeight = selected.getWeight();
230232
List<Entity> units = g.getEntitiesVector(c, true);
231233
for (Entity ent : units) {
232-
if (CollapseWarning.isEntityPartOfWeight(selected, ent)) {
234+
boolean weightCounts = (hex.hasBridge() && isEntityPartOfBridgeWeight(selected, ent,
235+
hex.terrainLevel(Terrains.BRIDGE_ELEV)))
236+
|| (!hex.hasBridge() && isEntityPartOfWeight(selected, ent));
237+
if (weightCounts) {
233238
totalWeight += ent.getWeight();
234239
}
235240
}
@@ -240,6 +245,10 @@ private static boolean isEntityPartOfWeight(Entity selected, Entity inHex) {
240245
return ((selected != inHex) && inHex.isGround() && !inHex.isAirborneVTOLorWIGE());
241246
}
242247

248+
private static boolean isEntityPartOfBridgeWeight(Entity selected, Entity inHex, int bridgeElevation) {
249+
return isEntityPartOfWeight(selected, inHex) && (inHex.getElevation() == bridgeElevation);
250+
}
251+
243252
private CollapseWarning() {
244253
}
245254
}

megamek/src/megamek/common/Hex.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,14 @@ public boolean hasDepth1WaterOrDeeper() {
556556
return containsTerrain(Terrains.WATER) && (terrainLevel(Terrains.WATER) >= 1);
557557
}
558558

559+
/**
560+
* @return True if there is a bridge in this hex. Tests if the hex has Terrains.BRIDGE. When true, the hex will also
561+
* have Terrains.BRIDGE_ELEV and Terrains.BRIDGE_CF.
562+
*/
563+
public boolean hasBridge() {
564+
return containsTerrain(Terrains.BRIDGE);
565+
}
566+
559567
/**
560568
* @return the level of the terrain specified, or Terrain.LEVEL_NONE if the terrain is not present in the hex
561569
*/

megamek/unittests/megamek/client/ui/clientGUI/CollapseWarningTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import static org.junit.jupiter.api.Assertions.assertFalse;
3737
import static org.junit.jupiter.api.Assertions.assertNotNull;
3838
import static org.junit.jupiter.api.Assertions.assertTrue;
39+
import static org.mockito.ArgumentMatchers.any;
3940
import static org.mockito.Mockito.mock;
4041
import static org.mockito.Mockito.when;
4142

@@ -44,6 +45,7 @@
4445
import java.util.Vector;
4546

4647
import megamek.client.ui.clientGUI.boardview.CollapseWarning;
48+
import megamek.common.Hex;
4749
import megamek.common.board.Board;
4850
import megamek.common.board.Coords;
4951
import megamek.common.enums.GamePhase;
@@ -118,6 +120,7 @@ void testConstructionFactorWarningFindMovementWarnings() {
118120
IBuilding bld = createMockBuildingWith(buildingPosition, 20);
119121

120122
Board b = createMockBoardWith(buildingPosition, bld);
123+
when(g.getBoard()).thenReturn(b);
121124

122125
List<Coords> warnList = CollapseWarning.findCFWarningsMovement(g, e, b);
123126

@@ -216,6 +219,7 @@ void testConstructionFactorWarningFindDeploymentWarnings() {
216219
when(b.isLegalDeployment(expectedHex, e)).thenReturn(true);
217220

218221
Game g = mock(Game.class);
222+
when(g.getBoard()).thenReturn(b);
219223

220224
List<Coords> warnList = CollapseWarning.findCFWarningsDeployment(g, e, b);
221225

@@ -347,6 +351,8 @@ void testConstructionFactorWarningCalcTotalWeightWithUnit() {
347351
entities.add(onBuilding);
348352

349353
when(g.getEntitiesVector(new Coords(3, 7), true)).thenReturn(entities);
354+
Board b = createSimpleMockBoard();
355+
when(g.getBoard()).thenReturn(b);
350356

351357
double totalWeight = CollapseWarning.calculateTotalTonnage(g, e, new Coords(3, 7));
352358

@@ -368,6 +374,8 @@ void testConstructionFactorWarningCalcTotalWeightEntityOnBuilding() {
368374
entities.add(e);
369375

370376
when(g.getEntitiesVector(new Coords(3, 3), true)).thenReturn(entities);
377+
Board b = createSimpleMockBoard();
378+
when(g.getBoard()).thenReturn(b);
371379

372380
double totalWeight = CollapseWarning.calculateTotalTonnage(g, e, new Coords(3, 3));
373381

@@ -391,6 +399,8 @@ void testConstructionFactorWarningCalcTotalWeightVTOLOverHex() {
391399
entities.add(vtol);
392400

393401
when(g.getEntitiesVector(new Coords(3, 7), true)).thenReturn(entities);
402+
Board b = createSimpleMockBoard();
403+
when(g.getBoard()).thenReturn(b);
394404

395405
double totalWeight = CollapseWarning.calculateTotalTonnage(g, e, new Coords(3, 7));
396406

@@ -413,6 +423,8 @@ void testConstructionFactorWarningCalcTotalWeightAeroOverHex() {
413423
entities.add(aero);
414424

415425
when(g.getEntitiesVector(new Coords(3, 7), true)).thenReturn(entities);
426+
Board b = createSimpleMockBoard();
427+
when(g.getBoard()).thenReturn(b);
416428

417429
double totalWeight = CollapseWarning.calculateTotalTonnage(g, e, new Coords(3, 7));
418430

@@ -447,6 +459,16 @@ private IBuilding createMockBuildingWith(Coords pos, int cf) {
447459
private Board createMockBoardWith(Coords pos, IBuilding bld) {
448460
Board b = mock(Board.class);
449461
when(b.getBuildingAt(pos)).thenReturn(bld);
462+
Hex hex = mock(Hex.class);
463+
when(hex.hasBridge()).thenReturn(false);
464+
return b;
465+
}
466+
467+
private Board createSimpleMockBoard() {
468+
Board b = mock(Board.class);
469+
Hex hex = mock(Hex.class);
470+
when(hex.hasBridge()).thenReturn(false);
471+
when(b.getHex(any())).thenReturn(hex);
450472
return b;
451473
}
452474
}

0 commit comments

Comments
 (0)