User:Nat/Free code

From Robowiki
< User:Nat
Revision as of 12:02, 2 April 2009 by Nat (talk | contribs) (add new snippet)
Jump to navigation Jump to search

Here I'll put all my code snippets. No license apply.

Extended Point2D

Here my version of extended Point2D. It can convert vector into location within its constructor and provide angleTo function, which apply to robocode only. I found it useful to keep enemy data in melee with Kawigi style. (class EnemyInfo extends Point2D.Double)

public class ExtendedPoint2D extends Point2D.Double {
	private static final long serialVersionUID = 74324L;
	public ExtendedPoint2D(double x, double y) {
		super(x, y);
	}
	
	public ExtendedPoint2D(Point2D location, double angle, double dist) {
		double enemyX = location.getX() + Math.sin(angle) * dist;
		double enemyY = location.getY() + Math.cos(angle) * dist;
		x = enemyX;
		y = enemyY;
	}
	
	public void setVectorLocation(double angle, double dist) {
		double enemyX = x + Math.sin(angle) * dist;
		double enemyY = y + Math.cos(angle) * dist;
		x = enemyX;
		y = enemyY;
	}
	
	public double angleTo(Point2D location) {
		double ex = location.getX();
		double ey = location.getY();
		return Math.atan2(ex - x, ey - y);
	}
	
	public double angleTo(double x, double y) {
		return angleTo(new Point2D.Double(x, y));
	}
}

The variable name is a bit confuse because I apply term 'enemy' where it should ve only another location, but I can't think better name.

XXX State

An incomplete state classes set. This is to be part of Asteroid framework, but the framework isn't ready yet so I released here before.

class State {
	public static class PlaceState extends Point2D.Double {
		private static final long serialVersionUID = -6610510098428272571L;
		public int round;
		public int time;
		
		public PlaceState(double x, double y, int round, int time) {
			super(x, y);
			this.round = round;
			this.time = time;
		}
		
		public PlaceState(double x, double y, int round, long time) {
			this(x, y, round, (int) time);
		}
		
		public double getBearingTo(PlaceState ps) {
			return Math.atan2(ps.x - x, ps.y - y);
		}
	}
	
	public static class MovingPlaceState extends PlaceState {
		private static final long serialVersionUID = 1L;
		public double velocity, heading;
		
		public MovingPlaceState(double x, double y, int round, int time,
				double heading, double velocity) {
			super(x, y, round, time);
			this.velocity = velocity;
			this.heading = heading;
		}
		
		public MovingPlaceState(double x, double y, int round, long time,
				double heading, double velocity) {
			this(x, y, round, (int) time, heading, velocity);
		}
	}
	
	public static class RobotState extends MovingPlaceState {
		private static final long serialVersionUID = -6759124718664347016L;
		
		public RobotState(double x, double y, int round, int time,
				double heading, double velocity) {
			super(x, y, round, time, heading, velocity);
		}
		
		public RobotState(double x, double y, int round, long time,
				double heading, double velocity) {
			this(x, y, round, (int) time, heading, velocity);
		}
		
		public double getRelativeBearingTo(PlaceState ps) {
			return Utils.normalRelativeAngle(getBearingTo(ps) - heading);
		}
		
		public double getLateralVelocityOf(RobotState rs) {
			return rs.velocity * Math.sin(rs.heading - getBearingTo(rs));
		}
		
		public double getAdvancingVelocityOf(RobotState rs) {
			return rs.velocity * -Math.cos(rs.heading - getBearingTo(rs));
		}
		
		public double getLateralVelocityFrom(RobotState rs) {
			return velocity * Math.sin(getBearingTo(rs));
		}
		
		public double getAdvancingVelocityFrom(RobotState rs) {
			return velocity * Math.cos(getBearingTo(rs));
		}
	}
}
Description

All method name is self-explaining except the get.......Velocity[From|Of](.....). Let me explain here. get.......VelocityOf(.....) returns a ....... velocity of passed state from this state where From method returns a ....... velocity of this state from passed state. (It look silly since you can switch calling from another RobotState instead)

(more to be come)