Skip to content

Commit

Permalink
Don't double count selected entity weight when on building.
Browse files Browse the repository at this point in the history
  • Loading branch information
HoneySkull committed Dec 26, 2023
1 parent 24d8bca commit 90671bc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
4 changes: 3 additions & 1 deletion megamek/src/megamek/common/ConstructionFactorWarning.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,9 @@ protected static double calculateTotalTonnage(Game g, Entity e, Coords c) {
double totalWeight = e.getWeight();
List<Entity> units = g.getEntitiesVector(c, true);
for (Entity ent : units) {
totalWeight += ent.getWeight();
if (e != ent) {
totalWeight += ent.getWeight();
}
}
return totalWeight;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Entity> entities = new ArrayList<Entity>();
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);
Expand Down

0 comments on commit 90671bc

Please sign in to comment.