Random Targeting

From Robowiki
Revision as of 22:10, 10 May 2010 by Bostonvaulter (talk | contribs) (→‎A simpler solution: added link to musashi trick)
Jump to navigation Jump to search

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

// Add import robocode.util.* for Utils
// This code goes in your onScannedRobot() event handler.

public void onScannedRobot(ScannedRobotEvent e) {
    double randomGuessFactor = (Math.random() - .5) * 2;
    double bulletPower = 3;
    double maxEscapeAngle = Math.asin(8.0/(20 - (3 * bulletPower)));
    double firingAngle = randomGuessFactor * maxEscapeAngle;
    double absBearingToEnemy = e.getBearingRadians() + getHeadingRadians();
	
    setTurnGunRightRadians(Utils.normalRelativeAngle(
            absBearingToEnemy + firingAngle - getGunHeadingRadians()));
    fire(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:

D=4*x+2*(x-1)

The smallest size of a robot, in radians:

S=36/distance

The total escape area, in radians:

A=2*asin(8/(20-3*x))

Probability of a hit, assuming uniform spread of bullets over A:

P=min(1,S/A)

The expected damage from a shot:

E=D*P

The heat created by a shot:

H=1+x/5

The firing frequency:

F=1/ceil(H/0.1)

Expected damage per tick of combat:

DPS=E*F

Back substitution to get the expected damage per tick in terms of distance and x and optimization of DPS are left as an exercise for the coder.

See also