var forward = true;
var rotateClockwise = true;
var turnClockwise = true;
var correctCannonForTurning = true;
var attackClone = false;
var startedScanClockwise = true;
var spotted = false;
var moveSpeed = 3;
var rotateSpeed = 2;
var turnSpeed = 0;
var scanning = true;
var scanAngle = 15;
var defaultScanAngle = 15;
var scannedClockwise = 0;
var scannedCounterclockwise = 0;

var Robot = function(robot) 
{

};

Robot.prototype.onIdle = function(ev) 
{
	var robot = ev.robot;
	move(ev);
	rotate(ev);
	turn(ev);
	if(scanning)
		scan(ev);
};

Robot.prototype.onScannedRobot = function(ev) 
{
	if(!attackClone && ev.scannedRobot.parentID!=null)
		return;
	ev.robot.fire();
	spotted = true;
	scanAngle = defaultScanAngle;
	startedScanClockwise = rotateClockwise;
	scannedClockwise = 0;
	scannedCounterclockwise = 0;
};

Robot.prototype.onRobotCollision = function(ev)
{
	forward = !(forward);
};

Robot.prototype.onWallCollision = function(ev)
{
	forward = !(forward);
};

Robot.prototype.onHitByBullet = function(ev) 
{

};

function move(ev)
{
	if(forward)
		ev.robot.ahead(moveSpeed);
	else
		ev.robot.back(moveSpeed);
};

function rotate(ev)
{
	if(rotateClockwise)
		ev.robot.rotateCannon(rotateSpeed);
	else
		ev.robot.rotateCannon(-rotateSpeed);
};

function turn(ev)
{
	if(turnClockwise)
		ev.robot.turn(turnSpeed);
	else
		ev.robot.turn(-turnSpeed);
	if(!correctCannonForTurning)
		return;
	if(turnClockwise)
		ev.robot.rotateCannon(-turnSpeed);
	else
		ev.robot.rotateCannon(turnSpeed);
};

function scan(ev)
{
	if(!spotted) return;
	
	if(rotateClockwise)
	{
		scannedClockwise++;
	}else{
		scannedCounterclockwise++;
	}
	
	if(scannedClockwise>=scanAngle && scannedCounterclockwise>=scanAngle)
	{
		spotted = false;
		scannedClockwise = 0;
		scannedCounterclockwise = 0;
		return;
	}
	
	if(startedScanClockwise && scannedClockwise>=(scanAngle/2))
	{
		rotateClockwise = false;
		if(scannedCounterclockwise >= scanAngle)
			rotateClockwise = true;
		scanAngle+=3;
	}else if(!startedScanClockwise && scannedCounterclockwise>=(scanAngle/2)) 
	{
		rotateClockwise = true;
		if(scannedClockwise >= scanAngle)
			rotateClockwise = false;
		scanAngle+=3;
	}
};