int maxHeat = alphaStrike.getHeat(); if (0 > maxHeat) { maxHeat = 0; final Targetable target = alphaStrike.getTarget(); bestPlans[0] = new FiringPlan(target, alphaStrike.getFlipArms()); final FiringPlan nonZeroHeatOptions = new FiringPlan(target); final FiringPlan swarmAttack = new FiringPlan(target); final FiringPlan legAttack = new FiringPlan(target); final FiringPlan fieldGuns = new FiringPlan(target); double fieldGunMassAlreadyFired = 0.0; //We need to track the tonnage of field guns being fired, because trying to fire more than the current possible total(# of men left) results in nothing being fired. for (final WeaponFireInfo weaponFireInfo : alphaStrike) { legAttack.add(weaponFireInfo); continue; } else if ((weaponFireInfo.getWeapon().getType()).getInternalName().equals(Infantry.SWARM_MEK)) { swarmAttack.add(weaponFireInfo); continue; fieldGuns.add(weaponFireInfo); fieldGunMassAlreadyFired += fieldGunMass; bestPlans[0].add(weaponFireInfo); } else { nonZeroHeatOptions.add(weaponFireInfo); bestPlans[heatLevel] = new FiringPlan(target);
@Test public void testGetEntityActionVector() { Vector<EntityAction> expected; // Test a no-twist plan. Mockito.when(testFiringPlan.getTwist()).thenReturn(0); expected = new Vector<>(3); expected.add(mockWeaponAttackActionMG); expected.add(mockWeaponAttackActionPPC); expected.add(mockWeaponAttackActionERML); Assert.assertEquals(expected, testFiringPlan.getEntityActionVector()); // todo Test torso-twists. // Test an empty firing plan. //noinspection MismatchedQueryAndUpdateOfCollection FiringPlan emptyPlan = new FiringPlan(mockTarget); expected = new Vector<>(0); Assert.assertEquals(expected, emptyPlan.getEntityActionVector()); }
final boolean shooterIsAero) { int overheat = 0; if (firingPlan.getHeat() > overheatTolerance) { overheat = firingPlan.getHeat() - overheatTolerance; modifier += calcCommandUtility(firingPlan.getTarget()); modifier += calcStrategicBuildingTargetUtility(firingPlan.getTarget()); modifier += calcPriorityUnitTargetUtility(firingPlan.getTarget()); double expectedDamage = firingPlan.getExpectedDamage(); double utility = 0; utility += DAMAGE_UTILITY * expectedDamage; utility += CRITICAL_UTILITY * firingPlan.getExpectedCriticals(); utility += KILL_UTILITY * firingPlan.getKillProbability(); utility *= calcTargetPotentialDamageMultiplier(firingPlan.getTarget()); utility += TARGET_HP_FRACTION_DEALT_UTILITY * calcDamageAllocationUtility(firingPlan.getTarget(), expectedDamage); utility -= calcCivilianTargetDisutility(firingPlan.getTarget()); utility *= modifier; utility -= (shooterIsAero ? OVERHEAT_DISUTILITY_AERO : OVERHEAT_DISUTILITY) * overheat; utility -= (firingPlan.getTarget() instanceof MechWarrior) ? EJECTED_PILOT_DISUTILITY : 0; firingPlan.setUtility(utility);
@Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof FiringPlan)) return false; if (!super.equals(o)) return false; FiringPlan that = (FiringPlan) o; final double TOLERANCE = 0.00001; if (twist != that.twist) return false; if (Math.abs(utility - that.utility) > TOLERANCE) return false; if (!target.equals(that.target)) return false; if (getHeat() != that.getHeat()) return false; if (Math.abs(getKillProbability() - that.getKillProbability()) > TOLERANCE) return false; if (Math.abs(getExpectedCriticals() - that.getExpectedCriticals()) > TOLERANCE) return false; //noinspection RedundantIfStatement if (Math.abs(getExpectedDamage() - that.getExpectedDamage()) > TOLERANCE) return false; return true; }
String getDebugDescription(boolean detailed) { if (size() == 0) { return "Empty FiringPlan!"; } StringBuilder description = new StringBuilder("Firing Plan for ").append(get(0).getShooter().getChassis()) .append(" at ") .append(getTarget().getDisplayName()) .append("; ").append(Integer.toString(size())) .append(" weapons fired "); if (detailed) { for (WeaponFireInfo weaponFireInfo : this) { description.append("\n\t\t").append(weaponFireInfo.getDebugDescription()); } } DecimalFormat decimalFormat = new DecimalFormat("0.00000"); description.append("\n\tTotal Expected Damage=").append(decimalFormat.format(getExpectedDamage())); description.append("\n\tTotal Expected Criticals=").append(decimalFormat.format(getExpectedCriticals())); description.append("\n\tKill Probability=").append(decimalFormat.format(getKillProbability())); description.append("\n\tUtility=").append(decimalFormat.format(getUtility())); return description.toString(); }
FiringPlan testFiringPlan = Mockito.spy(new FiringPlan(mockTarget)); Mockito.doReturn(15.0).when(testFiringPlan).getExpectedDamage(); Mockito.doReturn(0.46129).when(testFiringPlan).getExpectedCriticals(); Mockito.doReturn(0.02005).when(testFiringPlan).getKillProbability(); Mockito.doReturn(0).when(testFiringPlan).getHeat(); Mockito.doReturn(0.0).when(testFireControl).calcDamageAllocationUtility(Mockito.any(Targetable.class), Mockito.anyDouble()); testFireControl.calculateUtility(testFiringPlan, overheatTolerance, false); Assert.assertEquals(baseUtility, testFiringPlan.getUtility(), TOLERANCE); testFiringPlan = Mockito.spy(new FiringPlan(mockTarget)); Mockito.doReturn(15.0).when(testFiringPlan).getExpectedDamage(); Mockito.doReturn(0.46129).when(testFiringPlan).getExpectedCriticals(); Mockito.doReturn(0.02005).when(testFiringPlan).getKillProbability(); Mockito.doReturn(0).when(testFiringPlan).getHeat(); Mockito.doReturn(0.0).when(testFireControl).calcDamageAllocationUtility(Mockito.any(Targetable.class), Mockito.anyDouble()); testFireControl.calculateUtility(testFiringPlan, overheatTolerance, false); Assert.assertEquals(baseUtility * (1 + FireControl.COMMANDER_UTILITY), testFiringPlan.getUtility(), TOLERANCE); Mockito.when(mockTarget.isCommander()).thenReturn(false); testFiringPlan = Mockito.spy(new FiringPlan(mockTarget)); Mockito.doReturn(15.0).when(testFiringPlan).getExpectedDamage(); Mockito.doReturn(0.46129).when(testFiringPlan).getExpectedCriticals(); Mockito.doReturn(0.02005).when(testFiringPlan).getKillProbability(); Mockito.doReturn(0).when(testFiringPlan).getHeat(); Mockito.doReturn(0.0).when(testFireControl).calcDamageAllocationUtility(Mockito.any(Targetable.class), Mockito.anyDouble()); testFireControl.calculateUtility(testFiringPlan, overheatTolerance, false); Assert.assertEquals(baseUtility * (1 + FireControl.SUB_COMMANDER_UTILITY), testFiringPlan.getUtility(), TOLERANCE); Mockito.when(mockTarget.hasC3()).thenReturn(false);
FiringPlan retval = new FiringPlan(); retval.setTwist(facingChange); int originalFacing = shooter.getSecondaryFacing(); shooter.setSecondaryFacing(FireControl.correctFacing(originalFacing + facingChange)); aaa.setAmmoId(ammoID); actualFireInfo.setAction(aaa); retval.add(actualFireInfo); retval.setUtility(retval.getUtility() + maxDamage); owner.sendAmmoChange(shooter.getId(), shooter.getEquipmentNum(currentWeapon), ammoID); retval.add(tagInfo); retval.setUtility(retval.getUtility() + tagInfo.getProbabilityToHit());
game, ammoConservation); if ((null != plan) && (plan.getExpectedDamage() > 0)) { getFireControl(shooter).loadAmmo(shooter, plan); plan.sortPlan(); shooter.getDisplayName() + " - Best Firing Plan: " + plan.getDebugDescription(LogLevel.DEBUG == getVerbosity())); final Integer targetId = plan.getTarget().getTargetId(); final Double newDamage = damageMap.get(targetId) + plan.getExpectedDamage(); damageMap.replace(targetId,newDamage); sendAttackData(shooter.getId(), plan.getEntityActionVector()); return; } else {
@Before public void setUp() { mockTarget = Mockito.mock(Targetable.class); testFiringPlan = Mockito.spy(new FiringPlan(mockTarget)); mockWeaponFireInfoMG = Mockito.mock(WeaponFireInfo.class); testFiringPlan.add(mockWeaponFireInfoMG); Mounted mockMG = Mockito.mock(Mounted.class); Mockito.when(mockWeaponFireInfoMG.getWeapon()).thenReturn(mockMG); mockWeaponAttackActionMG = Mockito.mock(WeaponAttackAction.class); Mockito.when(mockWeaponFireInfoMG.getWeaponAttackAction()).thenReturn(mockWeaponAttackActionMG); mockWeaponFireInfoPPC = Mockito.mock(WeaponFireInfo.class); testFiringPlan.add(mockWeaponFireInfoPPC); mockPPC = Mockito.mock(Mounted.class); Mockito.when(mockWeaponFireInfoPPC.getWeapon()).thenReturn(mockPPC); mockWeaponAttackActionPPC = Mockito.mock(WeaponAttackAction.class); Mockito.when(mockWeaponFireInfoPPC.getWeaponAttackAction()).thenReturn(mockWeaponAttackActionPPC); mockWeaponFireInfoERML = Mockito.mock(WeaponFireInfo.class); testFiringPlan.add(mockWeaponFireInfoERML); Mounted mockERML = Mockito.mock(Mounted.class); Mockito.when(mockWeaponFireInfoERML.getWeapon()).thenReturn(mockERML); mockWeaponAttackActionERML = Mockito.mock(WeaponAttackAction.class); Mockito.when(mockWeaponFireInfoERML.getWeaponAttackAction()).thenReturn(mockWeaponAttackActionERML); }
FiringPlan testPlan = Mockito.spy(new FiringPlan(mockTarget)); testPlan.add(mockInfoLBX); testPlan.add(mockInfoLRM); testPlan.add(mockInfoLL); testPlan.add(mockInfoSRM); FiringPlan expectedOrder = new FiringPlan(mockTarget); expectedOrder.add(mockInfoLL); expectedOrder.add(mockInfoSRM); expectedOrder.add(mockInfoLBX); expectedOrder.add(mockInfoLRM); testPlan.sortPlan(); Assert.assertEquals("\nExpected: " + expectedOrder.getWeaponNames() + "\nActual : " + testPlan .getWeaponNames(), expectedOrder, testPlan); testPlan.clear(); testPlan.add(mockInfoLBX); testPlan.add(mockInfoLRM); testPlan.add(mockInfoLL); testPlan.add(mockInfoSRM); expectedOrder = new FiringPlan(mockTarget); expectedOrder.add(mockInfoLBX); expectedOrder.add(mockInfoLL); expectedOrder.add(mockInfoSRM); expectedOrder.add(mockInfoLRM); testPlan.sortPlan(); Assert.assertEquals("\nExpected: " + expectedOrder.getWeaponNames() + "\nActual : " + testPlan .getWeaponNames(),
return new FiringPlan(target); final FiringPlan myPlan = new FiringPlan(target); myPlan.getHeat() + shoot.getHeat() + shooter.getHeat() <= shooter.getHeatCapacity() && 0 < shoot.getExpectedDamage()) { myPlan.add(shoot); if(myPlan.getUtility() >= bombPlan.getUtility()) { return myPlan; } else {
FiringPlan betaStrike = guessFullFiringPlan(shooter, shooterState, target, targetState, game); betaStrike.setFlipArms(true); if(betaStrike.getUtility() > alphaStrike.getUtility()) { alphaStrike = betaStrike; if (alphaStrike.getHeat() <= maxHeat && !(shooter instanceof Infantry)) { return alphaStrike; FiringPlan bestPlan = new FiringPlan(target); if ((bestPlan.getUtility() < firingPlan.getUtility())) { bestPlan = firingPlan;
final NumberFormat DECF = new DecimalFormat("0.000"); final FiringPlan myPlan = new FiringPlan(target); final WeaponFireInfo shoot = buildWeaponFireInfo(shooter, target, weapon, game, false); if ((shoot.getProbabilityToHit() > toHitThreshold)) { myPlan.add(shoot); continue; if((bombingPlan.size() > 0) && (bombingPlan.getUtility() > myPlan.getUtility())) { return bombingPlan;
@Test public void testCalcFiringPlansUnderHeat() { final FiringPlan alphaStrike = new FiringPlan(mockTarget); Mockito.when(mockPPCFireInfo.getShooter()).thenReturn(mockShooter); Mockito.when(mockPPCFireInfo.getDebugDescription()).thenReturn("mock PPC"); alphaStrike.add(mockPPCFireInfo); Mockito.when(mockMLFireInfo.getShooter()).thenReturn(mockShooter); Mockito.when(mockMLFireInfo.getDebugDescription()).thenReturn("mock ML"); alphaStrike.add(mockMLFireInfo); Mockito.when(mockLRMFireInfo.getShooter()).thenReturn(mockShooter); Mockito.when(mockLRMFireInfo.getDebugDescription()).thenReturn("mock LRM"); alphaStrike.add(mockLRMFireInfo); Mockito.when(mockMGFireInfo.getShooter()).thenReturn(mockShooter); Mockito.when(mockMGFireInfo.getDebugDescription()).thenReturn("mock MG"); alphaStrike.add(mockMGFireInfo); expected[0] = new FiringPlan(mockTarget); expected[0].add(mockMGFireInfo); expected[0].setUtility(1.2); expected[1] = new FiringPlan(mockTarget); expected[1].add(mockMGFireInfo); expected[1].add(mockLRMFireInfo); expected[1].setUtility(3.0); expected[2] = new FiringPlan(mockTarget); expected[2].add(mockMGFireInfo); expected[2].add(mockLRMFireInfo);
final FiringPlan myPlan = new FiringPlan(target); myPlan.add(shoot); calculateUtility(bombingPlan, DOES_NOT_TRACK_HEAT, true); // bomb drops never cause heat if(bombingPlan.getUtility() > myPlan.getUtility()) { return bombingPlan;
private FiringPlan getBestFiringPlanUnderHeat(final Targetable target, final Entity shooter, final FiringPlan[] allPlans) { // Determine the best plan taking into account our heat tolerance. FiringPlan bestPlan = new FiringPlan(target); final boolean isAero = shooter.isAero(); final int heatTolerance = calcHeatTolerance(shooter, isAero); calculateUtility(bestPlan, heatTolerance, isAero); for (final FiringPlan firingPlan : allPlans) { calculateUtility(firingPlan, heatTolerance, isAero); if ((bestPlan.getUtility() < firingPlan.getUtility())) { bestPlan = firingPlan; } } return bestPlan; }
/** * Builds a {@link Vector} of all the actions, {@link EntityAction}, that make up this firing plan. * * @return The list of actions as a vector. */ synchronized Vector<EntityAction> getEntityActionVector() { Vector<EntityAction> actionVector = new Vector<>(); if (size() == 0) { return actionVector; } if(getTwist() != 0) { actionVector.add(new TorsoTwistAction(get(0).getShooter().getId(), FireControl.correctFacing(get(0).getShooter().getFacing() + getTwist()))); } if(flipArms) { actionVector.addElement(new FlipArmsAction(get(0).getShooter().getId(), flipArms)); } for (WeaponFireInfo weaponFireInfo : this) { actionVector.add(weaponFireInfo.getWeaponAttackAction()); } return actionVector; }
/** * Worker method that calculates a point blank shot action vector given a firing entity ID and a target ID. * * @param firingEntityID the ID of the entity taking the point blank shot * @param targetID the ID of the entity being shot at potentially */ protected Vector<EntityAction> calculatePointBlankShot(int firingEntityID, int targetID) { Entity shooter = getGame().getEntity(firingEntityID); Targetable target = getGame().getEntity(targetID); final FiringPlanCalculationParameters fccp = new FiringPlanCalculationParameters.Builder().buildExact(shooter, target, calcAmmoConservation(shooter)); FiringPlan plan = getFireControl(shooter).determineBestFiringPlan(fccp); getFireControl(shooter).loadAmmo(shooter, plan); plan.sortPlan(); return plan.getEntityActionVector(); }
.getDisplayName() + " - Best Firing Plan: " + plan.getDebugDescription(true)); if ((null == bestPlan) || (plan.getUtility() > bestPlan.getUtility())) { bestPlan = plan;
private void assertArrayEquals(final FiringPlan[] expected, final Object actual) { Assert.assertNotNull(actual); Assert.assertTrue("actual: " + actual.getClass().getName(), actual instanceof FiringPlan[]); final FiringPlan[] actualArray = (FiringPlan[]) actual; Assert.assertEquals(expected.length, actualArray.length); final StringBuilder failure = new StringBuilder(); for (int i = 0; i < expected.length; i++) { if ((null == expected[i]) && (null != actualArray[i])) { failure.append("\nExpected[").append(i).append("]: null"); failure.append("\nActual[").append(i).append("]: ").append(actualArray[i].getDebugDescription(true)); continue; } if (!expected[i].equals(actualArray[i])) { failure.append("\nExpected[").append(i).append("]: ").append(expected[i].getDebugDescription(true)); if (null == actualArray[i]) { failure.append("\nActual[").append(i).append("]: null"); } else { failure.append("\nActual[").append(i).append("]: ").append(actualArray[i].getDebugDescription (true)); } } } if (!StringUtil.isNullOrEmpty(failure.toString())) { Assert.fail(failure.toString()); } }