Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions megamek/src/megamek/common/compute/Compute.java
Original file line number Diff line number Diff line change
Expand Up @@ -6449,12 +6449,14 @@ public static Coords scatterAssaultDrop(Coords coords, int margin) {
public static @Nullable Entity getSwarmMissileTarget(Game game, int aeId, Coords coords,
int weaponId) {
Entity tempEntity;
Entity attacker = game.getEntity(aeId);
// first, check the hex of the original target
Iterator<Entity> entities = game.getEntities(coords);
Vector<Entity> possibleTargets = new Vector<>();
while (entities.hasNext()) {
tempEntity = entities.next();
if (!tempEntity.getTargetedBySwarm(aeId, weaponId)) {
if (!tempEntity.getTargetedBySwarm(aeId, weaponId)
&& canBeSwarmedFrom(game, attacker, tempEntity)) {
// we found a target
possibleTargets.add(tempEntity);
}
Expand All @@ -6476,7 +6478,8 @@ public static Coords scatterAssaultDrop(Coords coords, int margin) {
entities = game.getEntities(tempcoords);
if (entities.hasNext()) {
tempEntity = entities.next();
if (!tempEntity.getTargetedBySwarm(aeId, weaponId)) {
if (!tempEntity.getTargetedBySwarm(aeId, weaponId)
&& canBeSwarmedFrom(game, attacker, tempEntity)) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, and it matters more because of this PR. The single-unit scan was already there and was mostly harmless, but this branch adds a reason to reject a unit, so a hex whose first unit is sheltering infantry now contributes nothing and hides whatever else is standing in it. Changed to walk the hex in dd00dd9, matching the original-target scan a few lines above.

// we found a target
possibleTargets.add(tempEntity);
}
Expand All @@ -6490,6 +6493,36 @@ public static Coords scatterAssaultDrop(Coords coords, int margin) {
return null;
}

/**
* Whether leftover swarm missiles may pick this unit as their secondary target.
*
* <p>Conventional infantry inside a building cannot be fired on directly from outside it; the attacker has to
* shoot the building hex instead (TW p.172). Leftover swarm missiles look for a target among the units nearby
* (TO:AUE p.183), and that search has to obey the same rule, or a volley aimed at a building ends up hitting
* the platoon sheltering in it.</p>
*
* <p>Everything else is fair game, including infantry in the open and non-infantry inside the building, which
* the building absorption rules already cover.</p>
*
* @param game the game being played
* @param attacker the unit that fired the volley, or {@code null} if it has left the board
* @param target the unit being considered as a secondary target
*
* @return {@code true} if the missiles may attack this unit
*/
private static boolean canBeSwarmedFrom(Game game, @Nullable Entity attacker, Entity target) {
boolean isShelteringInfantry = (target instanceof Infantry) && isInBuilding(game, target);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in dd00dd9 as SwarmMissileTargetTest. Four cases: infantry in a building skipped from outside, infantry in the open still picked, a non-infantry unit in the same building still picked, and a legal target standing behind a shelterer still found.

The last one fails with the loop change reverted. The shelter case failed while the building terrain was absent, so it is genuinely reading the rule rather than passing by accident.

Worth noting for anyone reusing this: the board is built in code rather than from a board string, because the test board loader fills hexes in file order rather than at the coordinates written, which put the building in the wrong hex.

if (!isShelteringInfantry) {
return true;
}
if (attacker == null) {
return false;
}
// Fire that reaches them through the building itself is legal, which is how a unit already inside shoots
// the platoon it shares the building with.
return LosEffects.calculateLOS(game, attacker, target).getThruBldg() != null;
}

public static @Nullable Coords getFinalPosition(Coords currentPosition, int... v) {
if ((v == null) || (v.length != 6) || (currentPosition == null)) {
return currentPosition;
Expand Down
Loading