Difference between revisions of "Radar"

From Robowiki
Jump to navigation Jump to search
(Removing turn skip paragraph (it was incorrect anyway, see talk for details).)
(Its great your making videos, but try to only link on the most relevant pages. Like these are one-on-one only, and are on that page. So they don't need to be here.)
(22 intermediate revisions by 9 users not shown)
Line 1: Line 1:
The radar is one of the most vital components of your robot. Without it, [[targeting]] is effectively impossible and [[movement]] is purely random. Just as with movement and targeting, there are many simple and complex algorithms for radar control. In most robots the radar takes up the smallest portion of code.
+
'''Radar''' is one of the most vital components of your robot. Without it, [[targeting]] is effectively impossible and [[movement]] is purely random. Just as with movement and targeting, there are many simple and complex algorithms for radar control. In most robots the radar takes up the smallest portion of code.
 
 
  
 +
[[Image:Radar.jpg|thumb|right|300px|Radar in [[Robocode]]]]
  
 
== Technical Information ==
 
== Technical Information ==
 
A radar in Robocode can turn a maximum of 45° or &pi;/4<sup>rad</sup> in a single tick. The radar scans robots up to 1200 units away. The angle that the radar rotates between two ticks creates what is called a radar arc, and every robot detected within the arc is sent to the <code>onScannedRobot()</code> method in order of distance from the scanning bot. The closest bot is detected first, while the furthest bot is detected last. By default, the <code>onScannedRobot()</code> method has the lowest [[event priority]] of all the event handlers in Robocode, so it is the last one to be triggered each tick.
 
A radar in Robocode can turn a maximum of 45° or &pi;/4<sup>rad</sup> in a single tick. The radar scans robots up to 1200 units away. The angle that the radar rotates between two ticks creates what is called a radar arc, and every robot detected within the arc is sent to the <code>onScannedRobot()</code> method in order of distance from the scanning bot. The closest bot is detected first, while the furthest bot is detected last. By default, the <code>onScannedRobot()</code> method has the lowest [[event priority]] of all the event handlers in Robocode, so it is the last one to be triggered each tick.
 +
 +
== Initial Scan Direction ==
 +
The optimal direction to scan at the beginning of the round is generally considered to be the one with the shortest rotational distance to the angle to the center of the field. However there is likely many robots that have a more complex initial scan setup. Such variations include rotating the gun and robot to get a larger scan arc to find the enemy faster.
  
 
== 1-vs-1 Radars ==
 
== 1-vs-1 Radars ==
1-vs-1 radars are the smallest of the bunch and many can get a scan in every turn, producing a perfect lock.
+
: ''Main article: [[One on One Radar]]
 
+
One on one radars are the smallest of the bunch and many can get a scan in every turn, producing a perfect lock. The most common types of radar in [[One on One|one on one]] are:
=== Spinning radar ===
 
A simple spin of the radar, this is very ineffective in one on one, but still used in [[NanoBot]]s.
 
 
 
Here is an example of this type of radar:
 
<pre>
 
public void run() {
 
    // ...
 
 
 
    do {
 
        turnRadarRightRadians(Double.POSITIVE_INFINITY);
 
    } while (true);
 
}
 
</pre>
 
 
 
=== The infinity lock ===
 
The infinity lock is the simplest radar lock and it is used frequently in [[NanoBots]]. It has the disadvantage of "slipping" and losing its lock frequently. But is a much better alternative to the Spinning radar in one on one combat.
 
 
 
Here is an example of this type of radar:
 
<pre>
 
public void run() {
 
    // ...
 
    turnRadarRightRadians(Double.POSITIVE_INFINITY);
 
}
 
 
 
public void onScannedRobot(ScannedRobotEvent e) {
 
    // ...
 
    setTurnRadarLeftRadians(getRadarTurnRemainingRadians());
 
}
 
</pre>
 
 
 
=== Perfect radar locks ===
 
There are several "perfect" radar locks that will not slip once they have a lock.
 
 
 
==== Narrow lock ====
 
Point the radar at the enemy's last known location. This results in a thin beam which follows the enemy around the battlefield. In most cases the enemy rarely moves entirely outside the arc of the radar beam within a single turn. This fails only in extreme cases, and radar lock can occasionally be lost. There are ways to correct the problem of losing the radar lock, such as increasing the arc scanned by multiplying it by a factor (as described below). Many recent one on one bots use this type of radar. This radar lock is similar to, and considered an enhancement on the wide radar lock described below.
 
 
 
Here is an example of this kind of radar:
 
<pre>
 
import robocode.util.Utils;
 
 
 
public void run() {
 
    // ...
 
 
 
    turnRadarRightRadians(Double.POSITIVE_INFINITY);
 
    do {
 
        // Check for new targets
 
        scan();
 
    } while (true);
 
}
 
 
 
public void onScannedRobot(ScannedRobotEvent e) {
 
    double radarTurn =
 
        // Absolute bearing to target
 
        getHeadingRadians() + e.getBearingRadians()
 
        // Subtract current radar heading to get turn required
 
        - getRadarHeadingRadians();
 
 
 
    setTurnRadarRightRadians(Utils.normalRelativeAngle(radarTurn));
 
 
 
    // ...
 
}
 
</pre>
 
 
 
The problem of the odd slippage can be resolved by multiplying the sweep angle by a factor, eg. factor*Utils.normalRelativeAngle(radarTurn)
 
* 1.0 - Normal Thin Radar Lock.
 
* 1.9 - Radar arc starts wide and slowly narrows to be enough to stay on target.
 
* 2.0 - Radar arc continues to sweep somewhat wide, to avoid any kind of loss. Most used corrective factor.
 
 
 
==== Wide lock ====
 
The wide radar locks are used to avoid the rare slipping of the narrow lock. They scan a wider portion of the battlefield and will not slip. This type of lock is generally the widest of the one on one radars.
 
Here is an example of this type of radar:
 
<pre>
 
import robocode.util.Utils;
 
 
 
public void run() {
 
    // ...
 
 
 
    while(true) {
 
        turnRadarRightRadians(Double.POSITIVE_INFINITY);
 
    }
 
}
 
 
 
public void onScannedRobot(ScannedRobotEvent e) {
 
    double absoluteBearing = getHeadingRadians() + e.getBearingRadians();
 
    double radarTurn = Utils.normalRelativeAngle(absoluteBearing - getRadarHeadingRadians());
 
 
 
    // Width of the bot, plus twice the arc it can move in a tick, limit it to the max turn
 
    double arcToScan = Math.min(Math.atan(36.0 / e.getDistance()), PI/4.0);
 
  
    // We want to sent the radar even further in the direction it's moving
+
* Spinning radar
    radarTurn += (radarTurn < 0) ? -arcToScan : arcToScan;
+
* The Infinity Lock
    setTurnRadarRightRadians(radarTurn);
+
* Perfect Radar Locks
 
+
:* Turn Multiplier Lock
    // ...
+
:* Width Lock
}
 
</pre>
 
  
 
== Melee radars ==
 
== Melee radars ==
Melee radars are more complex and take up considerable more room inside a robot. Since the field of opponents does not usually fall within a 45° area, compromises must be made between frequent data of one bot (e.g., the firing target) and consistently updated data of all bots.
+
: ''Main article: [[Melee Radar]]
 
+
Melee radars are more complex and take up considerably more room inside a robot. Since the field of opponents does not usually fall within a 45° area, compromises must be made between frequent data of one bot (e.g., the firing target) and consistently updated data of all bots. Common melee radars include:
=== Spinning radar ===
+
* Spinning Radar
Just as with one on one, there is the generic spinning radar. This is the most used melee radar as it is by far the easiest to implement.
+
* Oldest Scanned Radar
 
+
* Gun Heat Lock  
<pre>
 
public void run() {
 
    // ...
 
 
 
    while(true) {
 
        turnRadarRightRadians(Double.POSITIVE_INFINITY);
 
    }
 
}
 
</pre>
 
 
 
==== Corner Arc ====
 
A variation on the spinning radar, if the robot is in a corner it scans back and forth across the 90 degree arc away from the corner, as not to waste time scanning where there cannot be any robots. Typically, this is used only in smaller melee robots, which do not ahve room for a more complicated radar system. Melee bots with room tend to use one of the implementations below.
 
 
 
=== Oldest Scanned ===
 
This type of melee radar spins towards the robot it hasn't seen in the longest amount of time. Here is an example of this type of radar.
 
<pre>
 
import java.util.*;
 
import robocode.*;
 
import robocode.util.*;
 
 
 
// ... Within robot class
 
 
 
static LinkedHashMap<String, Double> enemyHashMap;
 
static double scanDir;
 
static Object sought;
 
 
 
public void run() {
 
    scanDir = 1;
 
    enemyHashMap = new LinkedHashMap<String, Double>(5, 2, true);
 
 
 
    // ...
 
 
 
    while(true) {
 
        setTurnRadarRightRadians(scanDir * Double.POSITIVE_INFINITY);
 
        scan();
 
    }
 
}
 
 
 
public void onRobotDeath(RobotDeathEvent e) {
 
    enemyHashMap.remove(e.getName());
 
    sought = null;
 
}
 
 
 
public void onScannedRobot(ScannedRobotEvent e) {
 
    String name = e.getName();
 
    LinkedHashMap<String, Double> ehm = enemyHashMap;
 
 
 
    ehm.put(name, getHeadingRadians() + e.getBearingRadians());
 
 
 
    if ((name == sought || sought == null) && ehm.size() == getOthers()) {
 
scanDir = Utils.normalRelativeAngle(ehm.values().iterator().next()
 
            - getRadarHeadingRadians());
 
        sought = ehm.keySet().iterator().next();
 
    }
 
 
 
    // ...
 
}
 
</pre>
 
With above code, the radar will spin passed every bot, and then reverse it's direction until passed all bots and repeat again. if spin take more than 4 ticks, Radar will continue spinning like spinning radar above. This kind of radar are use in many top bots include Shadow and Phoenix. I prefer you to use this kind of radar unless you need a melee radar lock or your targeting system require you to have enemy scan every 8 ticks.
 
 
 
=== Gun Heat Lock ===
 
A technique developed by [[Paul Evans]]/[[Kawagi]] for [[SandboxDT]]/[[FloodHT]]. This radar locks onto a target when the robot has low gunheat. Normally this is best for melee robots which use GuessFactor Targeting.
 
<pre>
 
 
 
public void run() {
 
    // ...
 
    setTurnRadarRightRadians(Double.POSITIVE_INFINITY);
 
}
 
 
 
public void onScannedRobot(ScannedRobotEvent e) {
 
    // ... your own target selection
 
    double absoluteBearing = getHeadingRadians() + e.getBearingRadians();
 
    if (isCurrentTarget && getGunHeat() < 0.5) { // Lock for 5 ticks
 
        setTurnRadarRightRadians(3.5 * Utils.normalRelativeAngle(absoluteBearing - getRadarHeadingRadians()));
 
    else
 
        setTurnRadarRightRadians(Double.POSITIVE_INFINITY);
 
}
 
</pre>
 
The code above uses a Narrow Lock but the simpler Infinity Lock works just as well. For a narrow lock, it is recommended you multiply by a large factor to continue scanning other robots while locked.
 
 
 
 
== Notes ==
 
== Notes ==
 
For most of these radar locks, you will need to add one of the following to your <code>run()</code> method:
 
For most of these radar locks, you will need to add one of the following to your <code>run()</code> method:
  
<pre>
+
<syntaxhighlight>
 
setAdjustRadarForRobotTurn(true);
 
setAdjustRadarForRobotTurn(true);
 
setAdjustGunForRobotTurn(true);
 
setAdjustGunForRobotTurn(true);
 
setAdjustRadarForGunTurn(true);
 
setAdjustRadarForGunTurn(true);
</pre>
+
</syntaxhighlight>
  
 
[[Category:Code Snippets]]
 
[[Category:Code Snippets]]
[[th:เรดาร์]]
 

Revision as of 08:03, 15 October 2012

Radar is one of the most vital components of your robot. Without it, targeting is effectively impossible and movement is purely random. Just as with movement and targeting, there are many simple and complex algorithms for radar control. In most robots the radar takes up the smallest portion of code.

Radar in Robocode

Technical Information

A radar in Robocode can turn a maximum of 45° or π/4rad in a single tick. The radar scans robots up to 1200 units away. The angle that the radar rotates between two ticks creates what is called a radar arc, and every robot detected within the arc is sent to the onScannedRobot() method in order of distance from the scanning bot. The closest bot is detected first, while the furthest bot is detected last. By default, the onScannedRobot() method has the lowest event priority of all the event handlers in Robocode, so it is the last one to be triggered each tick.

Initial Scan Direction

The optimal direction to scan at the beginning of the round is generally considered to be the one with the shortest rotational distance to the angle to the center of the field. However there is likely many robots that have a more complex initial scan setup. Such variations include rotating the gun and robot to get a larger scan arc to find the enemy faster.

1-vs-1 Radars

Main article: One on One Radar

One on one radars are the smallest of the bunch and many can get a scan in every turn, producing a perfect lock. The most common types of radar in one on one are:

  • Spinning radar
  • The Infinity Lock
  • Perfect Radar Locks
  • Turn Multiplier Lock
  • Width Lock

Melee radars

Main article: Melee Radar

Melee radars are more complex and take up considerably more room inside a robot. Since the field of opponents does not usually fall within a 45° area, compromises must be made between frequent data of one bot (e.g., the firing target) and consistently updated data of all bots. Common melee radars include:

  • Spinning Radar
  • Oldest Scanned Radar
  • Gun Heat Lock

Notes

For most of these radar locks, you will need to add one of the following to your run() method:

setAdjustRadarForRobotTurn(true);
setAdjustGunForRobotTurn(true);
setAdjustRadarForGunTurn(true);