Skip to content

Commit

Permalink
Merge pull request #3011 from NickAragua/figher_squadron_rework
Browse files Browse the repository at this point in the history
multiple fixes related to counter-battery fire
  • Loading branch information
NickAragua authored Jul 17, 2021
2 parents 4d816f4 + b8fb572 commit 0387144
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 8 deletions.
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)) {
firingAtNewHex = true;
}
}
Expand Down

0 comments on commit 0387144

Please sign in to comment.