GITS/Targeting/Source

From Robowiki
< GITS‎ | Targeting
Revision as of 03:25, 15 May 2009 by J Litewski (talk | contribs)
Jump to navigation Jump to search

GITS Targeting (Source) - from version 0.5-051409


public void onScannedRobot(ScannedRobotEvent e) {
	double rSweep = 0.99; //RadarSweep tweak
	double bPower = Math.min(3.0,getEnergy());
	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 eHeading = e.getHeadingRadians();
	double eHeadingChange = eHeading - _eHeading;
	_eHeading = eHeading;
	double eVelocity = e.getVelocity();
	double eDistance = e.getDistance();
	double eEnergy = e.getEnergy();
	double dTime = 0;
	double fHeight = getBattleFieldHeight(),
		   fWidth = getBattleFieldWidth();
	double aHisX = hisX, aHisY = hisY;
		
	while((++dTime) * (20.0 - 3.0 * bPower) < Point2D.Double.distance(myX, myY, aHisX, aHisY)) {
		aHisX += (Math.sin(eHeading) * eVelocity);
		aHisY += (Math.cos(eHeading) * eVelocity);
		eHeading += eHeadingChange;
		if(aHisX < 18.0 || aHisX > (fWidth - 18.0) || aHisY < 18.0 || aHisY > (fHeight - 18.0)) {
			aHisX = Math.min(Math.max(18.0, aHisX), (fWidth - 18.0));
			aHisY = Math.min(Math.max(18.0, aHisY), (fHeight - 18.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)) { fire(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).