Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multiple fixes related to counter-battery fire #3011

Merged
merged 4 commits into from
Jul 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ private void handleButtonClick(OffBoardDirection direction) {
JOptionPane.QUESTION_MESSAGE, null, SharedUtility
.getDisplayArray(eligibleTargets), null);
choice = SharedUtility.getTargetPicked(eligibleTargets, input);
} else if (eligibleTargets.size() == 1) {
} else if ((eligibleTargets.size() == 1) && (eligibleTargets.get(0) != null)) {
choice = eligibleTargets.get(0);
} else {
return;
Expand Down
8 changes: 5 additions & 3 deletions megamek/src/megamek/client/ui/swing/boardview/BoardView1.java
Original file line number Diff line number Diff line change
Expand Up @@ -6132,13 +6132,15 @@ public String getHexTooltip(MouseEvent e) {
return txt.toString();
}

private ArrayList<ArtilleryAttackAction> getArtilleryAttacksAtLocation(
Coords c) {
private ArrayList<ArtilleryAttackAction> getArtilleryAttacksAtLocation(Coords c) {
ArrayList<ArtilleryAttackAction> v = new ArrayList<ArtilleryAttackAction>();

for (Enumeration<ArtilleryAttackAction> attacks = game
.getArtilleryAttacks(); attacks.hasMoreElements(); ) {
ArtilleryAttackAction a = attacks.nextElement();
if (a.getTarget(game).getPosition().equals(c)) {
Targetable target = a.getTarget(game);

if ((target != null) && c.equals(target.getPosition())) {
v.add(a);
}
}
Expand Down
15 changes: 15 additions & 0 deletions megamek/src/megamek/common/Targetable.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.io.Serializable;
import java.util.Map;

import megamek.common.annotations.Nullable;

public interface Targetable extends Serializable {
public static final int TYPE_ENTITY = 0;
public static final int TYPE_HEX_CLEAR = 1;
Expand Down Expand Up @@ -151,4 +153,17 @@ default boolean tracksHeat() {

@Override
int hashCode();

/**
* Utility function used to safely tell whether two Targetables are in the same hex.
* Does not throw exceptions in case of nulls.
*/
public static boolean areAtSamePosition(@Nullable Targetable first, @Nullable Targetable second) {
if ((first == null) || (second == null) ||
(first.getPosition() == null) || (second.getPosition() == null)) {
return false;
}

return first.getPosition().equals(second.getPosition());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,11 @@ else if ((null != bestSpotter) && !(this instanceof ArtilleryWeaponDirectFireHan
AreaEffectHelper.clearMineFields(targetPos, Minefield.CLEAR_NUMBER_WEAPON, ae, vPhaseReport, game, server);
}

if(aaa.getTarget(game).isOffBoard()) {
Targetable updatedTarget = aaa.getTarget(game);

// the attack's target may have been destroyed or fled since the attack was generated
// so we need to carry out offboard/null checks against the "current" version of the target.
if ((updatedTarget != null) && updatedTarget.isOffBoard()) {
DamageFalloff df = AreaEffectHelper.calculateDamageFallOff(atype, shootingBA, mineClear);
int actualDamage = df.damage - (df.falloff * targetPos.distance(target.getPosition()));
Coords effectiveTargetPos = aaa.getCoords();
Expand All @@ -406,7 +410,7 @@ else if ((null != bestSpotter) && !(this instanceof ArtilleryWeaponDirectFireHan
}

if(actualDamage > 0) {
AreaEffectHelper.artilleryDamageEntity((Entity) aaa.getTarget(game), actualDamage, null, 0, false, asfFlak, isFlak, altitude,
AreaEffectHelper.artilleryDamageEntity((Entity) updatedTarget, actualDamage, null, 0, false, asfFlak, isFlak, altitude,
effectiveTargetPos, atype, targetPos, false, ae, null, altitude, vPhaseReport, server);
}
} else {
Expand Down
6 changes: 4 additions & 2 deletions megamek/src/megamek/server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -13481,14 +13481,16 @@ private void processAttack(Entity entity, Vector<EntityAction> vector) {
boolean firingAtNewHex = false;
final ArtilleryAttackAction aaa = (ArtilleryAttackAction) ea;
final Entity firingEntity = game.getEntity(aaa.getEntityId());
Targetable attackTarget = aaa.getTarget(game);

for (Enumeration<AttackHandler> j = game.getAttacks(); !firingAtNewHex
&& j.hasMoreElements(); ) {
WeaponHandler wh = (WeaponHandler) j.nextElement();
if (wh.waa instanceof ArtilleryAttackAction) {
ArtilleryAttackAction oaaa = (ArtilleryAttackAction) wh.waa;

if ((oaaa.getEntityId() == aaa.getEntityId())
&& !oaaa.getTarget(game).getPosition()
.equals(aaa.getTarget(game).getPosition())) {
&& !Targetable.areAtSamePosition(oaaa.getTarget(game), attackTarget)) {
NickAragua marked this conversation as resolved.
Show resolved Hide resolved
firingAtNewHex = true;
}
}
Expand Down