Difference between revisions of "SuperWalls"

From Robowiki
Jump to navigation Jump to search
(New page: SuperWalls is a part of the Super Sample Bots set by CrazyBassoonist. It is intended to provide new robocoders with a new challenge after beating all of the sample robots. == Moveme...)
 
(Add infobox)
 
(13 intermediate revisions by 5 users not shown)
Line 1: Line 1:
SuperWalls is a part of the [[Super Sample Bots]] set by CrazyBassoonist. It is intended to provide new robocoders with a new challenge after beating all of the sample robots.
+
{{Infobox Robot
 +
| author          = [[CrazyBassoonist]], [[Nat]]
 +
| extends        = [[AdvancedRobot]]
 +
| targeting      = [[Head-On Targeting]], [[Linear Targeting]]
 +
| movement        = [[Random Movement|Random]] [[Pattern Movement]]
 +
| current_version = 1.0
 +
| license        = Public domain
 +
| download_link  = http://robocode-archive.strangeautomata.com/robots/supersample.SuperWalls_1.0.jar
 +
| isOpenSource    = yes
 +
| isMelee        = no
 +
| isOneOnOne      = yes
 +
}}
  
 +
SuperWalls is a part of the [[Super Sample Bots]] set by [[CrazyBassoonist]]. It is intended to provide new Robocoders with a new challenge after beating all of the [[Sample Bots|sample robots]].
  
 +
Thanks to [[Nat]] for cleaning up the bad coding caused by my horrible coding skills.
  
 
== Movement==
 
== Movement==
SuperWalls uses the exact same movement as Walls.
+
SuperWalls moves like Walls, but without a constant speed and it changes directions every now and then.
 
 
  
 
== Targeting ==
 
== Targeting ==
Line 11: Line 23:
  
 
== Code ==
 
== Code ==
 +
<syntaxhighlight>
 +
package supersample;
  
<pre>
 
package wiki.superSampleBot;
 
 
import robocode.HitRobotEvent;
 
import robocode.Robot;
 
import robocode.ScannedRobotEvent;
 
 
import robocode.*;
 
import robocode.*;
 +
import robocode.util.*;
 
import java.awt.*;
 
import java.awt.*;
import robocode.util.Utils;
 
  
 
/**
 
/**
 
  * SuperWalls - a sample robot by CrazyBassoonist based on the sample robot Walls by Mathew Nelson and maintained by Flemming N. Larsen
 
  * SuperWalls - a sample robot by CrazyBassoonist based on the sample robot Walls by Mathew Nelson and maintained by Flemming N. Larsen
* <p/>
 
 
  * Moves around the outer edge with two targeting systems
 
  * Moves around the outer edge with two targeting systems
 
  */
 
  */
 
public class SuperWalls extends AdvancedRobot {
 
public class SuperWalls extends AdvancedRobot {
static double HGShots;//Number of shots with Head-On Targeting
+
static int HGShots;     //Number of shots with Head-On Targeting
static double LGShots;//Number of shots with Linear Targeting
+
static int LGShots;     //Number of shots with Linear Targeting
static double HGHits;//Number of hits with Head-On Targeting
+
static int HGHits;     //Number of hits with Head-On Targeting
static double LGHits;//Number of hits with Linear Targeting
+
static int LGHits;     //Number of hits with Linear Targeting
static double HGRating;//Head-on Targeting gun's rating
+
boolean gunIdent;       //Used to tell which gun we are using
static double LGRating;//Linear gun's rating
+
int dir = 1;
static double roundNum;//Number of rounds
+
double energy;
double gunIdent;//Used to tell which gun we are using
+
static int enemyFireCount = 0;
boolean peek; // Don't turn if there's a robot there
 
double moveAmount; // How much to move
 
  
/**
 
* run: Move around the walls
 
*/
 
 
public void run() {
 
public void run() {
roundNum=roundNum+1;//update number of rounds
+
setAdjustRadarForRobotTurn(true);
setAdjustRadarForRobotTurn(true);//keeps our radar from moving with our gun
+
setAdjustRadarForGunTurn(true);
setAdjustRadarForGunTurn(true);//keeps our radar from moving with our robot
 
 
setBodyColor(Color.black);
 
setBodyColor(Color.black);
 
setGunColor(Color.black);
 
setGunColor(Color.black);
Line 51: Line 52:
 
setBulletColor(Color.cyan);
 
setBulletColor(Color.cyan);
 
setScanColor(Color.cyan);
 
setScanColor(Color.cyan);
// Initialize moveAmount to the maximum possible for this battlefield.
+
 
moveAmount = Math.max(getBattleFieldWidth(), getBattleFieldHeight());
+
setTurnRadarRight(Double.POSITIVE_INFINITY);
// Initialize peek to false
 
peek = false;
 
  
 
// turnLeft to face a wall.
 
// turnLeft to face a wall.
Line 60: Line 59:
 
// getHeading() divided by 90.
 
// getHeading() divided by 90.
 
turnLeft(getHeading() % 90);
 
turnLeft(getHeading() % 90);
ahead(moveAmount);
+
 
// Turn the gun to turn right 90 degrees.
 
peek = true;
 
turnRight(90);
 
 
while (true) {
 
while (true) {
setTurnRadarRightRadians(10000);
+
//if (getDistanceRemaining() == 0) {
// Look before we turn when ahead() completes.
+
// turnRight(90 * dir);
peek = true;
+
if (Utils.isNear(getHeadingRadians(), 0D) || Utils.isNear(getHeadingRadians(), Math.PI)) {
// Move up the wall
+
ahead((Math.max(getBattleFieldHeight() - getY(), getY()) - 28) * dir);
ahead(moveAmount);
+
} else {
// Don't look now
+
ahead((Math.max(getBattleFieldWidth() - getX(), getX()) - 28) * dir);
peek = false;
+
}
// Turn to the next wall
+
turnRight(90 * dir);
turnRight(90);
+
//}
 
}
 
}
 
}
 
}
  
 
/**
 
/**
* onHitRobot: Move away a bit.
+
* onScannedRobot: Fire!
 
*/
 
*/
public void onHitRobot(HitRobotEvent e) {
+
public void onScannedRobot(ScannedRobotEvent e) {
// If he's in front of us, set back up a bit.
+
double absBearing = e.getBearingRadians() + getHeadingRadians();                // The enemies location relative to us
if (e.getBearing() > -90 && e.getBearing() < 90) {
+
double latVel = e.getVelocity() * Math.sin(e.getHeadingRadians() - absBearing); // The enemies lateral velocity
back(100);
+
double radarTurn = absBearing - getRadarHeadingRadians();                      // The amount to turn our radar
} // else he's in back of us, so set ahead a bit.
+
 
else {
+
double HGRating = (double) HGHits / HGShots;
ahead(100);
+
double LGRating = (double) LGHits / LGShots;
 +
 +
if (energy > (energy = e.getEnergy())) {
 +
enemyFireCount++;
 +
if (enemyFireCount % 5 == 0) {
 +
dir = -dir;
 +
if (Utils.isNear(getHeadingRadians(), 0D) || Utils.isNear(getHeadingRadians(), Math.PI)) {
 +
setAhead((Math.max(getBattleFieldHeight() - getY(), getY()) - 28) * dir);
 +
} else {
 +
setAhead((Math.max(getBattleFieldWidth() - getX(), getX()) - 28) * dir);
 +
}
 +
}
 
}
 
}
}
+
 +
setMaxVelocity(Math.random() * 12);
  
/**
+
if ((getRoundNum() == 0 || LGRating > HGRating) && getRoundNum() != 1){ // In the first round or when linear gun got more hitting percentage use linear targeting
* onScannedRobot:  Fire!
+
double bulletPower = Math.min(3, e.getEnergy() / 4);
*/
+
setTurnGunRightRadians(Utils.normalRelativeAngle(absBearing - getGunHeadingRadians() + Math.asin(latVel / (20 - 3 * bulletPower))));
public void onScannedRobot(ScannedRobotEvent e) {
+
LGShots++;
double absBearing=e.getBearingRadians()+getHeadingRadians();//The enemies location relative to us
+
gunIdent = true;
double latVel=e.getVelocity()*Math.sin(e.getHeadingRadians()-absBearing);//The enemies lateral velocity
+
setFire(bulletPower); // Fire the minimum amount of energy needed to finish off the other robot
double radarTurn =getHeadingRadians() + e.getBearingRadians() - getRadarHeadingRadians();//The amount to turn our radar
+
} else { // in second round or when the head-on gun got more hitting percentage, use head-on gun.
HGRating=HGHits/HGShots;//recalculate both gun's ratings
+
setTurnGunRightRadians(Utils.normalRelativeAngle(absBearing - getGunHeadingRadians()));
LGRating=LGHits/LGShots;
+
HGShots++;
if(roundNum==1){//In the first round use linear targeting
+
gunIdent = false;
setTurnGunRightRadians(robocode.util.Utils.normalRelativeAngle(absBearing-getGunHeadingRadians()+latVel/11));//Turn our gun
+
setFire(e.getEnergy() / 4); // Fire the minimum amount of energy needed to finish off the other robot
LGShots=LGShots+1;
 
gunIdent=111;
 
 
}
 
}
else if(roundNum==2){//in the second use head-on
+
setTurnRadarRightRadians(Utils.normalRelativeAngle(radarTurn) * 2); // Make the radar lock on
setTurnGunRightRadians(robocode.util.Utils.normalRelativeAngle(absBearing-getGunHeadingRadians()));
 
HGShots=HGShots+1;
 
gunIdent=222;
 
}
 
else{//after that...
 
if(LGRating>HGRating){//use whichever gun has the highest rating
 
setTurnGunRightRadians(robocode.util.Utils.normalRelativeAngle(absBearing-getGunHeadingRadians()+latVel/11));//Turn our gun
 
LGShots=LGShots+1;
 
gunIdent=111;
 
}
 
else if(LGRating<=HGRating){
 
setTurnGunRightRadians(robocode.util.Utils.normalRelativeAngle(absBearing-getGunHeadingRadians()));
 
HGShots=HGShots+1;
 
gunIdent=222;
 
}
 
}
 
 
setTurnRadarRightRadians(Utils.normalRelativeAngle(radarTurn)*2);//Make the radar lock on
 
setFire(e.getEnergy()/4);//Fire the minimum amount of energy needed to finish off the other robot
 
 
}
 
}
public void onBulletHit(BulletHitEvent e){//add a hit to the ranking of whatever gun we were using
+
 
if(gunIdent==111){
+
public void onBulletHit(BulletHitEvent e) {
LGHits=LGHits+1;
+
if(gunIdent) {
}
+
LGHits = LGHits+1;
else{
+
} else {
HGHits=HGHits+1;
+
HGHits = HGHits+1;
 
}
 
}
 
}
 
}
 
}
 
}
</pre>
+
</syntaxhighlight>
 +
