Matt
2009-11-18 17:22:07 UTC
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/
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/