简体   繁体   中英

Robocode: precision of onScannedRobot

I wrote a relatively simple AdvancedRobot that turns its radar around and records all enemies with their velocities. Eventually, I noticed that the robot misses in cases where it's not supposed to miss. I copied the code from Robocode/Graphical Debugging wiki and tested that. Here is the code (Wiki is currently down):

// The coordinates of the last scanned robot
int scannedX = Integer.MIN_VALUE;
int scannedY = Integer.MIN_VALUE;

// Called when we have scanned a robot
public void onScannedRobot(ScannedRobotEvent e) {
    // Calculate the angle to the scanned robot
    double angle = Math.toRadians((getHeading() + e.getBearing()) % 360);

    // Calculate the coordinates of the robot
    scannedX = (int)(getX() + Math.sin(angle) * e.getDistance());
    scannedY = (int)(getY() + Math.cos(angle) * e.getDistance());
}

And the event handler:

// Paint a transparent square on top of the last scanned robot
public void onPaint(Graphics2D g) {
    // Set the paint color to a red half transparent color
    g.setColor(new Color(0xff, 0x00, 0x00, 0x80));

    // Draw a line from our robot to the scanned robot
    g.drawLine(scannedX, scannedY, (int)getX(), (int)getY());

    // Draw a filled square on top of the scanned robot that covers it
    g.fillRect(scannedX - 20, scannedY - 20, 40, 40);
}

The "filled square" is definitely NOT on top of the robot. A couple of screenshots are shown below. It looks like the precision depends on the distance, but I'm not sure. Is this expected, or am I doing something wrong?

第 1 枪 射击 2

One reason this may happen is that delivery of the onScannedRobot event is delayed until higher priority events have finished processing. In particular, if a higher priority event handler executes a command to rotate the body, that command will execute before onScannedRobot is called, causing time to advance, robots to move, and your robot's heading to be altered.

Since deferred event delivery causes a variety of problems, I recommend to never execute commands in event handlers. Instead, event handlers should simply inspect, think about, and store information in fields for the main loop to react to. This enables the main loop to look at all available information before committing to a course of action, and to intelligently select which action is most appropriate given the totality of information received. For instance, it could look at a robot hit event on the front combined with a radar detection of an opponent approaching from the rear, and decide that a sideways evasion is more promising than the usual evasion to the rear...

I think I know what the issue is. It does not seem to be documented; if I am wrong, please post a link. ScannedRobotEvent reports bearing relative to previous heading of the robot, ie, its heading when the last StatusEvent hit. Taking this into account greatly improved my bot's accuracy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM