[Home]Falcon/WithShrinkingTricks

Robo Home | Falcon | Changes | Preferences | AllPages

        Code    Class   Class
Nr      size    size    files   Location
--------------------------------------------------------------------
1       92      921     1       Falcon$Wave.class
2       200     1905    1       Falcon.class
...and the code...

package pez.tiny;
import robocode.*;
import robocode.util.Utils;
import java.awt.geom.*;

// This code is released under the RoboWiki Public Code Licence (RWPCL), datailed on:
// http://robowiki.net/?RWPCL
//
// Falcon, by PEZ. How small can I get a GF gunning bot?  Some "ugly codesize-reduction tricks" by Kawigi
// $Id: Falcon.java,v 1.4 2004/02/16 23:35:12 peter Exp $

public class Falcon extends AdvancedRobot
{
	static final double BULLET_POWER = 3.0;
	static final double BULLET_VELOCITY = 20 - 3 * BULLET_POWER;
	
	static final int AIM_FACTORS = 25;
	static final int MIDDLE_FACTOR = (AIM_FACTORS - 1) / 2;
	
	static double enemyX;
	static double enemyY;
	static int[] aimFactors = new int[AIM_FACTORS];
	
	public void run()
	{
		turnRadarRight(Double.POSITIVE_INFINITY);
	}
	
	public void onScannedRobot(ScannedRobotEvent e)
	{
		Wave wave;
		addCustomEvent(wave = new Wave());
		double enemyAbsoluteBearing = getHeadingRadians() + e.getBearingRadians();
		enemyX = (wave.wGunX = getX()) + Math.sin(enemyAbsoluteBearing) * e.getDistance();
		enemyY = (wave.wGunY = getY()) + Math.cos(enemyAbsoluteBearing) * e.getDistance();
		
		wave.wBearingDirection = (e.getVelocity() * Math.sin(e.getHeadingRadians() - enemyAbsoluteBearing) < 0 ? -1.0/MIDDLE_FACTOR : 1.0/MIDDLE_FACTOR) ;
		wave.wBearing = enemyAbsoluteBearing;
		int mostVisited = 0;
		int i = 0;
		do
		{
			if (aimFactors[i] > aimFactors[mostVisited])
				mostVisited = i;	
			i++;
		}
		while (i < AIM_FACTORS);
		
		setTurnGunRightRadians(Utils.normalRelativeAngle(enemyAbsoluteBearing - getGunHeadingRadians() + wave.wBearingDirection * (mostVisited - MIDDLE_FACTOR)));
		setFire(BULLET_POWER);
		setTurnRadarLeft(getRadarTurnRemaining());
    }
	
	static class Wave extends Condition
	{
		double wGunX;
		double wGunY;
		double wBearing;
		double wBearingDirection;
		double wDistance;
		
		public boolean test()
		{
			if (Math.abs((wDistance += BULLET_VELOCITY) - Point2D.distance(wGunX, wGunY, enemyX, enemyY)) <= BULLET_VELOCITY / 2)
			{
				//try
				//{
					aimFactors[(int)((Utils.normalRelativeAngle(Math.atan2(enemyX - wGunX, enemyY - wGunY) - wBearing)) / wBearingDirection) + MIDDLE_FACTOR]++;
				//}
				//catch (Exception e) {}
				//removeCustomEvent(this);
		    }
			return false;
		}
	}
}

Neat. I really don't see anything ugly there. Nothing that I wouldn't consider doing at least. I think you might just have given me some valuable bytes in Aristocles. Well, you should probably remove those events you register. So 4 bytes are ugly then. =) -- PEZ

Fair enough. But the wave hit detection might be better on that version, too. I'm also working out a VB version, which is ever so slightly smaller, but quite a bit slower, too. Also, the ugly 4 bytes are packaged with another 3 to 5 probably for having to un-static the Wave class. -- Kawigi

Wow. I didn't notice you have put static there. What does that do anyway? I have never figured static inner classes. I'm still quite new to Java you know... -- PEZ

as far as i can tell,it makes it so removeCustomEvent(this) doesn't work any more--andrew

