Discussion:
Robocode Robot Help
Matt
2009-11-18 17:22:07 UTC
Permalink
Okay, well, I'm working on a robot in robocode. I'm using the example gotobot found here: http://old.robowiki.net/robowiki?Movement/GoToBot

I have edited it so that I have the movement patterns that I want. I'm in the process of trying to use a simple linear targeting fire system. The linear targeting code I'm trying to use can be found here: http://robowiki.net/wiki/Linear_Targeting

However, when I replace the old firing system with the linear targeting system, my robot will randomly quit working out of no where. As in, the robot will work for a second or a minute then just freeze in place with no movement, no radar spin, no firing, and no gun turn. I'm not sure what is causing this.

Aside from that, I'm using a movement pattern that is proportional to the battlefield size. My problem with this (I have comments in the code itself about it) is I want to use the getBattleFieldHeight/Width rather than just inputing the numbers like I do now. When I use the getBattleField method though, the robot won't move. So if anyone knows how I can fix this as well I would really appreciate it.

Here's the robot:





package ml;
import robocode.*;
import java.awt.geom.Point2D;
import robocode.HitByBulletEvent;
import robocode.Robot;
import robocode.ScannedRobotEvent;
import java.awt.Color;
import java.awt.*;
import java.awt.geom.*;
import static robocode.util.Utils.normalRelativeAngleDegrees;
import robocode.util.*;

