Archived talk:User:Awesomeness 20090504

From Robowiki
Jump to navigation Jump to search
       Archive        This is an archive of past discussions. Do not edit the contents of this page. If you wish to start a new discussion or revive an old one, please do so on the current talk page.     

Okay, I need to make a list of bullets. I have made a bullet class and everything, but I need a special list that will let me access all of the bullets in a while/for loop and be able to add and remove them at any time and never mess up. If I did this in a normal array I could do a for loop and access using an increasing number, but I wouldn't know when to stop. In a while loop, if I used while(bulletsArray[x] != null) and then increment x, after I remove my first bullet that say, hit a wall, it will stop there. I need help!

Thanks,
Awesomeness 14:02, 2 May 2009 (UTC)

I think this will do:

ArrayList<Bullet> bullets = new ArrayList<Bullet>();
for (int i = 0; i < bullets.size(); i++) {
    Bullet bullet = bullets.get(i);
    if (bullet.needRemove()) {
        // I think this will work
        // bullets.remove(i--);
        // but usually I do
        bullets.remove(bullet);
        i--;
    }
}

Here I assume that you use ArrayList to store you bullets and your bullet's classname is Bullet. If you use a plain array to keep the bullets, consider change to ArrayList. It is more flexible. » Nat | Talk » 14:09, 2 May 2009 (UTC)

Oh my gosh! You're right! ArrayList is way more flexible! I looked at the documentation; you don't even have to increment or decrement anything! The function remove() shifts the other elements to the left already! Thanks! Awesomeness 14:14, 2 May 2009 (UTC)
Be sure don't use for-each style (for(Bullet bullet : bullets) or you will get ConcurentModificationException when you remove element (this is per java spec, but I still use the for-each style and didn't get this exception actually). And when you scroll through the array like one I mentioned, be sure you do i--, or you will just skip one element. » Nat | Talk » 14:19, 2 May 2009 (UTC)
Oh, you're right. Awesomeness 14:27, 2 May 2009 (UTC)
One suggestion. Don't use bullets.remove(bullet);, use bullets.remove(i); instead because removing by index is far faster than removing by content. Well, with the size the list is in robocode it probably doesn't matter a ton, but removal of list by content, when you already have the index on hand, seems terribly wasteful to me. --Rednaxela 14:45, 2 May 2009 (UTC)
I changed that myself already. =D lol Awesomeness 14:56, 2 May 2009 (UTC)

I've grown very fond of iterators for Lists, this would be my implementation:

ArrayList<Bullet> bullets = new ArrayList<Bullet>();
Iterator<Bullet> i = bullets.iterator();
while (i.hasNext()) {
    Bullet bullet = i.next();
    if (bullet.needRemove()) {
        i.remove();
    }
}

--Skilgannon 15:00, 2 May 2009 (UTC)

Ahhh... I never knew that iterator has remove method so I always using ArrayLisy.get(i) to prevent ConcurentModificationException. Thanks you very much. » Nat | Talk » 15:06, 2 May 2009 (UTC)
Actually, I'm very fond of them myself, and what gives you the best asymptotic speed is using LinkedList with Iterator.remove(), because with that method is never needs to recopy parts of the list to shift them over. Note, 'best asymptotic speed', means that it's the fastest for very very large numbers of list elements, and that if the list is small ArrayList/iterator is faster. To find out what "small" is defined as, one would need to benchmark it, and it is somewhat dependant on the particular computer. Also ArrayList/index is slightly faster than ArrayList/iterator version in all cases because the ArrayList/iterator version incurs overhead for function calls but in effect does the same thing. --Rednaxela 19:16, 2 May 2009 (UTC)

How do you find the x and y of a robot you've scanned? I see no ScannedRobotEvent.getX() or getY() method. Awesomeness 15:19, 2 May 2009 (UTC)

Trig Trig Trig...

double absBearing = e.getBearingRadians() + getHeadingRadians();
double x = getX() + Math.sin(absBearing) * e.getDistance();
double y = getY() + Math.cos(absBearing) * e.getDistance();

