public ToHitData toHit(IGame game, Targetable target) {
    final Entity ae = getEntity(game);

    // arguments legal?
    if (ae == null) {
      throw new IllegalStateException("Attacker is null");
    }

    // Do to pretreatment of physical attacks, the target may be null.
    if (target == null) {
      return new ToHitData(TargetRoll.IMPOSSIBLE, "Target is null");
    }

    if (!game.getOptions().booleanOption(OptionsConstants.BASE_FRIENDLY_FIRE)) {
      // a friendly unit can never be the target of a direct attack.
      if (target.getTargetType() == Targetable.TYPE_ENTITY
          && (((Entity) target).getOwnerId() == ae.getOwnerId()
              || (((Entity) target).getOwner().getTeam() != IPlayer.TEAM_NONE
                  && ae.getOwner().getTeam() != IPlayer.TEAM_NONE
                  && ae.getOwner().getTeam() == ((Entity) target).getOwner().getTeam())))
        return new ToHitData(
            TargetRoll.IMPOSSIBLE, "A friendly unit can never be the target of a direct attack.");
    }

    // set the to-hit
    ToHitData toHit = new ToHitData(2, "base");

    TeleMissile tm = (TeleMissile) ae;

    // thrust used
    if (ae.mpUsed > 0) toHit.addModifier(ae.mpUsed, "thrust used");

    // out of fuel
    if (tm.getFuel() <= 0) toHit.addModifier(+6, "out of fuel");

    // modifiers for the originating unit need to be added later, because
    // they may change as a result of damage

    // done!
    return toHit;
  }
  /**
   * handle this weapons firing
   *
   * @return a <code>boolean</code> value indicating wether this should be kept or not
   */
  @Override
  public boolean handle(IGame.Phase phase, Vector<Report> vPhaseReport) {
    if (!this.cares(phase)) {
      return true;
    }

    // Report weapon attack and its to-hit value.
    Report r = new Report(3115);
    r.indent();
    r.newlines = 0;
    r.subject = subjectId;
    r.add(wtype.getName());
    r.messageId = 3120;
    r.add(target.getDisplayName(), true);
    vPhaseReport.addElement(r);
    if (toHit.getValue() == TargetRoll.IMPOSSIBLE) {
      r = new Report(3135);
      r.subject = subjectId;
      r.add(toHit.getDesc());
      vPhaseReport.addElement(r);
      return false;
    } else if (toHit.getValue() == TargetRoll.AUTOMATIC_FAIL) {
      r = new Report(3140);
      r.newlines = 0;
      r.subject = subjectId;
      r.add(toHit.getDesc());
      vPhaseReport.addElement(r);
    } else if (toHit.getValue() == TargetRoll.AUTOMATIC_SUCCESS) {
      r = new Report(3145);
      r.newlines = 0;
      r.subject = subjectId;
      r.add(toHit.getDesc());
      vPhaseReport.addElement(r);
    }

    addHeat();

    // deliver screen
    Coords coords = target.getPosition();
    server.deliverScreen(coords, vPhaseReport);

    // damage any entities in the hex
    for (Entity entity : game.getEntitiesVector(coords)) {
      // if fighter squadron all fighters are damaged
      if (entity instanceof FighterSquadron) {
        for (Entity fighter : ((FighterSquadron) entity).getFighters()) {
          ToHitData squadronToHit = new ToHitData();
          squadronToHit.setHitTable(ToHitData.HIT_NORMAL);
          HitData hit = fighter.rollHitLocation(squadronToHit.getHitTable(), ToHitData.SIDE_FRONT);
          hit.setCapital(false);
          vPhaseReport.addAll(server.damageEntity(fighter, hit, attackValue));
          server.creditKill(fighter, ae);
        }
      } else {
        ToHitData hexToHit = new ToHitData();
        hexToHit.setHitTable(ToHitData.HIT_NORMAL);
        HitData hit = entity.rollHitLocation(hexToHit.getHitTable(), ToHitData.SIDE_FRONT);
        hit.setCapital(false);
        vPhaseReport.addAll(server.damageEntity(entity, hit, attackValue));
        server.creditKill(entity, ae);
      }
    }
    return false;
  }
  @Test
  public void testInitDamage() {
    Princess mockPrincess = Mockito.mock(Princess.class);

    FireControl mockFireControl = Mockito.mock(FireControl.class);
    Mockito.when(mockPrincess.getFireControl()).thenReturn(mockFireControl);

    ToHitData mockToHit = Mockito.mock(ToHitData.class);
    Mockito.when(
            mockFireControl.guessToHitModifierPhysical(
                Mockito.any(Entity.class),
                Mockito.any(EntityState.class),
                Mockito.any(Targetable.class),
                Mockito.any(EntityState.class),
                Mockito.any(PhysicalAttackType.class),
                Mockito.any(IGame.class)))
        .thenReturn(mockToHit);
    Mockito.when(mockToHit.getValue()).thenReturn(7);

    Entity mockShooter = Mockito.mock(BipedMech.class);
    Mockito.when(mockShooter.getId()).thenReturn(1);
    Mockito.when(mockShooter.getWeight()).thenReturn(50.0);

    EntityState mockShooterState = Mockito.mock(EntityState.class);

    Mech mockTarget = Mockito.mock(BipedMech.class);
    Mockito.when(mockTarget.isLocationBad(Mockito.anyInt())).thenReturn(false);
    Mockito.when(mockTarget.getArmor(Mockito.anyInt(), Mockito.eq(false))).thenReturn(10);
    Mockito.when(mockTarget.getArmor(Mockito.anyInt(), Mockito.eq(true))).thenReturn(5);
    Mockito.when(mockTarget.getInternal(Mockito.anyInt())).thenReturn(6);

    EntityState mockTargetState = Mockito.mock(EntityState.class);

    IGame mockGame = Mockito.mock(IGame.class);

    PhysicalInfo testPhysicalInfo = Mockito.spy(new PhysicalInfo(mockPrincess));
    testPhysicalInfo.setShooter(mockShooter);
    testPhysicalInfo.setTarget(mockTarget);
    Mockito.doNothing()
        .when(testPhysicalInfo)
        .setDamageDirection(Mockito.any(EntityState.class), Mockito.any(Coords.class));
    Mockito.doReturn(1).when(testPhysicalInfo).getDamageDirection();

    PhysicalAttackType punch = PhysicalAttackType.LEFT_PUNCH;
    PhysicalAttackType kick = PhysicalAttackType.LEFT_KICK;

    PunchAttackAction punchAction = Mockito.mock(PunchAttackAction.class);
    Mockito.doReturn(punchAction)
        .when(testPhysicalInfo)
        .buildAction(Mockito.eq(punch), Mockito.anyInt(), Mockito.any(Targetable.class));
    Mockito.when(punchAction.toHit(Mockito.any(IGame.class))).thenReturn(mockToHit);

    KickAttackAction kickAction = Mockito.mock(KickAttackAction.class);
    Mockito.doReturn(kickAction)
        .when(testPhysicalInfo)
        .buildAction(Mockito.eq(kick), Mockito.anyInt(), Mockito.any(Targetable.class));
    Mockito.when(kickAction.toHit(Mockito.any(IGame.class))).thenReturn(mockToHit);

    // Test a vanilla punch.
    testPhysicalInfo.setShooter(mockShooter);
    testPhysicalInfo.setAttackType(punch);
    testPhysicalInfo.initDamage(punch, mockShooterState, mockTargetState, true, mockGame);
    Assert.assertEquals(0.583, testPhysicalInfo.getProbabilityToHit(), TOLERANCE);
    Assert.assertEquals(5.0, testPhysicalInfo.getMaxDamage(), TOLERANCE);
    Assert.assertEquals(0.0099, testPhysicalInfo.getExpectedCriticals(), TOLERANCE);
    Assert.assertEquals(0.0, testPhysicalInfo.getKillProbability(), TOLERANCE);
    Assert.assertEquals(5.0, testPhysicalInfo.getExpectedDamageOnHit(), TOLERANCE);

    // Test a vanilla kick.
    testPhysicalInfo.setShooter(mockShooter);
    testPhysicalInfo.setAttackType(kick);
    testPhysicalInfo.initDamage(kick, mockShooterState, mockTargetState, true, mockGame);
    Assert.assertEquals(0.583, testPhysicalInfo.getProbabilityToHit(), TOLERANCE);
    Assert.assertEquals(10.0, testPhysicalInfo.getMaxDamage(), TOLERANCE);
    Assert.assertEquals(0.0099, testPhysicalInfo.getExpectedCriticals(), TOLERANCE);
    Assert.assertEquals(0.0, testPhysicalInfo.getKillProbability(), TOLERANCE);
    Assert.assertEquals(10.0, testPhysicalInfo.getExpectedDamageOnHit(), TOLERANCE);

    // Make the puncher heavier.
    Mockito.when(mockShooter.getWeight()).thenReturn(100.0);
    testPhysicalInfo.setShooter(mockShooter);
    testPhysicalInfo.setAttackType(punch);
    testPhysicalInfo.initDamage(punch, mockShooterState, mockTargetState, true, mockGame);
    Assert.assertEquals(0.583, testPhysicalInfo.getProbabilityToHit(), TOLERANCE);
    Assert.assertEquals(10.0, testPhysicalInfo.getMaxDamage(), TOLERANCE);
    Assert.assertEquals(0.0099, testPhysicalInfo.getExpectedCriticals(), TOLERANCE);
    Assert.assertEquals(0.0, testPhysicalInfo.getKillProbability(), TOLERANCE);
    Assert.assertEquals(10.0, testPhysicalInfo.getExpectedDamageOnHit(), TOLERANCE);

    // Give the target less armor and internals
    Mockito.when(mockTarget.isLocationBad(Mockito.anyInt())).thenReturn(false);
    Mockito.when(mockTarget.getArmor(Mockito.anyInt(), Mockito.eq(false))).thenReturn(6);
    Mockito.when(mockTarget.getArmor(Mockito.anyInt(), Mockito.eq(true))).thenReturn(3);
    Mockito.when(mockTarget.getInternal(Mockito.anyInt())).thenReturn(3);
    Mockito.when(mockShooter.getWeight()).thenReturn(100.0);
    testPhysicalInfo.setShooter(mockShooter);
    testPhysicalInfo.setAttackType(punch);
    testPhysicalInfo.initDamage(punch, mockShooterState, mockTargetState, true, mockGame);
    Assert.assertEquals(0.583, testPhysicalInfo.getProbabilityToHit(), TOLERANCE);
    Assert.assertEquals(10.0, testPhysicalInfo.getMaxDamage(), TOLERANCE);
    Assert.assertEquals(0.5929, testPhysicalInfo.getExpectedCriticals(), TOLERANCE);
    Assert.assertEquals(0.1943, testPhysicalInfo.getKillProbability(), TOLERANCE);
    Assert.assertEquals(10.0, testPhysicalInfo.getExpectedDamageOnHit(), TOLERANCE);

    // Test a non-biped trying to punch.
    testPhysicalInfo.setShooter(Mockito.mock(QuadMech.class));
    testPhysicalInfo.initDamage(punch, mockShooterState, mockTargetState, true, mockGame);
    Assert.assertEquals(0.0, testPhysicalInfo.getProbabilityToHit(), TOLERANCE);
    Assert.assertEquals(0.0, testPhysicalInfo.getMaxDamage(), TOLERANCE);
    Assert.assertEquals(0.0, testPhysicalInfo.getExpectedCriticals(), TOLERANCE);
    Assert.assertEquals(0.0, testPhysicalInfo.getKillProbability(), TOLERANCE);
    Assert.assertEquals(0.0, testPhysicalInfo.getExpectedDamageOnHit(), TOLERANCE);

    // Test not being able to hit.
    Mockito.when(mockToHit.getValue()).thenReturn(13);
    testPhysicalInfo.initDamage(punch, mockShooterState, mockTargetState, true, mockGame);
    Assert.assertEquals(0.0, testPhysicalInfo.getProbabilityToHit(), TOLERANCE);
    Assert.assertEquals(0.0, testPhysicalInfo.getMaxDamage(), TOLERANCE);
    Assert.assertEquals(0.0, testPhysicalInfo.getExpectedCriticals(), TOLERANCE);
    Assert.assertEquals(0.0, testPhysicalInfo.getKillProbability(), TOLERANCE);
    Assert.assertEquals(0.0, testPhysicalInfo.getExpectedDamageOnHit(), TOLERANCE);

    // Test a non-mech.
    testPhysicalInfo.setShooter(Mockito.mock(Tank.class));
    testPhysicalInfo.initDamage(punch, mockShooterState, mockTargetState, true, mockGame);
    Assert.assertEquals(0.0, testPhysicalInfo.getProbabilityToHit(), TOLERANCE);
    Assert.assertEquals(0.0, testPhysicalInfo.getMaxDamage(), TOLERANCE);
    Assert.assertEquals(0.0, testPhysicalInfo.getExpectedCriticals(), TOLERANCE);
    Assert.assertEquals(0.0, testPhysicalInfo.getKillProbability(), TOLERANCE);
    Assert.assertEquals(0.0, testPhysicalInfo.getExpectedDamageOnHit(), TOLERANCE);
  }
  /** To-hit number for the mech to push another mech */
  public static ToHitData toHit(IGame game, int attackerId, Targetable target) {
    final Entity ae = game.getEntity(attackerId);
    int targetId = Entity.NONE;
    Entity te = null;
    if (target.getTargetType() == Targetable.TYPE_ENTITY) {
      te = (Entity) target;
      targetId = target.getTargetId();
    }
    if (ae == null)
      return new ToHitData(ToHitData.IMPOSSIBLE, "You can't attack from a null entity!");
    if (te == null) return new ToHitData(ToHitData.IMPOSSIBLE, "You can't target a null entity!");
    IHex attHex = game.getBoard().getHex(ae.getPosition());
    IHex targHex = game.getBoard().getHex(te.getPosition());
    final int attackerElevation = ae.getElevation() + attHex.getElevation();
    final int targetElevation = target.getElevation() + targHex.getElevation();
    final boolean targetInBuilding = Compute.isInBuilding(game, te);
    Building bldg = null;
    if (targetInBuilding) {
      bldg = game.getBoard().getBuildingAt(te.getPosition());
    }
    ToHitData toHit = null;

    // arguments legal?
    if (ae == null || target == null) {
      throw new IllegalArgumentException("Attacker or target not valid");
    }

    // can't target yourself
    if (ae.equals(te)) {
      return new ToHitData(ToHitData.IMPOSSIBLE, "You can't target yourself");
    }

    // non-mechs can't push
    if (!(ae instanceof Mech)) {
      return new ToHitData(ToHitData.IMPOSSIBLE, "Non-mechs can't push");
    }

    // Quads can't push
    if (ae.entityIsQuad()) {
      return new ToHitData(ToHitData.IMPOSSIBLE, "Attacker is a quad");
    }

    // can't make physical attacks while spotting
    if (ae.isSpotting()) {
      return new ToHitData(ToHitData.IMPOSSIBLE, "Attacker is spotting this turn");
    }

    // Can only push mechs
    if (te != null && !(te instanceof Mech)) {
      return new ToHitData(ToHitData.IMPOSSIBLE, "Target is not a mech");
    }

    // Can't push with flipped arms
    if (ae.getArmsFlipped()) {
      return new ToHitData(ToHitData.IMPOSSIBLE, "Arms are flipped to the rear. Can not push.");
    }

    // Can't target a transported entity.
    if (te != null && Entity.NONE != te.getTransportId()) {
      return new ToHitData(ToHitData.IMPOSSIBLE, "Target is a passenger.");
    }

    // Can't target a entity conducting a swarm attack.
    if (te != null && Entity.NONE != te.getSwarmTargetId()) {
      return new ToHitData(ToHitData.IMPOSSIBLE, "Target is swarming a Mek.");
    }

    // check if both arms are present
    if (ae.isLocationBad(Mech.LOC_RARM) || ae.isLocationBad(Mech.LOC_LARM)) {
      return new ToHitData(ToHitData.IMPOSSIBLE, "Arm missing");
    }

    // check if attacker has fired arm-mounted weapons
    if (ae.weaponFiredFrom(Mech.LOC_RARM) || ae.weaponFiredFrom(Mech.LOC_LARM)) {
      return new ToHitData(ToHitData.IMPOSSIBLE, "Weapons fired from arm this turn");
    }

    // check range
    if (ae.getPosition().distance(target.getPosition()) > 1) {
      return new ToHitData(ToHitData.IMPOSSIBLE, "Target not in range");
    }

    // target must be at same elevation
    if (attackerElevation != targetElevation) {
      return new ToHitData(ToHitData.IMPOSSIBLE, "Target not at same elevation");
    }

    // can't push mech making non-pushing displacement attack
    if (te != null && te.hasDisplacementAttack() && !te.isPushing()) {
      return new ToHitData(ToHitData.IMPOSSIBLE, "Target is making a charge/DFA attack");
    }

    // can't push mech pushing another, different mech
    if (te != null && te.isPushing() && te.getDisplacementAttack().getTargetId() != ae.getId()) {
      return new ToHitData(ToHitData.IMPOSSIBLE, "Target is pushing another mech");
    }

    // can't do anything but counter-push if the target of another attack
    if (ae.isTargetOfDisplacementAttack()
        && ae.findTargetedDisplacement().getEntityId() != target.getTargetId()) {
      return new ToHitData(
          ToHitData.IMPOSSIBLE, "Attacker is the target of another push/charge/DFA");
    }

    // can't attack the target of another displacement attack
    if (te != null
        && te.isTargetOfDisplacementAttack()
        && te.findTargetedDisplacement().getEntityId() != ae.getId()) {
      return new ToHitData(ToHitData.IMPOSSIBLE, "Target is the target of another push/charge/DFA");
    }

    // check facing
    if (!target.getPosition().equals(ae.getPosition().translated(ae.getFacing()))) {
      return new ToHitData(ToHitData.IMPOSSIBLE, "Target not directly ahead of feet");
    }

    // can't push while prone
    if (ae.isProne()) {
      return new ToHitData(ToHitData.IMPOSSIBLE, "Attacker is prone");
    }

    // can't push prone mechs
    if (te != null && te.isProne()) {
      return new ToHitData(ToHitData.IMPOSSIBLE, "Target is prone");
    }

    // Can't target units in buildings (from the outside).
    if (targetInBuilding) {
      if (!Compute.isInBuilding(game, ae)) {
        return new ToHitData(ToHitData.IMPOSSIBLE, "Target is inside building");
      } else if (!game.getBoard().getBuildingAt(ae.getPosition()).equals(bldg)) {
        return new ToHitData(ToHitData.IMPOSSIBLE, "Target is inside differnt building");
      }
    }

    // Attacks against adjacent buildings automatically hit.
    if ((target.getTargetType() == Targetable.TYPE_BUILDING)
        || (target.getTargetType() == Targetable.TYPE_FUEL_TANK)) {
      return new ToHitData(
          ToHitData.IMPOSSIBLE,
          "You can not push a building (well, you can, but it won't do anything).");
    }

    // Can't target woods or ignite a building with a physical.
    if (target.getTargetType() == Targetable.TYPE_BLDG_IGNITE
        || target.getTargetType() == Targetable.TYPE_HEX_CLEAR
        || target.getTargetType() == Targetable.TYPE_HEX_IGNITE) {
      return new ToHitData(ToHitData.IMPOSSIBLE, "Invalid attack");
    }

    // Set the base BTH
    int base = 4;

    if (game.getOptions().booleanOption("maxtech_physical_BTH")) {
      base = ae.getCrew().getPiloting() - 1;
    }

    toHit = new ToHitData(base, "base");

    // attacker movement
    toHit.append(Compute.getAttackerMovementModifier(game, attackerId));

    // target movement
    toHit.append(Compute.getTargetMovementModifier(game, targetId));

    // attacker terrain
    toHit.append(Compute.getAttackerTerrainModifier(game, attackerId));

    // target terrain
    toHit.append(Compute.getTargetTerrainModifier(game, te));

    // damaged or missing actuators
    if (!ae.hasWorkingSystem(Mech.ACTUATOR_SHOULDER, Mech.LOC_RARM)) {
      toHit.addModifier(2, "Right Shoulder destroyed");
    }
    if (!ae.hasWorkingSystem(Mech.ACTUATOR_SHOULDER, Mech.LOC_LARM)) {
      toHit.addModifier(2, "Left Shoulder destroyed");
    }

    // water partial cover?
    if (te.height() > 0
        && te.getElevation() == -1
        && targHex.terrainLevel(Terrains.WATER) == te.height()) {
      toHit.addModifier(3, "target has partial cover");
    }

    // target immobile
    toHit.append(Compute.getImmobileMod(te));

    Compute.modifyPhysicalBTHForAdvantages(ae, te, toHit, game);

    toHit.append(nightModifiers(game, target, null, ae));
    // side and elevation shouldn't matter

    // If it has a torso-mounted cockpit and two head sensor hits or three sensor hits...
    // It gets a =4 penalty for being blind!
    if (((Mech) ae).getCockpitType() == Mech.COCKPIT_TORSO_MOUNTED) {
      int sensorHits =
          ae.getBadCriticals(CriticalSlot.TYPE_SYSTEM, Mech.SYSTEM_SENSORS, Mech.LOC_HEAD);
      int sensorHits2 =
          ae.getBadCriticals(CriticalSlot.TYPE_SYSTEM, Mech.SYSTEM_SENSORS, Mech.LOC_CT);
      if ((sensorHits + sensorHits2) == 3) {
        return new ToHitData(
            ToHitData.IMPOSSIBLE, "Sensors Completely Destroyed for Torso-Mounted Cockpit");
      } else if (sensorHits == 2) {
        toHit.addModifier(4, "Head Sensors Destroyed for Torso-Mounted Cockpit");
      }
    }

    // done!
    return toHit;
  }
示例#5
0
  /** To-hit number for a death from above attack, assuming that movement has been handled */
  public static ToHitData toHit(IGame game, int attackerId, Targetable target, Coords src) {
    final Entity ae = game.getEntity(attackerId);

    // arguments legal?
    if (ae == null) {
      throw new IllegalArgumentException("Attacker is null");
    }

    // Do to pretreatment of physical attacks, the target may be null.
    if (target == null) {
      return new ToHitData(TargetRoll.IMPOSSIBLE, "Target is null");
    }

    int targetId = Entity.NONE;
    Entity te = null;
    if (target.getTargetType() == Targetable.TYPE_ENTITY) {
      te = (Entity) target;
      targetId = target.getTargetId();
    } else {
      return new ToHitData(TargetRoll.IMPOSSIBLE, "Invalid Target");
    }

    if (!game.getOptions().booleanOption("friendly_fire")) {
      // a friendly unit can never be the target of a direct attack.
      if ((target.getTargetType() == Targetable.TYPE_ENTITY)
          && ((((Entity) target).getOwnerId() == ae.getOwnerId())
              || ((((Entity) target).getOwner().getTeam() != IPlayer.TEAM_NONE)
                  && (ae.getOwner().getTeam() != IPlayer.TEAM_NONE)
                  && (ae.getOwner().getTeam() == ((Entity) target).getOwner().getTeam())))) {
        return new ToHitData(
            TargetRoll.IMPOSSIBLE, "A friendly unit can never be the target of a direct attack.");
      }
    }

    final boolean targetInBuilding = Compute.isInBuilding(game, te);
    ToHitData toHit = null;

    final int attackerElevation =
        ae.getElevation() + game.getBoard().getHex(ae.getPosition()).getLevel();
    final int targetElevation =
        target.getElevation() + game.getBoard().getHex(target.getPosition()).getLevel();
    final int attackerHeight = attackerElevation + ae.getHeight();

    // check elevation of target flying VTOL
    if (target.isAirborneVTOLorWIGE()) {
      if ((targetElevation - attackerHeight) > ae.getJumpMP()) {
        return new ToHitData(TargetRoll.IMPOSSIBLE, "Elevation difference to high");
      }
    }

    // can't target yourself
    if (ae.equals(te)) {
      return new ToHitData(TargetRoll.IMPOSSIBLE, "You can't target yourself");
    }

    // Infantry CAN'T dfa!!!
    if (ae instanceof Infantry) {
      return new ToHitData(TargetRoll.IMPOSSIBLE, "Infantry can't dfa");
    }

    // Can't target a transported entity.
    if ((Entity.NONE != te.getTransportId())) {
      return new ToHitData(TargetRoll.IMPOSSIBLE, "Target is a passenger.");
    }

    // Can't target a entity conducting a swarm attack.
    if ((Entity.NONE != te.getSwarmTargetId())) {
      return new ToHitData(TargetRoll.IMPOSSIBLE, "Target is swarming a Mek.");
    }

    // check range
    if (src.distance(target.getPosition()) > 1) {
      return new ToHitData(TargetRoll.IMPOSSIBLE, "Target not in range");
    }

    // can't dfa while prone, even if you somehow did manage to jump
    if (ae.isProne()) {
      return new ToHitData(TargetRoll.IMPOSSIBLE, "Attacker is prone");
    }

    // can't attack mech making a different displacement attack
    if (te.hasDisplacementAttack()) {
      return new ToHitData(TargetRoll.IMPOSSIBLE, "Target is already making a charge/DFA attack");
    }

    // can't attack the target of another displacement attack
    if (te.isTargetOfDisplacementAttack()
        && (te.findTargetedDisplacement().getEntityId() != ae.getId())) {
      return new ToHitData(TargetRoll.IMPOSSIBLE, "Target is the target of another charge/DFA");
    }

    // Can't target units in buildings (from the outside).
    if (targetInBuilding) {
      return new ToHitData(TargetRoll.IMPOSSIBLE, "Target is inside building");
    }

    // Attacks against adjacent buildings automatically hit.
    if ((target.getTargetType() == Targetable.TYPE_BUILDING)
        || (target.getTargetType() == Targetable.TYPE_FUEL_TANK)
        || (target instanceof GunEmplacement)) {
      return new ToHitData(TargetRoll.AUTOMATIC_SUCCESS, "Targeting adjacent building.");
    }

    // Can't target woods or ignite a building with a physical.
    if ((target.getTargetType() == Targetable.TYPE_BLDG_IGNITE)
        || (target.getTargetType() == Targetable.TYPE_HEX_CLEAR)
        || (target.getTargetType() == Targetable.TYPE_HEX_IGNITE)) {
      return new ToHitData(TargetRoll.IMPOSSIBLE, "Invalid attack");
    }

    // Set the base BTH
    int base = ae.getCrew().getPiloting();

    toHit = new ToHitData(base, "base");

    // BMR(r), page 33. +3 modifier for DFA on infantry.
    if (te instanceof Infantry) {
      toHit.addModifier(3, "Infantry target");
    }

    // Battle Armor targets are hard for Meks and Tanks to hit.
    if (te instanceof BattleArmor) {
      toHit.addModifier(1, "battle armor target");
    }

    if ((ae instanceof Mech) && ((Mech) ae).isSuperHeavy()) {
      toHit.addModifier(1, "attacker is superheavy mech");
    }

    // attacker movement
    toHit.append(
        Compute.getAttackerMovementModifier(game, attackerId, EntityMovementType.MOVE_JUMP));

    // target movement
    toHit.append(Compute.getTargetMovementModifier(game, targetId));

    // piloting skill differential
    if ((ae.getCrew().getPiloting() != te.getCrew().getPiloting())) {
      toHit.addModifier(
          ae.getCrew().getPiloting() - te.getCrew().getPiloting(), "piloting skill differential");
    }

    // attacker is spotting
    if (ae.isSpotting()) {
      toHit.addModifier(+1, "attacker is spotting");
    }

    // target prone
    if (te.isProne()) {
      toHit.addModifier(-2, "target prone and adjacent");
    }

    // If it has a torso-mounted cockpit and two head sensor hits or three
    // sensor hits...
    // It gets a =4 penalty for being blind!
    if ((ae instanceof Mech) && (((Mech) ae).getCockpitType() == Mech.COCKPIT_TORSO_MOUNTED)) {
      int sensorHits =
          ae.getBadCriticals(CriticalSlot.TYPE_SYSTEM, Mech.SYSTEM_SENSORS, Mech.LOC_HEAD);
      int sensorHits2 =
          ae.getBadCriticals(CriticalSlot.TYPE_SYSTEM, Mech.SYSTEM_SENSORS, Mech.LOC_CT);
      if ((sensorHits + sensorHits2) == 3) {
        return new ToHitData(
            TargetRoll.IMPOSSIBLE, "Sensors Completely Destroyed for Torso-Mounted Cockpit");
      } else if (sensorHits == 2) {
        toHit.addModifier(4, "Head Sensors Destroyed for Torso-Mounted Cockpit");
      }
    }

    // target immobile
    toHit.append(Compute.getImmobileMod(te));

    toHit.append(AbstractAttackAction.nightModifiers(game, target, null, ae, false));

    Compute.modifyPhysicalBTHForAdvantages(ae, te, toHit, game);

    // evading bonuses (
    if (te.isEvading()) {
      toHit.addModifier(te.getEvasionBonus(), "target is evading");
    }

    if (te instanceof Tank) {
      toHit.setSideTable(ToHitData.SIDE_FRONT);
      toHit.setHitTable(ToHitData.HIT_NORMAL);
    } else if (te.isProne()) {
      toHit.setSideTable(ToHitData.SIDE_REAR);
      toHit.setHitTable(ToHitData.HIT_NORMAL);
    } else {
      toHit.setSideTable(te.sideTable(src));
      toHit.setHitTable(ToHitData.HIT_PUNCH);
    }
    // Attacking Weight Class Modifier.
    if (game.getOptions().booleanOption(OptionsConstants.AGM_TAC_OPS_PHYSICAL_ATTACK_PSR)) {
      if (ae.getWeightClass() == EntityWeightClass.WEIGHT_LIGHT) {
        toHit.addModifier(-2, "Weight Class Attack Modifier");
      } else if (ae.getWeightClass() == EntityWeightClass.WEIGHT_MEDIUM) {
        toHit.addModifier(-1, "Weight Class Attack Modifier");
      }
    }

    if ((ae instanceof Mech) && ((Mech) ae).hasIndustrialTSM()) {
      toHit.addModifier(2, "industrial TSM");
    }

    // done!
    return toHit;
  }