User:Wompi/GraphicDebugRobotPath

From Robowiki
Jump to navigation Jump to search
Robot Path Debug

To me it's very useful to see how my bots move on the battle field and so i wrote a graphical debug class to visualize this. The class can be implemented with just one call to every robot and can show the own and aswell the target movement. Every target can have its own color. Its written with melee battles in mind but works for 1vs1 too.




Just call within your robot:

        ...

	public void onScannedRobot(ScannedRobotEvent event)
        {
                 ..... 
                double eX = getX() + Math.sin(getHeadingRadians() + event.getBearingRadians())*event.getDistance(); 
                double eY = getY() + Math.cos(getHeadingRadians() + event.getBearingRadians())*event.getDistance(); 
		PaintRobotPath.onPaint(getGraphics(), event.getName(), getTime(), eX, eY, Color.YELLOW);
        }

       // or for your own robot
	public void onPaint(Graphics2D g)
	{
		PaintRobotPath.onPaint(g, getName(), getTime(), getX(), getY(), Color.GREEN);
	}
package wompi.misc.painter;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.GeneralPath;
import java.util.HashMap;

public class PaintRobotPath 
{
	static HashMap<String, PathHelper> pathMap = new HashMap<String, PathHelper>();
	static long lastTime;
	
	public static void onPaint(Graphics2D g,String botName, long time, double xRobot,double yRobot, Color pathColor)
	{
		if (lastTime > time) pathMap.clear();  // new battle reset
		lastTime = time;
		
		PathHelper myPath = pathMap.get(botName);
		if (myPath == null)
		{
			myPath = new PathHelper();
			myPath.rName = botName;
			myPath.rPath = new GeneralPath(GeneralPath.WIND_EVEN_ODD, 2000); 
			myPath.rPath.moveTo (xRobot, yRobot);
			myPath.rColor = pathColor;
			pathMap.put(botName, myPath);
		}
		
		if (time - myPath.rTime >= 5)  // thin out the path  
		{
			myPath.rPath.lineTo(xRobot,yRobot);
			myPath.rTime = time;
		}
		
		for (PathHelper helper : pathMap.values()) 
		{
			if ((time - helper.rTime) >= 30) continue;   // dead robots fade away after 30 turns
			g.setColor(helper.rColor);		
			g.draw(helper.rPath);				
		}
	}
}

class PathHelper
{
	GeneralPath rPath;
	String rName;
	Color rColor;
	long rTime;
}