That's correct. Inner classes are already sort of a funny thing when it comes to OO-ness... basically a non-static inner class can access non-static members and methods in the containing class, even if they're private. a static inner class can probably still access private static members and methods in the containing class, but is a little more seperated and not tied to a specific instance of the class. Therefore, the removeCustomEvent method won't work anymore, because you're not tied to a specific Robot object to call it on. You've actually used static inner classes before - that's what Point2D.Double and Rectangle2D.Double are. -- Kawigi

How much does it cost to keep a reference to the bot in the wave then? -- PEZ

wouldn't it make it a lot smaller if you took out the variables defining stuff like 25 and 12? --andrew

@PEZ - I think I tried it and it was the same as just making it non-static.

@andrew - those "variables" are constants, and the compiler substitutes the actual numbers in, so unless there is something that can't be determined at compile time, it's all the same as if we hadn't declared them by name. -- Kawigi

Except it's way more readable with names on the magic numbers! -- PEZ

thats cool. I didn't know that constants worked that way . thanks Kawigi/PEZ. --andrew

It's one of the big shortcomings of Java if you ask me. "static final" instead of "const" or something clear like that. It goes for many things. That "static" keyword is used everywhere for different things. Yuk! -- PEZ

(edit)I am an idiot.... I "edited" my posting and screwed things up. Fixed now, the second snippet is cheaper.

		int mostVisited = 0;
		int i = 0;
		do
		{
			if (aimFactors[i] > aimFactors[mostVisited])
				mostVisited = i;	
			i++;
		}
		while (i < AIM_FACTORS);
will be cheaper as
		int mostVisited = 0;
		int i = AIM_FACTORS;
		do
		{
			i--;

			if (aimFactors[i] > aimFactors[mostVisited])
				mostVisited = i;	
		}
		while (i > 0);

-- Kuuran

Nemo :-) --David Alves

Can Nemo match Falcon's TC results? -- PEZ

I don't see Falcon on the table of TargetingChallenge/Results ... Nemo got 83.59 --David Alves

No, I have a bug in Falcon that makes it misbehave badly against TheArtOfWar so a TC result will have to wait unfortunately. But with the bug it gets 78 something in TargetingResults/FastLearning?. I haven't recalculated it for a potential score with bug-free TAOW handling. I think maybe the bug could affect performance against the other bots too, maybe. 83.6 is a great result. That means your GF gun really works. Something I actually doubted. I tried doing Waves like you do them there in an early hopless release of BlackWidow... But it obviously was due to bad implementation. Now I must really check Nemo's code. Can I borrow the gun for a nano with a different movement? -- PEZ

That's up to nano, he did most of it. I just did some codesize reduction. :-P --David Alves

I'll do my own nano-gun. Smaller than Nemo's and segmented. It wouldn't be any fun for you two to quake nano-land with Nemo 2.0 without a fight, would it? Alea jacta est! -- PEZ

FWIW, in Java, "static" means "implemented in the class, not the instance(s)", and final means "can't be changed after object instantiation". "static final" for constant is natural enough. In general, making things static should save memory, because everything in an instance also has to be in the class (though this could vary by JVM I guess).

You can save about 3 bytes if you take this out:

wave.wBearingDirection = (e.getVelocity() * Math.sin(e.getHeadingRadians() - enemyAbsoluteBearing) < 0 ? -1.0/MIDDLE_FACTOR : 1.0/MIDDLE_FACTOR) ;
and stick it in here:
setTurnGunRightRadians(Utils.normalRelativeAngle(enemyAbsoluteBearing - getGunHeadingRadians() + wave.wBearingDirection * (mostVisited - MIDDLE_FACTOR)));
giving you
setTurnGunRightRadians(Utils.normalRelativeAngle(enemyAbsoluteBearing - getGunHeadingRadians() + (wave.wBearingDirection = (e.getVelocity() * Math.sin(e.getHeadingRadians() - enemyAbsoluteBearing) < 0 ? -1.0/MIDDLE_FACTOR : 1.0/MIDDLE_FACTOR) ) * (mostVisited - MIDDLE_FACTOR)));

Robo Home | Falcon | Changes | Preferences | AllPages
Edit text of this page | View other revisions
Last edited June 3, 2008 7:17 EST by adsl-69-111-56-9.dsl.pltn13.pacbell.net (diff)
Search: