User:Wompi/MeleeRadar

From Robowiki
Jump to navigation Jump to search

I'm playing with a new (for me) radar right now and i think it is worth to share it.

It's a radar mostly usefull for melee bots and it scans all bots forth and back. If the radar founds all enemys in under 180 deg, the radar will change direction and search new. If only one enemy is left the radar goes to endless lock the enemy.

pro: todo

contra: todo

enhancements: todo

public class MeleeRadar 
{
    private static final double PI = Math.PI;
    
    AdvancedRobot myRobot;
    HashSet<String> scannedEnemys = new HashSet<String>();
    int noRadarTick;
    double startRadarHead;
		
    public MeleeRadar(AdvancedRobot robot)
    {
        myRobot = robot;
    }
	
    public void init()
    {		
        myRobot.setTurnRadarRightRadians(Double.MAX_VALUE);
	startRadarHead = myRobot.getRadarHeadingRadians();
    }
	
    public void onStatus(StatusEvent event) 
    {
        double rHead = myRobot.getRadarHeadingRadians(); 
	noRadarTick++;
		
	for (RobotDeathEvent e : myRobot.getRobotDeathEvents())
	{
	    scannedEnemys.remove(e.getName());
	}
		
	for (ScannedRobotEvent e : myRobot.getScannedRobotEvents()) 
	{
            if (scannedEnemys.add(e.getName()))
            {
                double SCAN_DIR = Math.signum(myRobot.getRadarTurnRemainingRadians());
	        double deltaRadar = Utils.normalAbsoluteAngle((rHead - startRadarHead) * SCAN_DIR);

		if (scannedEnemys.size() == myRobot.getOthers())
		{
	             scannedEnemys.clear();
		     if (deltaRadar <= PI)
		     {	
	                 myRobot.setTurnRadarLeftRadians(myRobot.getRadarTurnRemainingRadians());
		     }
		     startRadarHead = rHead;

	             if (myRobot.getScannedRobotEvents().size() < myRobot.getOthers())  
	             {
		         onStatus(null);   // recursion be carefull 
	             }
		}				
	   }
           noRadarTick = 0;
       }
		
       if (noRadarTick >= 2)
       {
           scannedEnemys.clear();
           startRadarHead = rHead;
       }
  }
}