diff --git a/megamek/src/megamek/common/ConstructionFactorWarning.java b/megamek/src/megamek/common/ConstructionFactorWarning.java index 09217076b23..117bda30c0c 100644 --- a/megamek/src/megamek/common/ConstructionFactorWarning.java +++ b/megamek/src/megamek/common/ConstructionFactorWarning.java @@ -187,7 +187,9 @@ protected static double calculateTotalTonnage(Game g, Entity e, Coords c) { double totalWeight = e.getWeight(); List units = g.getEntitiesVector(c, true); for (Entity ent : units) { - totalWeight += ent.getWeight(); + if (e != ent) { + totalWeight += ent.getWeight(); + } } return totalWeight; } diff --git a/megamek/unittests/megamek/common/ConstructionFactorWarningTest.java b/megamek/unittests/megamek/common/ConstructionFactorWarningTest.java index 54dadd22343..d65a7836432 100644 --- a/megamek/unittests/megamek/common/ConstructionFactorWarningTest.java +++ b/megamek/unittests/megamek/common/ConstructionFactorWarningTest.java @@ -328,6 +328,27 @@ public void testConstructionFactorWarningCalcTotalWeightWithUnit() { assertEquals(entityWeight + onBuildingWeight, totalWeight); } + @Test + public void testConstructionFactorWarningCalcTotalWeightEntityOnBuilding() { + // This test simulates the selected entity on a building. When + // calculating the weight we don't want to double count ourselves. (we + // are already accounting for our own weigh as the selected entity) + double entityWeight = 35.0; + + Game g = mock(Game.class); + Entity e = createMockEntityWith(new Coords(3, 3), 5, 3, entityWeight, true, false); + + // Mock a 25 ton entity already on the building hex. + List entities = new ArrayList(); + entities.add(e); + + when(g.getEntitiesVector(new Coords(3, 3), true)).thenReturn(entities); + + double totalWeight = ConstructionFactorWarning.calculateTotalTonnage(g, e, new Coords(3,3)); + + assertEquals(entityWeight, totalWeight); + } + // Helper function to setup a mock entity with various attributes. private Entity createMockEntityWith(Coords pos, int run, int jump, double weight, boolean ground, boolean offboard) { Entity e = mock(Entity.class);