[[Category:Super Sample Bots]]
 +
__NOTOC__ __NOEDITSECTION__

Latest revision as of 11:38, 4 September 2017

SuperWalls
Author(s) CrazyBassoonist, Nat
Extends AdvancedRobot
Targeting Head-On Targeting, Linear Targeting
Movement Random Pattern Movement
Current Version 1.0
Code License Public domain
Download

SuperWalls is a part of the Super Sample Bots set by CrazyBassoonist. It is intended to provide new Robocoders with a new challenge after beating all of the sample robots.

Thanks to Nat for cleaning up the bad coding caused by my horrible coding skills.

Movement

SuperWalls moves like Walls, but without a constant speed and it changes directions every now and then.

Targeting

SuperWalls uses two guns, a Linear Targeting gun and a Head-On Targeting gun. It keeps track of the number and of shots and hits of each gun, and uses whichever gun works better.

Code

package supersample;

import robocode.*;
import robocode.util.*;
import java.awt.*;

/**
 * SuperWalls - a sample robot by CrazyBassoonist based on the sample robot Walls by Mathew Nelson and maintained by Flemming N. Larsen
 * Moves around the outer edge with two targeting systems
 */
public class SuperWalls extends AdvancedRobot {
	static int HGShots;     //Number of shots with Head-On Targeting
	static int LGShots;     //Number of shots with Linear Targeting
	static int HGHits;      //Number of hits with Head-On Targeting
	static int LGHits;      //Number of hits with Linear Targeting
	boolean gunIdent;       //Used to tell which gun we are using
	int dir = 1;
	double energy;
	static int enemyFireCount = 0;

