if (target == null || (target.getTargetType() == Targetable.TYPE_ENTITY && (te.isDestroyed() || te.isDoomed() || te.crew.isDead()))) {
if (target == null || target.getTargetType() == Targetable.TYPE_ENTITY && (te.isDestroyed() || te.isDoomed() || te.crew.isDead())) {
    private void resolveDfaAttack(PhysicalResult pr, int lastEntityId) {        final DfaAttackAction daa = (DfaAttackAction)pr.aaa;        final Entity ae = game.getEntity(daa.getEntityId());        final Targetable target = game.getTarget(daa.getTargetType(), daa.getTargetId());        // get damage, ToHitData and roll from the PhysicalResult        int damage = pr.damage;        final ToHitData toHit = pr.toHit;        int roll = pr.roll;        Entity te = null;        if (target != null && target.getTargetType() == Targetable.TYPE_ENTITY) {            // Lets re-write around that horrible hack that was here before.            // So instead of asking if a specific location is wet and praying            // that it won't cause an NPE...            // We'll check 1) if the hex has water, and 2) if it's deep enough            // to cover the unit in question at its current elevation.            // It's especially important to make sure it's done this way,            // because some units (Sylph, submarines) can be at ANY elevation            // underwater, and VTOLs can be well above the surface.            te = (Entity)target;            IHex hex = game.getBoard().getHex(te.getPosition());            if (hex.containsTerrain(Terrains.WATER)) {                if (te.absHeight() < hex.getElevation())                    damage = (int)Math.ceil(damage * 0.5f);            }        }        boolean throughFront = true;        if (te!=null) {            throughFront = Compute.isThroughFrontHex(game, daa.getEntityId(), te);        }        final boolean glancing = (game.getOptions().booleanOption("maxtech_glancing_blows") && (roll == toHit.getValue()));        Report r;        // Which building takes the damage?        Building bldg = game.getBoard().getBuildingAt( daa.getTargetPos() );        // is the attacker dead?  because that sure messes up the calculations        if (ae == null) {            return;        }        final int direction = ae.getFacing();        if (lastEntityId != daa.getEntityId()) {            //who is making the attack            r = new Report(4005);            r.subject = ae.getId();            r.addDesc(ae);            addReport(r);        }        // entity isn't DFAing any more        ae.setDisplacementAttack(null);        // should we even bother?        if (target == null || (target.getTargetType() == Targetable.TYPE_ENTITY             && (te.isDestroyed() || te.isDoomed() || te.crew.isDead()))) {            r = new Report(4245);            r.subject = ae.getId();            r.indent();            addReport(r);            if (ae.isProne()) {                // attacker prone during weapons phase                doEntityFall(ae, daa.getTargetPos(), 2, 3, ae.getBasePilotingRoll());            } else {                // same effect as successful DFA                doEntityDisplacement(ae, ae.getPosition(), daa.getTargetPos(), new PilotingRollData(ae.getId(), 4, "executed death from above"));            }            return;        }        r = new Report(4246);        r.subject = ae.getId();        r.indent();        r.add(target.getDisplayName());        r.newlines = 0;        addReport(r);        // target still in the same position?        if ( !target.getPosition().equals(daa.getTargetPos()) ) {            r = new Report(4215);            r.subject = ae.getId();            addReport(r);            doEntityFallsInto(ae, ae.getPosition(), daa.getTargetPos(), ae.getBasePilotingRoll());            return;        }        // hack: if the attacker's prone, or incapacitated, fudge the roll        if (ae.isProne() || !ae.isActive()) {            roll = -12;            r = new Report(4250);            r.subject = ae.getId();            r.add(toHit.getDesc());            addReport(r);        } else if (toHit.getValue() == ToHitData.IMPOSSIBLE) {            roll = -12;            r = new Report(4255);            r.subject = ae.getId();            r.add(toHit.getDesc());            addReport(r);        } else if (toHit.getValue() == ToHitData.AUTOMATIC_SUCCESS) {            r = new Report(4260);            r.subject = ae.getId();            r.add(toHit.getDesc());            addReport(r);            roll = Integer.MAX_VALUE;        } else {            // report the roll            r = new Report(4025);            r.subject = ae.getId();            r.add(toHit.getValue());            r.add(roll);            r.newlines = 0;            addReport(r);            if (glancing) {                r = new Report(4030);                r.subject = ae.getId();                r.newlines = 0;                addReport(r);            }        }        // do we hit?        if (roll < toHit.getValue()) {            Coords dest = te.getPosition();            Coords targetDest = Compute.getPreferredDisplacement(game, te.getId(), dest, direction);            //miss            r = new Report(4035);            r.subject = ae.getId();            addReport(r);            if (targetDest != null) {                // attacker falls into destination hex                r = new Report(4265);                r.subject = ae.getId();                r.addDesc(ae);                r.add(dest.getBoardNum(), true);                addReport(r);                doEntityFall(ae, dest, 2, 3, ae.getBasePilotingRoll());                // move target to preferred hex                doEntityDisplacement(te, dest, targetDest, null);            } else {                // attacker destroyed  Tanks                // suffer an ammo/power plant hit.                // TODO : a Mech suffers a Head Blown Off crit.                addReport(                                      destroyEntity(ae, "impossible displacement", (ae instanceof Mech), (ae instanceof Mech)));            }            return;        }        // we hit...        // Can't DFA a target inside of a building.        int damageTaken = DfaAttackAction.getDamageTakenBy(ae);        r = new Report(4040);        r.subject = ae.getId();        addReport(r);        // Targeting a building.        if ( target.getTargetType() == Targetable.TYPE_BUILDING ) {            // The building takes the full brunt of the attack.            Report buildingReport = damageBuilding( bldg, damage );            buildingReport.indent();            buildingReport.subject = ae.getId();            addReport(buildingReport);            // Damage any infantry in the hex.            this.damageInfantryIn( bldg, damage );        }        // Target isn't building.        else {            if (glancing) {                damage = (int)Math.floor(damage/2.0);            }            //damage target            r = new Report(4230);            r.subject = ae.getId();            r.add(damage);            r.add(toHit.getTableDesc());            r.newlines = 0;            addReport(r);            while (damage > 0) {                int cluster = Math.min(5, damage);                HitData hit = te.rollHitLocation(toHit.getHitTable(), toHit.getSideTable());                addReport(                                      damageEntity(te, hit, cluster, false, 0, false, false, throughFront));                damage -= cluster;            }        }        if (glancing) {            // Glancing Blow rule doesn't state whether damage to attacker on charge            // or DFA is halved as well, assume yes. TODO: Check with PM            damageTaken = (int)Math.floor(damageTaken/2.0);        }        //damage attacker        r = new Report(4240);        r.subject = ae.getId();        r.add(damageTaken);        r.newlines = 0;        addReport(r);        while (damageTaken > 0) {            int cluster = Math.min(5, damageTaken);            HitData hit = ae.rollHitLocation(ToHitData.HIT_KICK, ToHitData.SIDE_FRONT);            addReport(                                      damageEntity(ae, hit, cluster));            damageTaken -= cluster;        }        addNewLines();        // That's it for target buildings.        // TODO: where do I put the attacker?!?        if ( target.getTargetType() == Targetable.TYPE_BUILDING ) {            return;        }        // Target entities are pushed away or destroyed.        Coords dest = te.getPosition();        Coords targetDest = Compute.getValidDisplacement(game, te.getId(), dest, direction);        if (targetDest != null) {            doEntityDisplacement(te, dest, targetDest, new PilotingRollData(te.getId(), 2, "hit by death from above"));        } else {            // ack!  automatic death!  Tanks            // suffer an ammo/power plant hit.            // TODO : a Mech suffers a Head Blown Off crit.            addReport(destroyEntity(te, "impossible displacement", (te instanceof Mech), (te instanceof Mech)));        }        // HACK: to avoid automatic falls, displace from dest to dest        doEntityDisplacement(ae, dest, dest, new PilotingRollData(ae.getId(), 4, "executed death from above"));    }