public class MattRobot extends AdvancedRobot { //public class opening bracket

//put the height of the current battlefield into a variable
int fieldheight = 600;

//put the width of the current battlefield into a variable
int fieldwidth = 800;

/****************************************************************************************************
I want to be able to input the getBattleFieldWidth() and getBattleFieldHeight()
instead of just inputing the numbers...however, the code won't work when I do that for some reason.
****************************************************************************************************/

/*put the center point of the battlefield into an x and y coord...used later in the "safeway" factor.
and set up 2 additional coordinate points to make the code more random and more unpredictable. */
int centerx = (fieldwidth / 2);
int centery = (fieldheight / 2);

//set the variable from the wall that is the maximum distance we can get to the wall through the goTo
int walldistancevariable = (fieldwidth / 26);

//L1 stands for Layer 1 and the P1/etc is Point 1 or so forth...each layer has 4 points
int L1P1x = (centerx / 2);
int L1P2x = (centerx / 2);
int L1P3x = (3 * centerx) / 4;
int L1P4x = (3 * centerx) / 4;

int L1P1y = (3 * centery) / 4;
int L1P2y = (centery / 2);
int L1P3y = (centery / 2);
int L1P4y = (3 * centery) / 4;

//L2 stands for Layer 2 and the P1/etc is Point 1 or so forth...each layer has 4 points
int L2P1x = (centerx / 4);
int L2P2x = centerx;
int L2P3x = (3 * centerx) / 2;
int L2P4x = centerx;

int L2P1y = centery;
int L2P2y = (centery / 4);
int L2P3y = centery;
int L2P4y = (3 * centery) / 2;

//L3 stands for Layer 3 and the P1/etc is Point 1 or so forth...each layer has 4 points
int L3P1x = (fieldwidth - fieldwidth + walldistancevariable);
int L3P2x = (fieldwidth - fieldwidth + walldistancevariable);
int L3P3x = (fieldwidth - walldistancevariable);
int L3P4x = (fieldwidth - walldistancevariable);

int L3P1y = (fieldheight - walldistancevariable);
int L3P2y = (fieldheight - fieldheight + walldistancevariable);
int L3P3y = (fieldheight - fieldheight + walldistancevariable);
int L3P4y = (fieldheight - walldistancevariable);


//set up all the random points with the center and 3 layers to have picked later to go to
private Point2D[] destinations = { //private point2d opening bracket

new Point2D.Double(centerx, centery),
new Point2D.Double(L1P1x, L1P1y),
new Point2D.Double(L1P2x, L1P2y),
new Point2D.Double(L1P3x, L1P3y),
new Point2D.Double(L1P4x, L1P4y),
new Point2D.Double(L2P1x, L2P1y),
new Point2D.Double(L2P2x, L2P2y),
new Point2D.Double(L2P3x, L2P3y),
new Point2D.Double(L2P4x, L2P4y),
new Point2D.Double(L3P1x, L3P1y),
new Point2D.Double(L3P2x, L3P2y),
new Point2D.Double(L3P3x, L3P3y),
new Point2D.Double(L3P4x, L3P4y)

}; //private point2d closing bracket


public void run() { //run opening bracket

turnGunRightRadians(Double.POSITIVE_INFINITY);

} //run closing bracket


public void onScannedRobot(ScannedRobotEvent e) { //onScannedRobot opening bracket


double bulletPower = Math.min(3.0,getEnergy());
double myX = getX();
double myY = getY();
double absoluteBearing = getHeadingRadians() + e.getBearingRadians();
double enemyX = getX() + e.getDistance() * Math.sin(absoluteBearing);
double enemyY = getY() + e.getDistance() * Math.cos(absoluteBearing);
double enemyHeading = e.getHeadingRadians();
double enemyVelocity = e.getVelocity();


double deltaTime = 0;
double battleFieldHeight = getBattleFieldHeight(), battleFieldWidth = getBattleFieldWidth();
double predictedX = enemyX, predictedY = enemyY;

while((++deltaTime) * (20.0 - 3.0 * bulletPower) < Point2D.Double.distance(myX, myY, predictedX, predictedY)){ //while opening bracket

predictedX += Math.sin(enemyHeading) * enemyVelocity;
predictedY += Math.cos(enemyHeading) * enemyVelocity;

if( predictedX < 18.0

|| predictedY < 18.0
|| predictedX > battleFieldWidth - 18.0
|| predictedY > battleFieldHeight - 18.0){ //if opening bracket

predictedX = Math.min(Math.max(18.0, predictedX), battleFieldWidth - 18.0);
predictedY = Math.min(Math.max(18.0, predictedY), battleFieldHeight - 18.0);
break;

} //if closing bracket

setTurnGunRightRadians(0 - getGunTurnRemainingRadians());
if (Math.abs(getDistanceRemaining()) < 5) { //if opening bracket

goTo(destinations[(int)(Math.random() * destinations.length)]);

} //if closing bracket
} //while closing bracket

double theta = Utils.normalAbsoluteAngle(Math.atan2(
predictedX - getX(), predictedY - getY()));

setTurnRadarRightRadians(Utils.normalRelativeAngle(absoluteBearing - getRadarHeadingRadians()));
setTurnGunRightRadians(Utils.normalRelativeAngle(theta - getGunHeadingRadians()));

fire(bulletPower);

} //on scanned robot closing backet

private void goTo(Point2D destination) { //goTo opening bracket

Point2D location = new Point2D.Double(getX(), getY());
double distance = location.distance(destination);
double angle = normalRelativeAngle(absoluteBearing(location, destination) - getHeading());

if (Math.abs(angle) > 90) { //if1 opening bracket

distance *= -1;
if (angle > 0) { //if2 opening bracket

angle -= 180;

} //if2 closing bracket

else { //else for if2 opening bracket

angle += 180;

} //else closing bracket
} //if1 closing bracket

setTurnRight(angle);
setAhead(distance);

} //goTo closing bracket

//Reset the absoluteBearing to the new absolute bearing so that the numbers are current
private double absoluteBearing(Point2D source, Point2D target) { //private double opening bracket

return Math.toDegrees(Math.atan2(target.getX() - source.getX(), target.getY() - source.getY()));

} //private double closing bracket

//Reset the angle to the new angle so that the numbers are current
private double normalRelativeAngle(double angle) { //private double opening bracket

angle = Math.toRadians(angle);
return Math.toDegrees(Math.atan2(Math.sin(angle), Math.cos(angle)));

} //private double closing bracket
} //public class closing bracket






