Re: Need help getting the OnScannedRobot method to be called

791 views
Skip to first unread message

fnl

unread,
Nov 28, 2012, 5:21:35 PM11/28/12
to robo...@googlegroups.com
Hi Red Raven,

The onScannedRobot method is (only) called every time your robot's radar scans another robot, meaning that another robot is within the scan arc of the radar - not at every turn.

I have compiled your robot. Thanks for posting it, which makes is easier to study the issue. Your robot is not turning the radar that often. You should turn your radar to scan for enemies. When the radar detects the enemy, onScannedRobot will be called.
This is much easier to debug if you simply turn on the visible scan arc in Robocode. You do this from the menu by selecting Options -> Preferences -> View Options and then put a check mark in "Visible Scan Arc". This way you'll be able to see on-screen what your robot can see. :-)
Please notice, that the visible scan arc draws the "radar sweep" - meaning the delta movement from one scan angle to the next. Hence, when your radar does not move, it will draw a thin line, when the radar sweep is very little. Notice that your robot will only be able to scan other robots within this scan arc/sweep, so you need to turn your radar up to a maximum of 45 degrees to detect opponents in a broader angle. :-)

I hope this makes sense? :-)

Cheers,
- Flemming

Den tirsdag den 27. november 2012 21.35.08 UTC+1 skrev Red_Raven:
I'm a beginner taking AP Comp Sci (Java), and we're practicing with Robocode ATM. I noticed my robot could easily beat the Crazy example, but it rarely fired so the fights were long. I put a print method in the OnScannedRobot method to debug it because that method tells my robot to fire. Sure enough, the print statements in the run method are called all the time, but those in my OnScannedRobot method rarely print. I think the OnScannedRobot  method it called before every movement, so why does my robot rarely call it? Here's my code:

package DL;
import robocode.*;
import static robocode.util.Utils.normalRelativeAngleDegrees;
import java.util.Random;
//import java.awt.Color;


/**
 * Spaz - a robot by Drew Langston
 */
public class Spaz extends Robot
{
/**
* run: Spaz's default behavior
*/
public void run() {
// Initialization of the robot should be put here
setAdjustGunForRobotTurn(true);
Random ranGen = new Random();
String move = null;
int move1 = 0;
boolean wallClose = false;
// Robot main loop
while(true) {
               //These if statements let the robot know if it's close to a wall.
if (getX() < 20){
wallClose = true;
if (0 <= getHeading() && getHeading() <= 90){
turnRight(45);
ahead(50);
}
else{
turnLeft(45);
ahead(50);
}
}
if (getBattleFieldWidth() - getX() < 20){
wallClose = true;
if (180 <= getHeading() && getHeading() <= 270){
turnRight(45);
ahead(50);
}
else{
turnLeft(45);
ahead(50);
}
}
if (getY() < 20){
wallClose = true;
if (180 <= getHeading() && getHeading() <= 270){
turnLeft(45);
ahead(50);
}
else{
turnRight(45);
ahead(50);
}
}
if (getBattleFieldHeight() - getY() < 20){
wallClose = true;
if ( 0 <= getHeading() && getHeading() <= 90){
turnLeft(45);
ahead(50);
}
else{
turnRight(45);
ahead(50);
}
}
//generate random number and shorten it to a practical value for moving and turning.
move = Integer.toString(Math.abs(ranGen.nextInt())).substring(0, 3);
move1 = Integer.parseInt(move);

System.out.println(" (Run Loop) Ahead");
ahead(move1);

if(wallClose == false){
System.out.println("(Run loop) Turn, Back, turn");
turnRight(move1 / 4);
back(move1);
turnLeft(move1 / 4);
}
}
}

/**
* onScannedRobot: What to do when you see another robot
*/
public void onScannedRobot(ScannedRobotEvent e) {
// Replace the next line with any behavior you would like
System.out.println("Scan activated");
double turnGunAmt = normalRelativeAngleDegrees(e.getBearing() + (getHeading() - getRadarHeading()));

if(e.getDistance() < 200  && getEnergy() > 50)
{
System.out.println("Fire 3, " + turnGunAmt + " " + e.getDistance());
turnGunRight(turnGunAmt);
fire(3);
}
else if (e.getDistance() < 300 && getEnergy() > 50)
{
System.out.println("Fire 2, " + turnGunAmt + " " + e.getDistance());
turnGunRight(turnGunAmt);
fire(2);
}
else
{
System.out.println("Too far, " + turnGunAmt + " " + e.getDistance());
turnRight(turnGunAmt);
ahead(200);
scan();
}
scan();
}

/**
* onHitByBullet: What to do when you're hit by a bullet
*/
public void onHitByBullet(HitByBulletEvent e) {
System.out.println("Hit by bullet. Evasive manuvers");
turnRight(20);
ahead(10);
turnRight(20);
ahead(20);
turnRight(30);
ahead(20);
}
/**
* onHitWall: What to do when you hit a wall
*/
public void onHitWall(HitWallEvent e) {
// Replace the next line with any behavior you would like
System.out.println("Hit wall. Turning.");
turnRight(90);
ahead(50);
}

Red_Raven

unread,
Dec 4, 2012, 5:56:04 PM12/4/12
to robo...@googlegroups.com

Thank you so much! I didn't even know the radar had to be turned, I thought it was omnidirectional. I don't have time to integrate that function ATM, but I switched on the radar view and sure enough it rarely moved. You've been very helpful.

fnl

unread,
Dec 5, 2012, 5:07:32 PM12/5/12
to robo...@googlegroups.com
You are welcome. :-)

