Pattern Match Aim in a Nanobot. Here's the code:
static final int SEARCH_DEPTH = 30; // Increasing this slows down game execution - beware! static final int BULLET_SPEED = 11; // 3 power bullets travel at this speed. static final int MAX_RANGE = 800; // Range where we're guarenteed to get a look-ahead lock // 1200 would be another good value as this is the max radar distance. // Yet too large makes it take longer to hit new movement patterns (Lemon)... static final int SEARCH_END_BUFFER = SEARCH_DEPTH + MAX_RANGE / BULLET_SPEED; // How much room to leave for leading double targetBearing = e.getBearingRadians() + getHeadingRadians(); double ArcMovement = e.getVelocity() * Math.sin(e.getHeadingRadians() - targetBearing); int matchIndex; // Cummulative radial velocity relative to us. This is the ArcLength that the enemy traces relative to us. // ArcLength S = Angle (radians) * Radius of circle. arcLength[++historyIndex] = arcLength[historyIndex - 1] + ArcMovement; // Add ArcMovement to lookup buffer. Typecast to char so it takes 1 entry. patternMatcher.append((char)(ArcMovement)); // Do adjustable buffer pattern match. Thanks Albert! (I did reduce the codesize a bit here though) do { matchIndex = patternMatcher.lastIndexOf( patternMatcher.substring(Math.max(historyIndex-searchDepth,0)), historyIndex-SEARCH_END_BUFFER); } while (--searchDepth*matchIndex < -1); // Update index to end of search matchIndex += searchDepth; // Aim at target (asin() in front of sin would be better, but no room at 3 byte cost.) setTurnGunRightRadians(Math.sin( (arcLength[matchIndex+((int)(e.getDistance()/BULLET_SPEED))]-arcLength[matchIndex])/e.getDistance() + targetBearing - getGunHeadingRadians() ));Basically, it creates a string out of the enemy's velocity relative to us. It then uses Java's built in string search ability to do an exact pattern match check for the last 30 entries. If 30 fails, then 29, then..., down to 2. At that point, it rougly assumes the bot will do what it did at the beginning of 1st round (rougly nothing.) This system turns out to be really, really good at hitting (after 1 round or so) almost all types of movement, but it tends to have trouble with bots that move more than rougly 15 ticks in the same direction beforing turning. It also takes a few shots to lock onto new patterns (such as when a robot runs out of energy and just sits there.) My fire control system helps with this alot, but increasing the search depth would also help at the cost of slowing down game execution and eventually causes missed turns (who'd have ever thought that could happen in a nano...).
Infinite Radar Locking system (Very small codesize that keeps a really good lock and has wide melee coverage.)
// Last line in Run() turnRadarRightRadians(Double.POSITIVE_INFINITY); // Last line in onScanned() // Lock Radar Infinite style. About a 99% lock rate - plus good melee coverage. setTurnRadarLeftRadians(getRadarTurnRemaining());This line allows us to remove (in run()):
setAdjustRadarForGunTurn(true); do { }while(true);and thus saving codespace. It also gives a wide scan arc which is nice.
It's movement and firepower are too simple to expand on, so if you want to see those, go download it. ;)
:) I'm amazed this bot is still doing as well as it is... --Miked0801