	public void run() {
		setAdjustRadarForRobotTurn(true);
		setAdjustRadarForGunTurn(true);
		setBodyColor(Color.black);
		setGunColor(Color.black);
		setRadarColor(Color.orange);
		setBulletColor(Color.cyan);
		setScanColor(Color.cyan);

		setTurnRadarRight(Double.POSITIVE_INFINITY);

		// turnLeft to face a wall.
		// getHeading() % 90 means the remainder of
		// getHeading() divided by 90.
		turnLeft(getHeading() % 90);

		while (true) {
			//if (getDistanceRemaining() == 0) {
			//	turnRight(90 * dir);
				if (Utils.isNear(getHeadingRadians(), 0D) || Utils.isNear(getHeadingRadians(), Math.PI)) {
					ahead((Math.max(getBattleFieldHeight() - getY(), getY()) - 28) * dir);
				} else {
					ahead((Math.max(getBattleFieldWidth() - getX(), getX()) - 28) * dir);
				}
			turnRight(90 * dir);
			//}
		}
	}

	/**
	 * onScannedRobot: Fire!
	 */
	public void onScannedRobot(ScannedRobotEvent e) {
		double absBearing = e.getBearingRadians() + getHeadingRadians();                // The enemies location relative to us
		double latVel = e.getVelocity() * Math.sin(e.getHeadingRadians() - absBearing); // The enemies lateral velocity
		double radarTurn = absBearing - getRadarHeadingRadians();                       // The amount to turn our radar

		double HGRating = (double) HGHits / HGShots;
		double LGRating = (double) LGHits / LGShots;
		
		if (energy > (energy = e.getEnergy())) {
			enemyFireCount++;
			if (enemyFireCount % 5 == 0) {
				dir = -dir;
				if (Utils.isNear(getHeadingRadians(), 0D) || Utils.isNear(getHeadingRadians(), Math.PI)) {
					setAhead((Math.max(getBattleFieldHeight() - getY(), getY()) - 28) * dir);
				} else {
					setAhead((Math.max(getBattleFieldWidth() - getX(), getX()) - 28) * dir);
				}
			}
		}
	
		setMaxVelocity(Math.random() * 12);

		if ((getRoundNum() == 0 || LGRating > HGRating) && getRoundNum() != 1){ // In the first round or when linear gun got more hitting percentage use linear targeting
			double bulletPower = Math.min(3, e.getEnergy() / 4);
			setTurnGunRightRadians(Utils.normalRelativeAngle(absBearing - getGunHeadingRadians() + Math.asin(latVel / (20 - 3 * bulletPower))));
			LGShots++;
			gunIdent = true;
			setFire(bulletPower); // Fire the minimum amount of energy needed to finish off the other robot
		} else { // in second round or when the head-on gun got more hitting percentage, use head-on gun.
			setTurnGunRightRadians(Utils.normalRelativeAngle(absBearing - getGunHeadingRadians()));
			HGShots++;
			gunIdent = false;
			setFire(e.getEnergy() / 4); // Fire the minimum amount of energy needed to finish off the other robot
		}
		setTurnRadarRightRadians(Utils.normalRelativeAngle(radarTurn) * 2); // Make the radar lock on
	}

	public void onBulletHit(BulletHitEvent e) {
		if(gunIdent) {
			LGHits = LGHits+1;
		} else {
			HGHits = HGHits+1;
		}
	}
}