Also make sure to check out the RoboWiki which contains lots of useful information about how to code robots for Robocode. :-)

Cheers,
- Flemming

dseker...@isev.k12.tr

unread,
Nov 20, 2015, 4:36:52 AM11/20/15
to robocode


fnl I tested your robot with my robot cyclops and it only moved didn't fired!! but in the program it says fire if(getEnergy() > 50){
fire(2);
}
Can you tell why it don't fires 

This si my robot cyclops

package legends;
import robocode.*;

import java.awt.Color;
import robocode.AdvancedRobot;
import robocode.ScannedRobotEvent;
import static robocode.util.Utils.normalRelativeAngleDegrees;
 * Cyclop - a robot by (your name here)
 */
public class Cyclop extends AdvancedRobot
{
double energyEnemy = 100;
double directionToMove = 1;
boolean rightAngle = false;
double turningTime = 1;
/**
* run: Cyclop's default behavior
*/
public void run() {
// Initialization of the robot should be put here
setAdjustGunForRobotTurn(true);
turnGunRight(90);
// After trying out your robot, try uncommenting the import at the top,
// and the next line:

// setColors(Color.red,Color.blue,Color.green); // body,gun,radar
setGunColor(new Color(255, 255, 255));
setBulletColor(new Color(255, 0, 0));
setBodyColor(new Color(220, 160, 122));
setRadarColor(new Color(0, 0, 0));
// Robot main loop
while(true) {
// Replace the next 4 lines with any behavior you would like
turnGunRight(20);
}
}

/**
* onScannedRobot: What to do when you see another robot
*/
public void onScannedRobot(ScannedRobotEvent e) {
// Replace the next line with any behavior you would like
boolean systemUse = true;
double absoluteBearing = getHeading() + e.getBearing();
double bearingFromGun = normalRelativeAngleDegrees(absoluteBearing - getGunHeading());
double distance = e.getDistance();
if(distance > 600){
setTurnRight(e.getBearing() + 90 - 30 * directionToMove);
systemUse = false;
}
if(distance > 800){
fire(1);
}else if(distance > 500){
fire(2);
}else if(distance > 200){
fire(3);
}else{
fire(3.5);
}
turnGunRight(bearingFromGun);
if(energyEnemy - e.getEnergy() > 0){
if(energyEnemy - e.getEnergy() < 5){
directionToMove = -directionToMove;
setAhead((distance / 4 + 25) * directionToMove);
}
} energyEnemy = e.getEnergy();
scan();
if(systemUse == true){
turnRight(e.getBearing() + 90);
}
}

/**
* onHitByBullet: What to do when you're hit by a bullet
*/
public void onHitByBullet(HitByBulletEvent e) {
// Replace the next line with any behavior you would like
setAhead(75);
}
/**
* onHitWall: What to do when you hit a wall
*/
public void onHitWall(HitWallEvent e) {
// Replace the next line with any behavior you would like
back(200);
ahead(100);
}
}
//A robot that made by Doruk...
Reply all
Reply to author
Forward
0 new messages