throw new IllegalStateException("Attacker is null");
}
// Due 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();
}
if (!game.getOptions().booleanOption("friendly_fire")) {
// a friendly unit can never be the target of a direct attack.
if (!skid && (target.getTargetType() == Targetable.TYPE_ENTITY)
&& ((((Entity)target).getOwnerId() == ae.getOwnerId())
|| ((((Entity)target).getOwner().getTeam() != Player.TEAM_NONE)
&& (ae.getOwner().getTeam() != Player.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.");
}
}
IHex attHex = game.getBoard().getHex(src);
IHex targHex = game.getBoard().getHex(target.getPosition());
final int attackerElevation = elevation + attHex.getElevation();
final int attackerHeight = attackerElevation + ae.height();
final int targetElevation = target.getElevation() + targHex.getElevation();
final int targetHeight = targetElevation + target.getHeight();
Building bldg = game.getBoard().getBuildingAt(getTargetPos());
ToHitData toHit = null;
boolean targIsBuilding = ((getTargetType() == Targetable.TYPE_FUEL_TANK) || (getTargetType() == Targetable.TYPE_BUILDING));
boolean inSameBuilding = Compute.isInSameBuilding(game, ae, te);
// can't target yourself
if (ae.equals(te)) {
return new ToHitData(TargetRoll.IMPOSSIBLE, "You can't target yourself");
}
// Can't target a transported entity.
if ((te != null) && (Entity.NONE != te.getTransportId())) {
return new ToHitData(TargetRoll.IMPOSSIBLE, "Target is a passenger.");
}
// Can't target a entity conducting a swarm attack.
if ((te != null) && (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");
}
// mechs can only charge standing mechs
if ((ae instanceof Mech) && !skid) {
if ((te != null) && !(te instanceof Mech)) {
return new ToHitData(TargetRoll.IMPOSSIBLE, "Target is not a mech");
}
if ((te != null) && te.isProne()) {
return new ToHitData(TargetRoll.IMPOSSIBLE, "Target is prone");
}
} else if (te instanceof Infantry) {
// Can't charge infantry.
return new ToHitData(TargetRoll.IMPOSSIBLE, "Target is infantry");
} else if (te instanceof Protomech) {
// Can't charge protomechs.
return new ToHitData(TargetRoll.IMPOSSIBLE, "Target is protomech");
}
// target must be within 1 elevation level
if ((attackerElevation > targetHeight) || (attackerHeight < targetElevation)) {
return new ToHitData(TargetRoll.IMPOSSIBLE, "Target must be within 1 elevation level");
}
// can't attack mech making a different displacement attack
if ((te != null) && te.hasDisplacementAttack()) {
return new ToHitData(TargetRoll.IMPOSSIBLE, "Target is already making a charge/DFA attack");
}
// target must have moved already, unless it's a skid charge
if ((te != null) && !te.isDone() && !skid) {
return new ToHitData(TargetRoll.IMPOSSIBLE, "Target must be done with movement");
}
// can't attack the target of another displacement attack
if ((te != null) && 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 ((null != bldg) && (!targIsBuilding) && (te != null) && Compute.isInBuilding(game, te)) {
if (!Compute.isInBuilding(game, ae)) {
return new ToHitData(TargetRoll.IMPOSSIBLE, "Target is inside building");
} else if (!game.getBoard().getBuildingAt(ae.getPosition()).equals(bldg)) {
return new ToHitData(TargetRoll.IMPOSSIBLE, "Target is inside differnt 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");
// attacker movement
toHit.append(Compute.getAttackerMovementModifier(game, ae.getId(), movement));
// target movement
toHit.append(Compute.getTargetMovementModifier(game, targetId));
// attacker terrain
toHit.append(Compute.getAttackerTerrainModifier(game, ae.getId()));
// target terrain
toHit.append(Compute.getTargetTerrainModifier(game, te, 0, inSameBuilding));
// attacker is spotting
if (ae.isSpotting()) {
toHit.addModifier(+1, "attacker is spotting");
}
if (te != null) {
// piloting skill differential
if (ae.getCrew().getPiloting() != te.getCrew().getPiloting()) {
toHit.addModifier(ae.getCrew().getPiloting() - te.getCrew().getPiloting(), "piloting skill differential");
}
// target prone
if (te.isProne()) {
toHit.addModifier(-2, "target prone and adjacent");
}
// water partial cover?
if ((te.height() > 0) && (te.getElevation() == -1) && (targHex.terrainLevel(Terrains.WATER) == te.height())) {
toHit.addModifier(1, "target has partial cover");
}
}
// 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));
// skids have a penalty for unintentional charge
if (skid) {
toHit.addModifier(3, "unintentional charge");
}
Compute.modifyPhysicalBTHForAdvantages(ae, te, toHit, game);
//evading bonuses (
if(te.isEvading()) {
toHit.addModifier(te.getEvasionBonus(), "target is evading");
}
// determine hit direction
toHit.setSideTable(te.sideTable(src));
// all charges resolved against full-body table, except vehicles
// and charges against mechs in water partial cover
if ((targHex.terrainLevel(Terrains.WATER) == te.height()) && (te.getElevation() == -1) && (te.height() > 0)) {
toHit.setHitTable(ToHitData.HIT_PUNCH);
} else if (ae.getHeight() < target.getHeight()) {
toHit.setHitTable(ToHitData.HIT_KICK);
} else {
toHit.setHitTable(ToHitData.HIT_NORMAL);
}
//Attacking Weight Class Modifier.
if ( game.getOptions().booleanOption("tacops_attack_physical_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;
}