That's all. » Nat | Talk » 15:21, 2 May 2009 (UTC)

Here's a method I use in my utils class (I first saw it in a PEZ bot):

    public static Point2D.Double project(Point2D.Double sourceLocation, 
        double angle, double length) {
        return new Point2D.Double(sourceLocation.x + Math.sin(angle) * length,
            sourceLocation.y + Math.cos(angle) * length);
    }
    // You'd pass it something like: 
    // myLocation = new Point2D.Double(getX(), getY()); 
    // enemyLocation = MyUtils.project(myLocation, absBearing, e.getDistance());

--Voidious 16:15, 2 May 2009 (UTC)


Lastly, I need a way to "dodge" these simulated bullets and to figure out what angle the oppenent bot would be shooting from if it used linear targeting against me. I don't know how to do either of these. Awesomeness 16:31, 2 May 2009 (UTC)

To dodge, use Anti-Gravity Movement, to assume linear firing, use this code:

angle = Utils.normalRelativeAngle(absBearing +  Math.PI + Math.asin((Math.sin(e.getBearingRadians()) * getVelocity())/(20 - 3 * BULLET_POWER)));

replace BULLET_POWER to which you get from energy drop. This prediction use non-iterative no-wall-stopping linear targeting. » Nat | Talk » 16:35, 2 May 2009 (UTC)

Thanks! This fits exactly what I was planning to do! Awesomeness 16:44, 2 May 2009 (UTC)
However anti-gravity confuses me greatly... Awesomeness 17:11, 2 May 2009 (UTC)


UGH! I worked so hard on this and look what it does! Enable the debug graphics to see where it thinks the bullets are... Theres no point in working on antigrav movement until I fix this. Program:

package awesomeness;
import robocode.*;
import robocode.util.*;
import java.util.Random;
import java.util.ArrayList;
import java.awt.*;

/**
 * PwnBot - a robot by (your name here)
 */
public class PwnBot extends AdvancedRobot {
	
	static StringBuffer pattern = new StringBuffer("" + (char)0 + (char)0 + (char)0 + (char)0 + (char)0 + (char)0 + (char)0 + (char)0 + (char)0 + (char)0 + (char)0 + (char)0 + (char)0 + (char)0 + (char)0 + (char)0 + (char)0 + (char)0 + (char)0 + (char)0 + (char)0 + (char)0 + (char)-8 + (char)-7 + (char)-6 + (char)-5 + (char)-4 + (char)-3 + (char)-2 + (char)-1 + (char)1 + (char)2 + (char)3 + (char)4 + (char)5 + (char)6 + (char)7 + (char)8);
	
	static double previousEnergy = 100d;
	static double changeInEnergy;
	//static double totalBulletDist;
	//double[] bulletDists;
	
	
	static int movementDirection = 50;
	static long lastTime; //The last time known, used to track bullets
	
	static Random generator = new Random(); //This makes random numbers
	ArrayList<VirtualBullet> bullets = new ArrayList<VirtualBullet>();
	static VirtualBullet newBullet, bullet;
	
	public void run() {
		lastTime = 0;
		setAdjustGunForRobotTurn(true);
		turnRadarRightRadians(Double.POSITIVE_INFINITY);
	}

