Difference between revisions of "GITS/Targeting/Source"

From Robowiki
Jump to navigation Jump to search
(Updated Source)
Line 1: Line 1:
'''GITS Targeting (Source)''' - from version 0.5-051409
+
'''GITS Targeting (Source)''' - from version 0.5-051509
 
----
 
----
 
<pre>
 
<pre>
 
public void onScannedRobot(ScannedRobotEvent e) {
 
public void onScannedRobot(ScannedRobotEvent e) {
 
double rSweep = 0.99; //RadarSweep tweak
 
double rSweep = 0.99; //RadarSweep tweak
double bPower = Math.min(3.0,getEnergy());
 
 
double myX = getX();
 
double myX = getX();
 
double myY = getY();
 
double myY = getY();
Line 10: Line 9:
 
double hisX = (getX() + e.getDistance() * Math.sin(aBearing));
 
double hisX = (getX() + e.getDistance() * Math.sin(aBearing));
 
double hisY = (getY() + e.getDistance() * Math.cos(aBearing));
 
double hisY = (getY() + e.getDistance() * Math.cos(aBearing));
 +
double eAbsBearing = e.getBearingRadians() + getHeadingRadians(); //Enemy Absolute Bearing
 
double eHeading = e.getHeadingRadians();
 
double eHeading = e.getHeadingRadians();
double eHeadingChange = eHeading - _eHeading;
+
double eHeadingChange = eHeading - _eHeading; //Heading data for later
_eHeading = eHeading;
+
_eHeading = eHeading; //Update the enemy heading
 
double eVelocity = e.getVelocity();
 
double eVelocity = e.getVelocity();
 +
double eLatVel = eVelocity*Math.sin(eHeading - eAbsBearing); //Enemy Lateral Velocity
 
double eDistance = e.getDistance();
 
double eDistance = e.getDistance();
 
double eEnergy = e.getEnergy();
 
double eEnergy = e.getEnergy();
 
double dTime = 0;
 
double dTime = 0;
 
double fHeight = getBattleFieldHeight(),
 
double fHeight = getBattleFieldHeight(),
  fWidth = getBattleFieldWidth();
+
fWidth = getBattleFieldWidth();
 
double aHisX = hisX, aHisY = hisY;
 
double aHisX = hisX, aHisY = hisY;
 +
int GBM = (eVelocity < 0 ? -1 : 1); //Going Backwards Multiplier
 +
int MCV = 0; //Most Common Velocity
 +
int eAbsVelocity = (int)Math.round(Math.abs(eVelocity));
 +
_vCounts[eAbsVelocity]++;
 +
int _MCV = _vCounts[0];
 +
//A bunch of bullet calulations
 +
double MBP = (eEnergy > 15.9 ? 3.0 : //If Enemy's Energy is at or over 16 points
 +
(eEnergy > 7.9 ? 2.0 : //If Enemy's Energy is at or over 8 points
 +
(eEnergy > 3.9 ? 1.0 : 0.5))); //Everything else, basically
 +
double bPower = Math.min(MBP,getEnergy()); //Figure out the max you can spend on firing a bullet
 +
double bVelocity = (20 - (3 * bPower));
 +
//Historical Velocity Recall
 +
for (int i = 1; i < 9; i++) {
 +
if(_vCounts[i] > _MCV) {
 +
_MCV = _vCounts[i];
 +
MCV = i;
 +
}
 +
}
 
 
while((++dTime) * (20.0 - 3.0 * bPower) < Point2D.Double.distance(myX, myY, aHisX, aHisY)) {
+
while((++dTime) * bVelocity < Point2D.Double.distance(myX, myY, aHisX, aHisY)) {
 
aHisX += (Math.sin(eHeading) * eVelocity);
 
aHisX += (Math.sin(eHeading) * eVelocity);
 
aHisY += (Math.cos(eHeading) * eVelocity);
 
aHisY += (Math.cos(eHeading) * eVelocity);
 
eHeading += eHeadingChange;
 
eHeading += eHeadingChange;
if(aHisX < 18.0 || aHisX > (fWidth - 18.0) || aHisY < 18.0 || aHisY > (fHeight - 18.0)) {
+
if(aHisX < 20.0 || aHisX > (fWidth - 20.0) || aHisY < 20.0 || aHisY > (fHeight - 20.0)) {
aHisX = Math.min(Math.max(18.0, aHisX), (fWidth - 18.0));
+
aHisX = Math.min(Math.max(20.0, aHisX), (fWidth - 20.0));
aHisY = Math.min(Math.max(18.0, aHisY), (fHeight - 18.0));
+
aHisY = Math.min(Math.max(20.0, aHisY), (fHeight - 20.0));
 
break;
 
break;
 
}
 
}
Line 38: Line 57:
 
execute();
 
execute();
 
 
if(eEnergy >= (0.025 * eDistance)) { fire(bPower); } //PowerConserve tweak
+
if(eEnergy >= (0.025 * eDistance)) { setFire(bPower); } //PowerConserve tweak
 
}
 
}
 
</pre>
 
</pre>
 
----
 
----
 
Currently, this targeting formula works for simple robots 90% of the time. It's rubbish against most advanced robots. Feel free to improve/comment on it so I can learn (since tutorials are utterly useless on me, I need visual aids).
 
Currently, this targeting formula works for simple robots 90% of the time. It's rubbish against most advanced robots. Feel free to improve/comment on it so I can learn (since tutorials are utterly useless on me, I need visual aids).

Revision as of 18:27, 15 May 2009

GITS Targeting (Source) - from version 0.5-051509


public void onScannedRobot(ScannedRobotEvent e) {
	double rSweep = 0.99; //RadarSweep tweak
	double myX = getX();
	double myY = getY();
	double aBearing = (getHeadingRadians() + e.getBearingRadians());
	double hisX = (getX() + e.getDistance() * Math.sin(aBearing));
	double hisY = (getY() + e.getDistance() * Math.cos(aBearing));
	double eAbsBearing = e.getBearingRadians() + getHeadingRadians(); //Enemy Absolute Bearing
	double eHeading = e.getHeadingRadians();
	double eHeadingChange = eHeading - _eHeading; //Heading data for later
	_eHeading = eHeading; //Update the enemy heading
	double eVelocity = e.getVelocity();
	double eLatVel = eVelocity*Math.sin(eHeading - eAbsBearing); //Enemy Lateral Velocity
	double eDistance = e.getDistance();
	double eEnergy = e.getEnergy();
	double dTime = 0;
	double fHeight = getBattleFieldHeight(),
	fWidth = getBattleFieldWidth();
	double aHisX = hisX, aHisY = hisY;
	int GBM = (eVelocity < 0 ? -1 : 1); //Going Backwards Multiplier
	int MCV = 0; //Most Common Velocity
	int eAbsVelocity = (int)Math.round(Math.abs(eVelocity));
	_vCounts[eAbsVelocity]++;
	int _MCV = _vCounts[0];
	//A bunch of bullet calulations
	double MBP = (eEnergy > 15.9 ? 3.0 : //If Enemy's Energy is at or over 16 points
			(eEnergy > 7.9 ? 2.0 : //If Enemy's Energy is at or over 8 points
			(eEnergy > 3.9 ? 1.0 : 0.5))); //Everything else, basically
	double bPower = Math.min(MBP,getEnergy()); //Figure out the max you can spend on firing a bullet
	double bVelocity = (20 - (3 * bPower));
	//Historical Velocity Recall
	for (int i = 1; i < 9; i++) {
		if(_vCounts[i] > _MCV) {
			_MCV = _vCounts[i];
			MCV = i;
		}
	}
		
	while((++dTime) * bVelocity < Point2D.Double.distance(myX, myY, aHisX, aHisY)) {
		aHisX += (Math.sin(eHeading) * eVelocity);
		aHisY += (Math.cos(eHeading) * eVelocity);
		eHeading += eHeadingChange;
		if(aHisX < 20.0 || aHisX > (fWidth - 20.0) || aHisY < 20.0 || aHisY > (fHeight - 20.0)) {
			aHisX = Math.min(Math.max(20.0, aHisX), (fWidth - 20.0));
			aHisY = Math.min(Math.max(20.0, aHisY), (fHeight - 20.0));
			break;
		}
	}
	double theta = Utils.normalAbsoluteAngle(Math.atan2((aHisX - getX()), (aHisY - getY())));
		
	//Set and Execute Things
	setTurnGunRightRadians(Utils.normalRelativeAngle(theta - getGunHeadingRadians()));
	setTurnRadarRightRadians((rSweep)*Utils.normalRelativeAngle(aBearing - getRadarHeadingRadians()));
	execute();
		
	if(eEnergy >= (0.025 * eDistance)) { setFire(bPower); } //PowerConserve tweak
}

Currently, this targeting formula works for simple robots 90% of the time. It's rubbish against most advanced robots. Feel free to improve/comment on it so I can learn (since tutorials are utterly useless on me, I need visual aids).