// Handle escape of transported units.
Enumeration<Entity> transporter = entity.getLoadedUnits().elements();
if (transporter.hasMoreElements()) {
Entity other = null;
Coords curPos = entity.getPosition();
IHex entityHex = game.getBoard().getHex(curPos);
int curFacing = entity.getFacing();
while (transporter.hasMoreElements()) {
other = transporter.nextElement();
// Can the other unit survive?
boolean survived = false;
if (entity instanceof Tank) {
if ((entity.getMovementMode() == IEntityMovementMode.NAVAL)
|| (entity.getMovementMode() == IEntityMovementMode.HYDROFOIL)) {
if (other.getMovementMode() == IEntityMovementMode.INF_UMU) {
survived = Compute.d6() <= 3;
} else if (other.getMovementMode() == IEntityMovementMode.INF_JUMP) {
survived = Compute.d6() == 1;
} else if (other.getMovementMode() == IEntityMovementMode.VTOL) {
survived = Compute.d6() <= 2;
}
} else if (entity.getMovementMode() == IEntityMovementMode.SUBMARINE) {
if (other.getMovementMode() == IEntityMovementMode.INF_UMU) {
survived = Compute.d6() == 1;
}
} else {
survived = Compute.d6() <= 4;
}
}
if (!survivable || (externalUnits.contains(other) && !survived)) {
// Nope.
other.setDestroyed(true);
game.moveToGraveyard(other.getId());
entityUpdate(other.getId());
send(createRemoveEntityPacket(other.getId(), condition));
r = new Report(6370);
r.subject = other.getId();
r.addDesc(other);
vDesc.addElement(r);
}
// Can we unload the unit to the current hex?
// TODO : unloading into stacking violation is not
// explicitly prohibited in the BMRr.
else if ((null != Compute.stackingViolation(game, other.getId(), curPos)) || other.isHexProhibited(entityHex)) {
// Nope.
other.setDestroyed(true);
game.moveToGraveyard(other.getId());
entityUpdate(other.getId());
send(createRemoveEntityPacket(other.getId(), condition));
r = new Report(6375);
r.subject = other.getId();
r.addDesc(other);
vDesc.addElement(r);
} // End can-not-unload
else {
// The other unit survives.
unloadUnit(entity, other, curPos, curFacing, entity.getElevation());
}
} // Handle the next transported unit.
} // End has-transported-unit
// Handle transporting unit.
if (Entity.NONE != entity.getTransportId()) {
final Entity transport = game.getEntity(entity.getTransportId());
Coords curPos = transport.getPosition();
int curFacing = transport.getFacing();
unloadUnit(transport, entity, curPos, curFacing, transport.getElevation());
entityUpdate(transport.getId());
//if this is the last fighter in a fighter squadron then remove the squadron
if((transport instanceof FighterSquadron) && (((FighterSquadron)transport).getFighters().size() < 1)) {
transport.setDestroyed(true);
game.moveToGraveyard(transport.getId());
entityUpdate(transport.getId());
send(createRemoveEntityPacket(transport.getId(), condition));
r = new Report(6365);
r.subject = transport.getId();
r.addDesc(transport);
r.add("fighter destruction");
vDesc.addElement(r);
}
} // End unit-is-transported
// Is this unit being swarmed?
final int swarmerId = entity.getSwarmAttackerId();
if (Entity.NONE != swarmerId) {
final Entity swarmer = game.getEntity(swarmerId);
swarmer.setSwarmTargetId(Entity.NONE);
// a unit that stopped swarming due to the swarmed unit dieing
// should be able to move
swarmer.setUnloaded(false);
entity.setSwarmAttackerId(Entity.NONE);
Report.addNewline(vDesc);
r = new Report(6380);
r.subject = swarmerId;
r.addDesc(swarmer);
vDesc.addElement(r);
if ((entity instanceof Mech) && !entity.isProne()) {
// Swarming infantry take a 2d6 point hit.
// ASSUMPTION : damage should not be doubled.
r = new Report(2335);
r.subject = swarmer.getId();
r.addDesc(swarmer);
vDesc.add(r);
vDesc.addAll(damageEntity(swarmer, swarmer.rollHitLocation(ToHitData.HIT_NORMAL, ToHitData.SIDE_FRONT), Compute.d6(2)));
Report.addNewline(vPhaseReport);
}
entityUpdate(swarmerId);
}
// Is this unit swarming somebody?
final int swarmedId = entity.getSwarmTargetId();
if (Entity.NONE != swarmedId) {
final Entity swarmed = game.getEntity(swarmedId);
swarmed.setSwarmAttackerId(Entity.NONE);
entity.setSwarmTargetId(Entity.NONE);
r = new Report(6385);
r.subject = swarmed.getId();
r.addDesc(swarmed);
vDesc.addElement(r);
entityUpdate(swarmedId);
}
// If in a grapple, release both mechs
if (entity.getGrappled() != Entity.NONE) {
int grappler = entity.getGrappled();
entity.setGrappled(Entity.NONE, false);
Entity e = game.getEntity(grappler);
if (e != null) {
e.setGrappled(Entity.NONE, false);
}
entityUpdate(grappler);
}
} // End entity-not-already-destroyed.
//if using battlefield wreckage rules, then the destruction of this unit
//might convert the hex to rough
Coords curPos = entity.getPosition();
IHex entityHex = game.getBoard().getHex(curPos);
if(game.getOptions().booleanOption("tacops_battle_wreck")
&& (entityHex != null) && game.getBoard().onGround()
&& !((entity instanceof Infantry) || (entity instanceof Protomech))) {
//large support vees will create ultra rough, otherwise rough
if(entity instanceof LargeSupportTank) {
if(entityHex.terrainLevel(Terrains.ROUGH) < 2) {
entityHex.addTerrain(Terrains.getTerrainFactory().createTerrain(Terrains.ROUGH, 2));
sendChangedHex(curPos);
}
} else if ( (entity.getWeight() >= 40) && !entityHex.containsTerrain(Terrains.ROUGH)) {
entityHex.addTerrain(Terrains.getTerrainFactory().createTerrain(Terrains.ROUGH, 1));
sendChangedHex(curPos);
}
}