Thanks,
Matt



------------------------------------

Yahoo! Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/Robocode/

<*> Your email settings:
Individual Email | Traditional

<*> To change settings online go to:
http://groups.yahoo.com/group/Robocode/join
(Yahoo! ID required)

<*> To change settings via email:
Robocode-***@yahoogroups.com
Robocode-***@yahoogroups.com

<*> To unsubscribe from this group, send an email to:
Robocode-***@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
Johannes Slotta
2009-11-18 23:47:29 UTC
Permalink
Hi Matt,

You cannot call anything like getBattleFieldWidth() in the
constructor, so you have to do the initialization stuff in the
beginning of the run() method. This way it should work.

You should look at what the gotobot is doing. It is constantly
spinning (switching between +infinity and -infinity on fire), this is
vital for its functions since without spinning it won't scan anything
and all the logic is inside the onScannedRobot() routine. You use
values different from these for targeting reason, breaking this
movement and allowing it to stop spinning when the enemy manages to
slip through unseen. Too tired now (it's 00:40 in the morning) to do
further analysis on how to fix this. Don't like the idea of depending
on onScannedRobot() that much either.

HTH
Johannes


------------------------------------

Yahoo! Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/Robocode/

<*> Your email settings:
Individual Email | Traditional

<*> To change settings online go to:
http://groups.yahoo.com/group/Robocode/join
(Yahoo! ID required)

<*> To change settings via email:
Robocode-***@yahoogroups.com
Robocode-***@yahoogroups.com

<*> To unsubscribe from this group, send an email to:
Robocode-***@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
Matt
2009-11-19 14:09:24 UTC
Permalink
Post by Johannes Slotta
Hi Matt,
You cannot call anything like getBattleFieldWidth() in the
constructor, so you have to do the initialization stuff in the
beginning of the run() method. This way it should work.
You should look at what the gotobot is doing. It is constantly
spinning (switching between +infinity and -infinity on fire), this is
vital for its functions since without spinning it won't scan anything
and all the logic is inside the onScannedRobot() routine. You use
values different from these for targeting reason, breaking this
movement and allowing it to stop spinning when the enemy manages to
slip through unseen. Too tired now (it's 00:40 in the morning) to do
further analysis on how to fix this. Don't like the idea of depending
on onScannedRobot() that much either.
HTH
Johannes
You said that yo don't like depending on the onScannedRobot()...how do you not depend on it? (I'm not advanced enough to have ever seen or used a robot that can function well without the onScannedRobot()...)




------------------------------------

Yahoo! Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/Robocode/

<*> Your email settings:
Individual Email | Traditional

<*> To change settings online go to:
http://groups.yahoo.com/group/Robocode/join
(Yahoo! ID required)

<*> To change settings via email:
Robocode-***@yahoogroups.com
Robocode-***@yahoogroups.com

<*> To unsubscribe from this group, send an email to:
Robocode-***@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
Matt
2009-11-19 15:26:07 UTC
Permalink
Okay...I've been playing around with the code for about an hour and a half and I have yet to be able to fix either of the problems...I'm seriously confused on what to do...



------------------------------------

Yahoo! Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/Robocode/

<*> Your email settings:
Individual Email | Traditional

<*> To change settings online go to:
http://groups.yahoo.com/group/Robocode/join
(Yahoo! ID required)

<*> To change settings via email:
Robocode-***@yahoogroups.com
Robocode-***@yahoogroups.com

<*> To unsubscribe from this group, send an email to:
Robocode-***@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
Johannes Slotta
2009-11-19 14:56:27 UTC
Permalink
Hi Matt,
I think I didn't express it the right way. I agree, you need
onScannedRobot() or the event. But I think it should not be the only
way to initiate action. If your robot does not scan any enemy for a
certain time it will stop moving until it scans another one. This is
not a problem unless you play on very large arenas where robots might
get out of scan range, but I simply don't like it. Since large arenas
are not very common it might not be relevant.

How's your robot going? Could you change it in a way which keeps the
radar spinning?


------------------------------------

Yahoo! Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/Robocode/

<*> Your email settings:
Individual Email | Traditional

<*> To change settings online go to:
http://groups.yahoo.com/group/Robocode/join
(Yahoo! ID required)

<*> To change settings via email:
Robocode-***@yahoogroups.com
Robocode-***@yahoogroups.com

<*> To unsubscribe from this group, send an email to:
Robocode-***@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
Matt
2009-11-19 17:14:56 UTC
Permalink
Post by Johannes Slotta
Hi Matt,
I think I didn't express it the right way. I agree, you need
onScannedRobot() or the event. But I think it should not be the only
way to initiate action. If your robot does not scan any enemy for a
certain time it will stop moving until it scans another one. This is
not a problem unless you play on very large arenas where robots might
get out of scan range, but I simply don't like it. Since large arenas
are not very common it might not be relevant.
How's your robot going? Could you change it in a way which keeps the
radar spinning?
Well, to be honest...I don't completely understand the GoToBot code...I'm still new to java and robocode and I vaguely know how it works...and that is where the problem lies...because since I don't know how the GoToBot works all the way, I don't know what is causing the problem. As goes with the linear/circular targeting...I don't fully understand it and, like the GoToBot code...I'm not sure where or how to fix what is bugging the system out.



------------------------------------

Yahoo! Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/Robocode/

<*> Your email settings:
Individual Email | Traditional

<*> To change settings online go to:
http://groups.yahoo.com/group/Robocode/join
(Yahoo! ID required)

<*> To change settings via email:
Robocode-***@yahoogroups.com
Robocode-***@yahoogroups.com

<*> To unsubscribe from this group, send an email to:
Robocode-***@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
Johannes Slotta
2009-11-19 21:24:02 UTC
Permalink
Hello Matt,

I'll try to explain what this bot does. First, run() is called. It
starts spinning its gun (tells the gun to spin by an infinite angle),
then run() exits. Since the radar is fixed on the gun it starts
spinning, too. Nothing very interesting here.

Once the radar beam goes over an enemy robot, onScannedRobot() is
called by the game. The method fires and changes turning direction
(+inf to -inf or vice versa). That way it can "lock" on the target as
long as it is within range and does not move out of the sector the
radar beam will cover next tick. Additionally, if your bot is no
longer moving (or will be stopping soon) it calls goTo with a random
location entry of its array. This method calculates how to move there
and runs the necessary commands. But all this logic (besides the first
spin starter) is called only when an enemy is scanned. So never try
this behaviour on 5k*5k battlefields.

I have still enough to learn, too, my bots are worse than some of the
sample bots :-/

Happy coding,
Johannes


------------------------------------

Yahoo! Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/Robocode/

<*> Your email settings:
Individual Email | Traditional

<*> To change settings online go to:
http://groups.yahoo.com/group/Robocode/join
(Yahoo! ID required)

<*> To change settings via email:
Robocode-***@yahoogroups.com
Robocode-***@yahoogroups.com

<*> To unsubscribe from this group, send an email to:
Robocode-***@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
Matt
2009-11-20 14:49:37 UTC
Permalink
Post by Johannes Slotta
Hello Matt,
I'll try to explain what this bot does. First, run() is called. It
starts spinning its gun (tells the gun to spin by an infinite angle),
then run() exits. Since the radar is fixed on the gun it starts
spinning, too. Nothing very interesting here.
Once the radar beam goes over an enemy robot, onScannedRobot() is
called by the game. The method fires and changes turning direction
(+inf to -inf or vice versa). That way it can "lock" on the target as
long as it is within range and does not move out of the sector the
radar beam will cover next tick. Additionally, if your bot is no
longer moving (or will be stopping soon) it calls goTo with a random
location entry of its array. This method calculates how to move there
and runs the necessary commands. But all this logic (besides the first
spin starter) is called only when an enemy is scanned. So never try
this behaviour on 5k*5k battlefields.
I have still enough to learn, too, my bots are worse than some of the
sample bots :-/
Happy coding,
Johannes
I can understand all that fine. And...just to make sure I understand...this part:

//Reset the absoluteBearing to the new absolute bearing so that the numbers are current
private double absoluteBearing(Point2D source, Point2D target) { //private double opening bracket

return Math.toDegrees(Math.atan2(target.getX() - source.getX(), target.getY() - source.getY()));

} //private double closing bracket

//Reset the angle to the new angle so that the numbers are current
private double normalRelativeAngle(double angle) { //private double opening bracket

angle = Math.toRadians(angle);
return Math.toDegrees(Math.atan2(Math.sin(angle), Math.cos(angle)));

} //private double closing bracket


is just resetting the angle and distance to travel to get to the next point...right?



Anyway...I don't understand WHY the robot is breaking down...like...what is it in the code that is messing up the robot or contradicting or w/e you want to call it?



------------------------------------

Yahoo! Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/Robocode/

<*> Your email settings:
Individual Email | Traditional

<*> To change settings online go to:
http://groups.yahoo.com/group/Robocode/join
(Yahoo! ID required)

<*> To change settings via email:
Robocode-***@yahoogroups.com
Robocode-***@yahoogroups.com

<*> To unsubscribe from this group, send an email to:
Robocode-***@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
Johannes Slotta
2009-11-20 16:42:29 UTC
Permalink
absoluteBearing() gives you the absolute angle of the line between
"source" and "target" Has to be relativated by subtracting
getHeading().

normalRelativeAngle() translates the argument angle (which is in
degrees) into an interval of -180 to 180 so that your robot can turn
right or left, whichever is better. Without that one you may turn
right by 300 degrees which definitely takes longer than turning left
by 60 degrees. The same could be achieved by adding 180 plus a large
multiple of 360 (so even the smallest value you can expect from your
formula becomes positive), then using %360 on that, then subtracting
the 180 again. This was the way we did that in the old days when even
divisions were expensive operations and sine was replaced by lookup
tables where possible. But execution speed should not matter here.

The robot breaks down because after firing you don't set it back to
its original state with the plusminus infinity angle to turn the gun.
It does work in a limited way as long as your enemy crosses the radar
beam every turn. But after that your robot will eventually freeze.


------------------------------------

Yahoo! Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/Robocode/

<*> Your email settings:
Individual Email | Traditional

<*> To change settings online go to:
http://groups.yahoo.com/group/Robocode/join
(Yahoo! ID required)

<*> To change settings via email:
Robocode-***@yahoogroups.com
Robocode-***@yahoogroups.com

<*> To unsubscribe from this group, send an email to:
Robocode-***@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/

Julian Kent
2009-11-20 22:43:29 UTC
Permalink
Post by Matt
However, when I replace the old firing system with the linear targeting system, my robot will randomly quit working out of no where. As in, the robot will work for a second or a minute then just freeze in place with no movement, no radar spin, no firing, and no gun turn. I'm not sure what is causing this.
From what I can see, it's because you're using fire() instead of
setFire(). fire() is a blocking method which will lock your robot until
your gun cools sufficiently to fire a bullet.

HTH,

Julian (aka Skilgannon on Robowiki)



------------------------------------

Yahoo! Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/Robocode/

<*> Your email settings:
Individual Email | Traditional

<*> To change settings online go to:
http://groups.yahoo.com/group/Robocode/join
(Yahoo! ID required)

<*> To change settings via email:
Robocode-***@yahoogroups.com
Robocode-***@yahoogroups.com

<*> To unsubscribe from this group, send an email to:
Robocode-***@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
Loading...