	/**
	 * onScannedRobot: What to do when you see another robot
	 */
	public void onScannedRobot(ScannedRobotEvent e) {
		
		//The absolute bearing, this is used a lot
		double absoluteBearing = getHeadingRadians() + e.getBearingRadians(); ;
		
		///////////////////////////////////////////////////////
		////////////////////Movement Code//////////////////////
		
		/////////////////////Bullet Code///////////////////////
		
		//If there's a change in energy, it probably fired

		if ((changeInEnergy = previousEnergy - (previousEnergy = e.getEnergy())) > 0d && changeInEnergy<=3.01) {
			
			newBullet = new VirtualBullet(getX() + Math.sin(absoluteBearing) * e.getDistance(), getY() + Math.cos(absoluteBearing) * e.getDistance(), 20 - 3 * changeInEnergy, Utils.normalRelativeAngle(absoluteBearing +  Math.PI + Math.asin((Math.sin(e.getBearingRadians()) * getVelocity())/(20 - 3 * changeInEnergy))));
			newBullet.tick(1);
			bullets.add(newBullet);
			
			
		}
		
		
		for (int i = 0; i < bullets.size(); i++) {
			bullet = bullets.get(i);
			bullet.tick(getTime() - lastTime);
			bullets.set(i, bullet);
			
			if (bullets.get(i).checkHitWall()) {
				// I think this will work
				// bullets.remove(i--);
				// but usually I do
				bullets.remove(i);
				i--;
			}
			
			//bulletDists[i] = bullets.get(i).getDistance(getX(), getY());
			//totalBulletDist += bullets.get(i).getDistance(getX(), getY());
			
		}
		
		///////////////////End Bullet Code/////////////////////
		
		
		//Stay at almost right angles
		//setTurnRightRadians(Math.cos(absoluteBearing+(-0.003*movementDirection)));
		
		//setAhead(movementDirection);
		
		
		
		////////////////////Movement Code//////////////////////
		///////////////////////////////////////////////////////
		
		///////////////////////////////////////////////////////
		///////////////////////Gun Code////////////////////////
		
		double absbearing;
		absbearing = (e.getBearingRadians()-Math.PI/2)+1.57079633;
		//rounding to be more accurate in projection!
		pattern.insert(0, (char)(Math.round(Math.sin(e.getHeadingRadians() - (absbearing = absbearing + getHeadingRadians()))*e.getVelocity())));
		
		int index=0, searchlength = 30;
		//lol, if I ever make a haiku pattern-matcher, this will be in it:
		while ((index = pattern.toString().indexOf(pattern.substring(0, searchlength--), 1)) < 0);
		
		//searchlength will now become the index of the StringBuffer that I will project back to (back because my pattern 
		//is stored backward).
		double power;
		double dist;
		searchlength =  index - (int)((dist = e.getDistance())/(20-(power = Math.min(3, Math.min(getEnergy(), e.getEnergy())/4))*3));
		
		//just add the offset to the bearing instead of making a new variable!
		//The fact that this actually reconstructs future movement (like a normal pattern-matcher does) probably makes this
		//just about the most accurate current nano pattern-matcher (except possibly Kakuru's, since it uses doubles instead of
		//characters).  The nice thing about it is that it correctly projects patterns even if I'm at a different distance than
		//when I collected the pattern.
		do
		{
			absbearing += Math.asin(((byte)pattern.charAt(index--))/dist);
		}
		while (index >= Math.max(0, searchlength));
		
		setTurnGunRightRadians(robocode.util.Utils.normalRelativeAngle(absbearing - getGunHeadingRadians()));
		setFire(power);
		
		
		///////////////////////Gun Code////////////////////////
		///////////////////////////////////////////////////////
		
		
		
		///////////////////////////////////////////////////////
		/////////////////////Radar Code////////////////////////
		
		setTurnRadarLeftRadians(getRadarTurnRemainingRadians());								
		
		
		/////////////////////Radar Code////////////////////////
		///////////////////////////////////////////////////////	
		
		
		lastTime = getTime();
		
	}		

	public void onHitWall(HitWallEvent e) {
		i();
	}
	
	public void i() {
		movementDirection = -movementDirection;
	}

	// Paint a transparent square on top of the last scanned robot
	public void onPaint(Graphics2D g) {
		// Set the paint color to a red half transparent color
		g.setColor(new Color(0xff, 0x00, 0x00, 0x80));

		for (int i = 0; i < bullets.size(); i++) {
			bullet = bullets.get(i);
			
			
			g.drawLine( (int) bullet.getX(), (int) bullet.getY(), (int)getX(), (int)getY());
			
			// Draw a filled square on top of the scanned robot that covers it
			g.fillRect( (int) bullet.getX() - 5, (int) bullet.getY() - 5, 10, 10);
			
			
		}
	}
	
	
	public class VirtualBullet {  //A class to simulate bullets.
		
