Difference between revisions of "Random Targeting"

From Robowiki
Jump to navigation Jump to search
(→‎Example: A simpler implementation)
Line 2: Line 2:
  
 
== Example ==
 
== Example ==
 
 
<syntaxhighlight>
 
<syntaxhighlight>
// Add import robocode.util.* for Utils
 
// This code goes in your onScannedRobot() event handler.
 
 
 
public void onScannedRobot(ScannedRobotEvent e) {
 
public void onScannedRobot(ScannedRobotEvent e) {
    double randomGuessFactor = (Math.random() - .5) * 2;
+
// ...
    double bulletPower = 3;
+
double targetAngle = getHeadingRadians() + e.getBearingRadians();
    double maxEscapeAngle = Math.asin(8.0/(20 - (3 * bulletPower)));
 
    double firingAngle = randomGuessFactor * maxEscapeAngle;
 
    double absBearingToEnemy = e.getBearingRadians() + getHeadingRadians();
 
 
 
    setTurnGunRightRadians(Utils.normalRelativeAngle(
+
double bulletPower = Math.max(0.1,Math.random() * 3.0);
            absBearingToEnemy + firingAngle - getGunHeadingRadians()));
+
double escapeAngle = Math.asin(8 / Rules.getBulletSpeed(bulletPower));
    fire(bulletPower);
+
double randomAimOffset = -escapeAngle + Math.random() * 2 * escapeAngle;
 +
 
 +
double headOnTargeting = targetAngle - getGunHeadingRadians();
 +
setTurnGunRightRadians(Utils.normalRelativeAngle(headOnTargeting + randomAimOffset));
 +
setFire(bulletPower);
 +
// ...
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 05:59, 6 February 2013

A method of targeting that simply chooses a random angle among the angles that could possibly hit the opponent. Some successful NanoBots use this firing method. Its implementation is very small and for unpredictable movements, it will give a consistent hit percentage.

Example

public void onScannedRobot(ScannedRobotEvent e) {
	// ...
	double targetAngle = getHeadingRadians() + e.getBearingRadians();
	
	double bulletPower = Math.max(0.1,Math.random() * 3.0);
	double escapeAngle = Math.asin(8 / Rules.getBulletSpeed(bulletPower));
	double randomAimOffset = -escapeAngle + Math.random() * 2 * escapeAngle;

	double headOnTargeting = targetAngle - getGunHeadingRadians();
	setTurnGunRightRadians(Utils.normalRelativeAngle(headOnTargeting + randomAimOffset));
	setFire(bulletPower);
	// ...
}

A simpler solution

A simpler method is to assume that the enemy is traveling in a circle around you (see Musashi Trick), which is often true among NanoBots and 1-vs-1 bots. If the enemy is traveling in a circle around you, the maximum distance it can cover before a bullet reaches it is enemy velocity / bullet velocity (in radians). For example, a power 3.0 bullet fired at an enemy going at full speed should be fired at a bearing offset between -8/11 and +8/11.

Selecting firepower

The advantage of a random gun is that it should have a roughly equal hit rate on any type of movement. This makes ideal firepower selection pretty easy to pre-calculate. In the following equations x is the choice of firepower. It is assumed that damage output per tick is the quantity you're interested in maximizing, it may not be.

Bullet damage, assuming firepower > 1:

<math>D=4x+2(x-1)</math>


The smallest size of a robot, in radians:

<math>s=\frac{36}{d}</math>, where <math>d</math> is the distance to the other bot.


The total escape area, in radians:

<math>\alpha=2 \cdot asin(\frac{8}{20-3x})</math>


Probability of a hit, assuming uniform spread of bullets over <math>\alpha</math>:

<math>P=min(1,\frac{s}{\alpha})</math>


The expected damage from a shot:

<math>E=DP</math>


The heat created by a shot:

<math>H=1+\frac{x}{5}</math>


The firing frequency:

<math>F=\frac{1}{ceil(10H)}</math>


Expected damage <math>E_{x,d}</math> per tick of combat:

<math>E_{x,d}=E \cdot F = D \cdot P \cdot \frac{1}{ceil(10H)} =\frac{(6x-2) \cdot min(1,\frac{16}{d \cdot asin(\frac{8}{20-3x})})}{10+ceil(2x))}</math>

Optimization of <math>E_{x,d}</math> is left as an exercise for the coder.

See also