/** * The possible fire control types. * If you're adding a new one, add it here then make sure to add it to Princess.InitializeFireControls */ public enum FireControlType { Basic, Infantry }
String getDebugDescription() { return getWeapon().getName() + " P. Hit: " + LOG_PER.format(getProbabilityToHit()) + ", Max Dam: " + LOG_DEC.format(getMaxDamage()) + ", Exp. Dam: " + LOG_DEC.format(getExpectedDamageOnHit()) + ", Num Crits: " + LOG_DEC.format(getExpectedCriticals()) + ", Kill Prob: " + LOG_PER.format(getKillProbability()); } }
/** * @return The amount of damage based on the damage of each weapon and their odds of hitting. */ synchronized double getExpectedDamage() { double expectedDamage = 0; for (WeaponFireInfo weaponFireInfo : this) { expectedDamage += weaponFireInfo.getExpectedDamageOnHit() * weaponFireInfo.getProbabilityToHit(); } return expectedDamage; }
new StringBuilder("Initializing Damage for ").append(getShooter().getDisplayName()) .append(" firing ").append(getWeapon().getDesc()) .append(" at ").append(getTarget().getDisplayName()) .append(":"); setAction(buildWeaponAttackAction()); setAction(buildBombAttackAction(bombPayload)); setToHit(calcRealToHit(getWeaponAttackAction())); } else if (null != shooterPath) { setToHit(calcToHit(shooterPath, assumeUnderFlightPath)); } else { setToHit(calcToHit()); if (12 < getToHit().getValue()) { owner.log(getClass(), METHOD_NAME, LogLevel.DEBUG, msg.append("\n\tImpossible toHit: ") .append(getToHit().getValue()).toString()); setProbabilityToHit(0); setMaxDamage(0); setHeat(0); setExpectedCriticals(0); setKillProbability(0); setExpectedDamageOnHit(0); return; if (getShooterState().hasNaturalAptGun()) { msg.append("\n\tAttacker has Natural Aptitude Gunnery");
WeaponFireInfo testWeaponFireInfo = Mockito.spy(new WeaponFireInfo(mockPrincess)); testWeaponFireInfo.setShooter(mockShooter); testWeaponFireInfo.setShooterState(mockShooterState); testWeaponFireInfo.setTarget(mockTarget); testWeaponFireInfo.setTargetState(mockTargetState); testWeaponFireInfo.setWeapon(mockWeapon); testWeaponFireInfo.setGame(mockGame); double expectedCriticals = 0.02460; double expectedKill = 0; Mockito.doReturn(mockToHitSix).when(testWeaponFireInfo).calcToHit(); Mockito.doReturn(mockWeaponAttackAction).when(testWeaponFireInfo).buildWeaponAttackAction(); Mockito.doReturn(expectedMaxDamage).when(testWeaponFireInfo).computeExpectedDamage(); testWeaponFireInfo.initDamage(null, false, true, null); TestCase.assertEquals(expectedMaxDamage, testWeaponFireInfo.getMaxDamage()); TestCase.assertEquals(expectedMaxDamage, testWeaponFireInfo.getExpectedDamageOnHit()); TestCase.assertEquals(expectedProbabilityToHit, testWeaponFireInfo.getProbabilityToHit(), DELTA); TestCase.assertEquals(expectedCriticals, testWeaponFireInfo.getExpectedCriticals(), DELTA); TestCase.assertEquals(expectedKill, testWeaponFireInfo.getKillProbability(), DELTA); expectedCriticals = 0.01867; expectedKill = 0.01155; Mockito.doReturn(mockToHitEight).when(testWeaponFireInfo).calcToHit(); Mockito.doReturn(mockWeaponAttackAction).when(testWeaponFireInfo).buildWeaponAttackAction(); Mockito.doReturn(expectedMaxDamage).when(testWeaponFireInfo).computeExpectedDamage(); testWeaponFireInfo.initDamage(null, false, true, null); TestCase.assertEquals(expectedMaxDamage, testWeaponFireInfo.getMaxDamage()); TestCase.assertEquals(expectedMaxDamage, testWeaponFireInfo.getExpectedDamageOnHit()); TestCase.assertEquals(expectedProbabilityToHit, testWeaponFireInfo.getProbabilityToHit(), DELTA);
Mockito.when(mockPPCFireInfo.getProbabilityToHit()).thenReturn(0.6); Mockito.when(mockPPCFireInfo.getHeat()).thenReturn(10); Mockito.when(mockPPCFireInfo.getExpectedDamageOnHit()).thenReturn(10.0); Mockito.when(mockPPCFireInfo.getExpectedCriticals()).thenReturn(0.46); Mockito.when(mockPPCFireInfo.getKillProbability()).thenReturn(0.002); Mockito.when(mockPPCFireInfo.getWeapon()).thenReturn(mockPPC); Mockito.when(mockPPCFireInfo.getShooter()).thenReturn(mockShooter); Mockito.when(mockPPCFireInfo.getDebugDescription()).thenReturn("mock PPC"); alphaStrike.add(mockPPCFireInfo); Mockito.when(mockMLFireInfo.getProbabilityToHit()).thenReturn(0.6); Mockito.when(mockMLFireInfo.getHeat()).thenReturn(3); Mockito.when(mockMLFireInfo.getExpectedDamageOnHit()).thenReturn(5.0); Mockito.when(mockMLFireInfo.getExpectedCriticals()).thenReturn(0.0); Mockito.when(mockMLFireInfo.getKillProbability()).thenReturn(0.0); Mockito.when(mockMLFireInfo.getWeapon()).thenReturn(mockML); Mockito.when(mockMLFireInfo.getShooter()).thenReturn(mockShooter); Mockito.when(mockMLFireInfo.getDebugDescription()).thenReturn("mock ML"); alphaStrike.add(mockMLFireInfo); Mockito.when(mockLRMFireInfo.getProbabilityToHit()).thenReturn(0.6); Mockito.when(mockLRMFireInfo.getHeat()).thenReturn(1); Mockito.when(mockLRMFireInfo.getExpectedDamageOnHit()).thenReturn(3.0); Mockito.when(mockLRMFireInfo.getExpectedCriticals()).thenReturn(0.0); Mockito.when(mockLRMFireInfo.getKillProbability()).thenReturn(0.0); Mockito.when(mockLRMFireInfo.getWeapon()).thenReturn(mockLRM5); Mockito.when(mockLRMFireInfo.getShooter()).thenReturn(mockShooter); Mockito.when(mockLRMFireInfo.getDebugDescription()).thenReturn("mock LRM"); alphaStrike.add(mockLRMFireInfo);
WeaponFireInfo wfi = new WeaponFireInfo(shooter, hexTarget, currentWeapon, game, false, owner); if(wfi.getProbabilityToHit() > 0) { damageValue *= wfi.getProbabilityToHit(); ArtilleryAttackAction aaa = (ArtilleryAttackAction) actualFireInfo.buildWeaponAttackAction(); int ammoID = findAmmo(shooter, currentWeapon, game); aaa.setAmmoId(ammoID); actualFireInfo.setAction(aaa); retval.add(actualFireInfo); retval.setUtility(retval.getUtility() + maxDamage); retval.setUtility(retval.getUtility() + tagInfo.getProbabilityToHit());
/** * Test to make sure that Princess will choose a FiringPlan that shots at * a MechWarrior, instead of choosing to do nothing. */ @Test public void testCalcFiringPlansAtMechwarrior() { mockTarget = Mockito.mock(MechWarrior.class); Mockito.when(mockPPCFireInfo.getProbabilityToHit()).thenReturn(0.6); Mockito.when(mockPPCFireInfo.getHeat()).thenReturn(10); Mockito.when(mockPPCFireInfo.getExpectedDamageOnHit()).thenReturn(10.0); Mockito.when(mockMLFireInfo.getProbabilityToHit()).thenReturn(0.6); Mockito.when(mockMLFireInfo.getHeat()).thenReturn(3); Mockito.when(mockMLFireInfo.getExpectedDamageOnHit()).thenReturn(5.0); Mockito.when(mockLRMFireInfo.getProbabilityToHit()).thenReturn(0.6); Mockito.when(mockLRMFireInfo.getHeat()).thenReturn(1); Mockito.when(mockLRMFireInfo.getExpectedDamageOnHit()).thenReturn(3.0); Mockito.doReturn(0.0).when(testFireControl).calcDamageAllocationUtility(Mockito.any(Targetable.class), Mockito.anyDouble()); Mockito.when(mockShooter.getPosition()).thenReturn(mockShooterCoords); Mockito.when(mockTarget.getPosition()).thenReturn(mockTargetCoords); Mockito.when(mockShooter.getWeaponList()).thenReturn(shooterWeapons); final FiringPlan plan = testFireControl.getBestFiringPlan(mockShooter, mockTarget, mockGame, testToHitThreshold); Assert.assertFalse("Expected not 0.0. Got " + plan.getUtility(), 0.00001 > Math.abs(0 - plan.getUtility())); } }
if (0 < shoot.getProbabilityToHit() && myPlan.getHeat() + shoot.getHeat() + shooter.getHeat() <= shooter.getHeatCapacity() && 0 < shoot.getExpectedDamage()) { myPlan.add(shoot);
/** * Worker function that calculates the shooter's "best" actions that result in a TAG being fired. * @param shooter * @param game * @param owner * @return */ private WeaponFireInfo getTAGInfo(Mounted weapon, Entity shooter, IGame game, Princess owner) { WeaponFireInfo retval = null; double hitOdds = 0.0; // pretty simple logic here: take the best shot that you have for(Targetable target : FireControl.getAllTargetableEnemyEntities(owner.getLocalPlayer(), game, owner.getFireControlState())) { WeaponFireInfo wfi = new WeaponFireInfo(shooter, target, weapon, game, false, owner); if(wfi.getProbabilityToHit() > hitOdds) { hitOdds = wfi.getProbabilityToHit(); retval = wfi; } } return retval; }
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(); }
if ((weaponFireInfo.getWeapon().getType()).getInternalName().equals(Infantry.LEG_ATTACK)) { legAttack.add(weaponFireInfo); continue; } else if ((weaponFireInfo.getWeapon().getType()).getInternalName().equals(Infantry.SWARM_MEK)) { swarmAttack.add(weaponFireInfo); continue; else if ((weaponFireInfo.getWeapon().getType()) instanceof StopSwarmAttack) { continue; } else if (!(shooter instanceof BattleArmor) && Infantry.LOC_FIELD_GUNS == weaponFireInfo.getWeapon() .getLocation()) { final double fieldGunMass = weaponFireInfo.getWeapon().getType().getTonnage(shooter); if (0 == weaponFireInfo.getHeat()) { bestPlans[0].add(weaponFireInfo); } else { final int leftoverHeatCapacity = heatLevel - weaponFireInfo.getHeat(); !bestPlans[leftoverHeatCapacity].containsWeapon(weaponFireInfo.getWeapon())) { testPlan.addAll(bestPlans[heatLevel - weaponFireInfo.getHeat()]); testPlan.add(weaponFireInfo); calculateUtility(testPlan, heatTolerance, isAero);
final WeaponFireInfo guessInfo = new WeaponFireInfo(shooter, new EntityState(shooter), target, true, owner); final WeaponFireInfo accurateInfo = new WeaponFireInfo(shooter, target, weapon, game, false, owner); if (guessInfo.getToHit().getValue() != accurateInfo.getToHit().getValue()) { ret += "Incorrect To Hit prediction, weapon " + weapon.getName() + " (" + shooter.getChassis() + " vs " + target.getDisplayName() + ")" + ":\n"; ret += " Guess: " + Integer.toString(guessInfo.getToHit().getValue()) + " " + guessInfo.getToHit().getDesc() + "\n"; ret += " Real: " + Integer.toString(accurateInfo.getToHit().getValue()) + " " + accurateInfo.getToHit().getDesc() + "\n";
/** * @return The total number of expected critical hits based on the chance to hit, damage to target, toughness of * target and odds of rolling a successful crit check. This is in the units of critical hits. */ synchronized double getExpectedCriticals() { double expectedCriticals = 0; for (WeaponFireInfo weaponFireInfo : this) { expectedCriticals += weaponFireInfo.getExpectedCriticals(); } return expectedCriticals; }
/** * @return The total heat for all weapons being fired with this plan. */ synchronized int getHeat() { int heat = 0; for (WeaponFireInfo weaponFireInfo : this) { heat += weaponFireInfo.getHeat(); } return heat; }
/** * Models the probability of each individual weapon getting a kill shot. * We treat each weapon shot as a Bernoulli trial and compute the probiblity * of the target surviving each shot. We can then take 1 - surviveChance to * get the chance of getting a kill. This model doesn't take into * consideration multiple weapons hitting the same location. * * @return The odds of getting a kill based on the odds of each individual * weapon getting a kill. The result will be between 0 and 1. */ synchronized double getKillProbability() { double surviveProbability = 1; for (WeaponFireInfo weaponFireInfo : this) { surviveProbability *= 1 - weaponFireInfo.getKillProbability(); } return 1 - surviveProbability; }
Mockito.when(mockPPCFireInfo.getExpectedDamage()).thenReturn(10.0); Mockito.when(mockLRMFireInfo.getExpectedDamage()).thenReturn(5.0); expected = new FiringPlan(mockTarget); expected.add(mockPPCFireInfo);
public double getExpectedDamage() { return getProbabilityToHit() * getExpectedDamageOnHit(); }
@Test public void testGetExpectedCriticals() { Mockito.when(mockWeaponFireInfoMG.getExpectedCriticals()).thenReturn(0.0); Mockito.when(mockWeaponFireInfoPPC.getExpectedCriticals()).thenReturn(0.423); Mockito.when(mockWeaponFireInfoERML.getExpectedCriticals()).thenReturn(0.015); double expected = 0 + 0.423 + 0.015; Assert.assertEquals(expected, testFiringPlan.getExpectedCriticals(), TOLERANCE); }