RoboCode Reborn
About
WartHogNG is a Robocode bot — an autonomous tank AI that fights other bots in a 2D arena. It evolved from WartHog, a bot I wrote in 2001 with a law-of-sines firing solution. The NG version replaces that with KNN adaptive targeting: a KD-tree that learns enemy movement patterns in real time and predicts where to shoot based on the most similar situations it has seen before.
How WartHogNG Works
Targeting: KNN Adaptive Gun
Every time WartHogNG fires, it records a 6-dimensional snapshot of the situation: distance, lateral velocity, advancing velocity, time since last direction change, wall proximity, and acceleration. When the bullet arrives (or misses), it records the actual angular offset the enemy ended up at — the guess factor.
To aim the next shot, it queries the KD-tree for the 8 most similar past situations and takes a weighted average of their guess factors. Early in the battle it blends with a linear predictor; after ~20 observations it relies entirely on KNN.
The 6D Feature Space
| Dimension | Range | Why it matters |
|---|---|---|
| Distance | 0–1 | Closer enemies have less time to dodge |
| Lateral velocity | −1–1 | How fast enemy moves sideways relative to us |
| Advancing velocity | −1–1 | How fast enemy approaches or retreats |
| Time since dir change | 0–1 | Longer runs predict continued movement |
| Wall proximity | 0–1 | Wall-adjacent enemies have limited escape |
| Acceleration | −1–1 | Speeding up vs slowing down changes dodge trajectory |
Movement: Tight Orbit + Bullet Dodge
WartHogNG orbits the target tangentially at 120–170px — close enough for high-damage shots, far enough to have dodge time. When the orbit drifts outside the band, the heading angles inward or outward to correct.
Three movement tricks layer on top:
- Bullet dodge: When enemy energy drops (indicating a shot), instantly reverse direction. Simple but effective — a fired bullet was aimed at our predicted position, and reversing invalidates that prediction.
- Random reversal: Every 40 ticks, 30% chance of spontaneous direction change. Prevents enemies from building a statistical profile of our movement.
- Energy-aware firepower: Below 30 energy, scale down shot power proportionally. Survive longer, keep firing.
Stalemate Prevention
If WartHogNG detects it's stuck in a repetitive orbit (same grid cell visited 4+ times in 200 ticks with no radar contact), it abandons the orbit and heads toward the field center while spinning the radar to re-acquire the target.
Origin
The original WartHog was a 2001 Java bot using law-of-sines predictive targeting. WartHogNG keeps the orbit movement but replaces the targeting with a KNN adaptive gun. The simulation above is a JavaScript port with a full physics engine matching Robocode's rules.
The Opponents
15 competitive bots in the live simulation, plus 15 historical bots described below. Win rates from a 200-match tournament per opponent using pure KNN targeting (no mimicry).
Nemesis
- RaikoMX (5%) — 6D segmented GuessFactor gun with wave surfing via recursive lookahead. WartHogNG's hardest opponent by far — its wave surfing dodges our bullets after they're fired, making any targeting approach ineffective.
Hard (< 80%)
- Chalk (57%) — KD-tree GuessFactor targeting + wave surfing with a flattener that activates on high hit rates.
- Oracle (61%) — Branching prediction tree simulates probabilistic enemy futures and collapses them into a centroid aim point.
- PlayItForward (63%) — Pattern matching: records exact movement sequences, finds the longest match in history, replays it forward.
- HawkOnFire (74%) — Minimum risk movement evaluates candidate positions for safety. Simple head-on gun, but our KNN learns its predictable patterns.
- Firestarter (75%) — Dynamic clustering GuessFactor gun with KD-tree, virtual gun system, and wave surfing with bullet shadow tracking. Ranked #6 in the RoboRumble.
Competitive (80–92%)
- DrussGT (80%) — The undisputed all-time #1 since 2008. KNN dynamic clustering with KD-tree + GoTo wave surfing evaluating 12 escape positions. The final boss of Robocode.
- CrazyBot (81%) — Random, chaotic movement that changes every 10–30 ticks. Hard to predict because there's nothing to predict.
- Mirror (82%) — Mirrors the opponent's angular movement to neutralize orbital targeting data. Segmented GuessFactor gun.
- BeepBoop (88%) — Ranked #1 in the RoboRumble. KNN dynamic clustering with KDE targeting + GoTo surfing with 5 danger estimators.
- Tron (89%) — Cardinal-direction movement with sharp 90° turns. Dynamic clustering gun. #1 in MeleeRumble (2003).
- CassiusClay (90%) — Pioneered wave surfing (“Butterfly”) and multi-segmented GuessFactor targeting (“Bee”). By PEZ.
- Wolverine (92%) — Circular targeting: observes turn rate and projects the enemy's path as an arc with tick-by-tick simulation and wall prediction.
Dominated (93–100%)
- Displacement (94%) — Records displacement vectors segmented by tactical situation. Predicts by adding the best-matching displacement to current position.
- ScalarR (95%) — Ranked #2 in the RoboRumble. KNN GuessFactor gun with weighted Euclidean distance and true GoTo wave surfing.
Historical Interest
These bots pioneered important Robocode techniques but WartHogNG beats them consistently (93–100%). They're not in the live simulation but their strategies shaped competitive Robocode.
- AntiSurfer (93%) — Anti-surfer targeting: predicts where wave surfers will dodge to and aims there instead.
- Evolved (95%) — All parameters derived through genetic algorithm optimization over thousands of simulated battles.
- Phantom (95%) — Wave surfing with adaptive profile flattening.
- Saguaro (96%) — Ranked #5 in the RoboRumble. KNN k=60 with Gaussian KDE + precise wave surfing.
- Komarious (97%) — MiniBot by Voidious: wave surfing + 6D segmented GuessFactor gun in ~1,500 bytes.
- PolyPredictor (97%) — Fits quadratic polynomials to recent x(t) and y(t) via least-squares regression and extrapolates.
- Diamond (98%) — Ranked #4 in the RoboRumble. Virtual guns with KNN k=15 KD-tree + wave surfing with branching prediction.
- BasicSurfer (100%) — Wave surfing with linear targeting. The textbook introduction to bullet dodging.
- Dookious (100%) — #1 in the RoboRumble for two years (2006–2008). Dual virtual guns + full wave surfing. By Voidious.
- MogBot (100%) — Pattern matching: records movement as tokens, finds longest matching subsequence, replays it forward.
- SandboxDT (100%) — Invented GuessFactor targeting (~2002). Flat movement resists profiling. Unrivaled #1 for two years.
- TheArtOfWar (100%) — Anti-gravity movement with virtual gun selection (head-on, linear, circular).