		double x, y, speed, angle;
		
		
		VirtualBullet(double startX, double startY, double velocity, double trajectory) {
			x = startX;
			y = startY;
			speed = velocity;
			angle = trajectory;
		}
		
		public void tick(long ticks) {  //Moves the bullet the amount it would
			while(ticks > 0) {
				x += Math.sin(angle)*speed;
				y += Math.cos(angle)*speed;
				ticks --;
			}
		}
		
		public double getX() {
			return x;
		}
		
		public double getY() {
			return y;
		}
		
		public double getDistance(double botX, double botY) {
			return Math.sqrt(Math.pow(botX-x, 2)+Math.pow(botY-y, 2));
		}
		
		
		public boolean checkHitWall() {
			if (x < 0 || y < 0 || x > getBattleFieldWidth() || y > getBattleFieldHeight()) {
				return true;
			}
			return false;
		}
		
	}
			
}
								

=( Awesomeness 17:28, 2 May 2009 (UTC)

First, Robobocode trig is not same as your Math class trig. It should be:


		
		public void tick(long ticks) {  //Moves the bullet the amount it would
			while(ticks > 0) {
				x += Math.sin(angle)*speed;
				y += Math.cos(angle)*speed;
				ticks --;
			}
		}

And when you detect energy drop, bullet already move one tick!, mean that you must advace bullet one tick when you add it. And, the absBearing you use in position calculation is not yet an absoluteBearing since it just e.getBearingRadians() because I use codesize trick. But if you are creating megabot, try remove all the codesize trick and make the code more readable. (or less ugly) Few suggestion:

  • Check for energy change in onBulletHit, onHitByBullet too because the robot loss/gain energy when hitbybullet/bullethit.
  • use more acculate radar lock, or you can't detect bullet acculately.

» Nat | Talk » 02:27, 3 May 2009 (UTC)

I have partially fixed it. It still fails miserably. I added a bit more debug graphics, which attempts to make a line from the bullet to PwnBot, but it doesn't work either. This confuses me... One end of the line should be touching my bot.
What you fixed? I fix to this code and it work correct as far as I can tell. I cannot be 100% accurate. I think just 80% is very good. This is my modified code:
package awesomeness;
import robocode.*;
import robocode.util.*;
import java.util.Random;
import java.util.ArrayList;
import java.awt.*;

/**
 * PwnBot - a robot by (your name here)
 */
public class PwnBot extends AdvancedRobot {
	
	static StringBuffer pattern = new StringBuffer("" + (char)0 + (char)0 + (char)0 + (char)0 + (char)0 + (char)0 + (char)0 + (char)0 + (char)0 + (char)0 + (char)0 + (char)0 + (char)0 + (char)0 + (char)0 + (char)0 + (char)0 + (char)0 + (char)0 + (char)0 + (char)0 + (char)0 + (char)-8 + (char)-7 + (char)-6 + (char)-5 + (char)-4 + (char)-3 + (char)-2 + (char)-1 + (char)1 + (char)2 + (char)3 + (char)4 + (char)5 + (char)6 + (char)7 + (char)8);
	
	static double previousEnergy = 100d;
	static double changeInEnergy;
	//static double totalBulletDist;
	//double[] bulletDists;
	
	
	static int movementDirection = 50;
	static long lastTime; //The last time known, used to track bullets
	
	static Random generator = new Random(); //This makes random numbers
	ArrayList<VirtualBullet> bullets = new ArrayList<VirtualBullet>();
	static VirtualBullet newBullet, bullet;
	
	public void run() {
		lastTime = 0;
		setAdjustGunForRobotTurn(true);
		setAdjustRadarForGunTurn(true);
		setAdjustRadarForRobotTurn(true);
		while(true)
		turnRadarRightRadians(Double.POSITIVE_INFINITY);
	}

	/**
	 * onScannedRobot: What to do when you see another robot
	 */
	public void onScannedRobot(ScannedRobotEvent e) {
		
		//The absolute bearing, this is used a lot
		double absoluteBearing = e.getBearingRadians() + getHeadingRadians();
		
		///////////////////////////////////////////////////////
		////////////////////Movement Code//////////////////////
		
		/////////////////////Bullet Code///////////////////////
		
		//If there's a change in energy, it probably fired

		if ((changeInEnergy = previousEnergy - (previousEnergy = e.getEnergy())) > 0d && changeInEnergy<=3) {
			
			newBullet = new VirtualBullet(getX() + Math.sin(absoluteBearing) * e.getDistance(), getY() + Math.cos(absoluteBearing) * e.getDistance(), 20 - 3 * changeInEnergy, Utils.normalRelativeAngle(absoluteBearing +  Math.PI + Math.asin((Math.sin(e.getBearingRadians()) * getVelocity())/(20 - 3 * changeInEnergy))));
			bullets.add(newBullet);
			
			
		}
		
		
		for (int i = 0; i < bullets.size(); i++) {
			bullet = bullets.get(i);
			bullet.tick(getTime() - lastTime);
			bullets.set(i, bullet);
			
			if (bullets.get(i).checkHitWall()) {
				// I think this will work
				// bullets.remove(i--);
				// but usually I do
				bullets.remove(i);
				i--;
			}
			
			//bulletDists[i] = bullets.get(i).getDistance(getX(), getY());
			//totalBulletDist += bullets.get(i).getDistance(getX(), getY());
			
		}
		
		///////////////////End Bullet Code/////////////////////
		
		
		//Stay at almost right angles
		setTurnRightRadians(Utils.normalRelativeAngle(absoluteBearing + Math.PI/2 - getHeadingRadians()));
		
		setAhead(movementDirection);
		
		if (Math.random() > 0.92) i();
		
		
		
		////////////////////Movement Code//////////////////////
		///////////////////////////////////////////////////////
		
		///////////////////////////////////////////////////////
		///////////////////////Gun Code////////////////////////
		
		double absbearing;
		absbearing = absoluteBearing;
		//rounding to be more accurate in projection!
		pattern.insert(0, (char)(Math.round(Math.sin(e.getHeadingRadians() - (absbearing))*e.getVelocity())));
		
		int index=0, searchlength = 30;
		//lol, if I ever make a haiku pattern-matcher, this will be in it:
		while ((index = pattern.toString().indexOf(pattern.substring(0, searchlength--), 1)) < 0);
		
		//searchlength will now become the index of the StringBuffer that I will project back to (back because my pattern 
		//is stored backward).
		double power;
		double dist;
		searchlength =  index - (int)((dist = e.getDistance())/(20-(power = Math.min(3, Math.min(getEnergy(), e.getEnergy())/4))*3));
		
		//just add the offset to the bearing instead of making a new variable!
		//The fact that this actually reconstructs future movement (like a normal pattern-matcher does) probably makes this
		//just about the most accurate current nano pattern-matcher (except possibly Kakuru's, since it uses doubles instead of
		//characters).  The nice thing about it is that it correctly projects patterns even if I'm at a different distance than
		//when I collected the pattern.
		do
		{
			absbearing += Math.asin(((byte)pattern.charAt(index--))/dist);
		}
		while (index >= Math.max(0, searchlength));
		
		setTurnGunRightRadians(robocode.util.Utils.normalRelativeAngle(absbearing - getGunHeadingRadians()));
		setFire(power);
		
		
		///////////////////////Gun Code////////////////////////
		///////////////////////////////////////////////////////
		
		
		
		///////////////////////////////////////////////////////
		/////////////////////Radar Code////////////////////////
		
		
		setTurnRadarRightRadians(2 * Utils.normalRelativeAngle(absoluteBearing - getRadarHeadingRadians()));
		
		
		/////////////////////Radar Code////////////////////////
		///////////////////////////////////////////////////////	
		
		
		lastTime = getTime();
		
	}		

	public void onHitWall(HitWallEvent e) {
		i();
	}
	
	public void i() {
		movementDirection = -movementDirection;
	}

	// Paint a transparent square on top of the last scanned robot
	public void onPaint(Graphics2D g) {
		// Set the paint color to a red half transparent color
		g.setColor(new Color(0xff, 0x00, 0x00, 0x80));

		for (int i = 0; i < bullets.size(); i++) {
			bullet = bullets.get(i);
			
			// Draw a filled square on top of the scanned robot that covers it
			g.fillRect( (int) bullet.getX() - 5, (int) bullet.getY() - 5, 10, 10);
			
			
		}
	}

	public void onHitByBullet(HitByBulletEvent e) { previousEnergy += 3 * e.getBullet().getPower(); }
	public void onBulletHit(BulletHitEvent e) { previousEnergy -= 4 * e.getBullet().getPower() + Math.max(0,2 * (e.getBullet().getPower() - 1)); }
	public void onHitRobot(HitRobotEvent e) { previousEnergy -= 0.6; }
	
	
	public class VirtualBullet {  //A class to simulate bullets.
		
		double x, y, speed, angle;
		
		
		VirtualBullet(double startX, double startY, double velocity, double trajectory) {
			x = startX;
			y = startY;
			speed = velocity;
			angle = trajectory;
		}
		
		public void tick(long ticks) {  //Moves the bullet the amount it would
			while(ticks > 0) {
				x += Math.sin(angle)*speed;
				y += Math.cos(angle)*speed;
				ticks --;
			}
		}
		
		public double getX() {
			return x;
		}
		
		public double getY() {
			return y;
		}
		
		public double getDistance(double botX, double botY) {
			return Math.sqrt(Math.pow(botX-x, 2)+Math.pow(botY-y, 2));
		}
		
		
		public boolean checkHitWall() {
			if (x < 0 || y < 0 || x > getBattleFieldWidth() || y > getBattleFieldHeight()) {
				return true;
			}
			return false;
		}
		
	}
			
}
I bet it 90% accurate, but you may see it doesn't accurate because enemy cannot turn the gun to it aim position in time before it fire. » Nat | Talk » 06:08, 3 May 2009 (UTC)

THANK YOU

Thank you guys! All of you! Especially you, Nat! I've made the official working code, all I have to do now is make them anti-gravity points! You fixed almost everything, all I had to do was inverse the Y axis for some reason. I don't know enough trig to fix whatever formula's going wrong. But thank you! YAY!

--Awesomeness 17:09, 3 May 2009 (UTC)

I just was browsing on the wiki... =( My bullet dodging idea isn't original or new... It's just this thing called "ShrapnelDodging"... Aww... --Awesomeness 17:23, 3 May 2009 (UTC)
Robocode's been out quite a while, so it's very difficult to find 100% original ideas. It's just the way it is, try not to let it get you down! =) Glad to hear you got your stuff working. --Voidious 17:27, 3 May 2009 (UTC)
You're right. Besides, maybe I'll even change something. Until I'm in highschool, I'll never have a full understanding of trigonometry, so I'll probably need help with that. I might use this virtual bullet engine in my other bots. It's not huge, but it'll never fit into a nano bot and probably not into a micro. Currently, without any movement and including the graphical debugging, it's 691 bytes, and I'm sure it can be compressed.

--Awesomeness 17:47, 3 May 2009 (UTC)

Actually, taking out the weak PM gun, the graphical debugging, and the weird movement I put in before I planned out the bullet sims and stuff I have now, (leaving only the basis of the bullet tracking itself) I get 409 bytes. Is that good, or is that way more than what other people have made?
Oh, I just checked the game physics, and I found out that the Y axis is well... reversed. That's weird. I don't like that. Oh well. Awesomeness 19:59, 3 May 2009 (UTC)
But that make a life more easier! » Nat | Talk » 23:45, 3 May 2009 (UTC)
I can't say it makes life easier, but I let you know that if end up going down a world with lot's of geometry, being capable of using any coordinate system is important and Robocode's system is a good example and practice field for that. --zyx 03:46, 4 May 2009 (UTC)
The atan2(x,y) seem reasonable to me more than atan2(y,x), at least for who that only know basic trig before starting robocode like me. » Nat | Talk » 06:09, 4 May 2009 (UTC)
There are no threads on this page yet.