🏎️ When Bots Play Better: The One and Only Bot for KartWars.io
Adrian Paul Nutiu
Posted on November 27, 2024
🚀 Intro
It was that day which found me wandering through multiplayer online browser games, just to stumble upon KartWars.io.
For those of you who don’t know, KartWars.io was a fast-paced multiplayer game where players controlled karts in an arena. The goal was simple: collect power-ups, outmaneuver your opponents, and survive for as long as possible while climbing the leaderboard.
I quickly got hooked, but as I played, an intriguing question popped into my mind:
What if I could create a bot to play this game for me?
Not because I didn’t enjoy the gameplay, but because the challenge of building something capable of navigating this chaotic digital battlefield seemed too fun to resist.
⚠️ Photosensitivity Warning
The following video involves fast-changing visuals that may trigger discomfort. Please proceed with caution.
The first step in creating my bot was understanding how KartWars.io worked under the hood. The game communicated with the server using WebSockets - a technology for real-time data exchange.
Using the browser's developer tools, I tapped into and monitored WebSocket traffic and identified key messages that revealed the kart's position, velocity, nearby power-ups, and other players' movements. It was like peeking behind the curtain of the game mechanics - a fascinating insight into how the arena operated.
🏎️ Teaching the Bot to Drive
Armed with this information, I wrote a basic JavaScript to control the kart’s movement. Initially, it was clumsy - the kart zigzagged, fell out of the arena, and failed spectacularly. 🥲
I called that "AI", but we all know what it actually means. 🤣
As I refined the logic, the bot grew smarter. It prioritized moving toward power-ups and clusters of food, avoided enemies, and steered away from collisions just in time. Watching it make strategic moves was incredibly rewarding.
🎮 In-Game Power-Ups
Power-ups were the heart of KartWars.io, making the gameplay unpredictable and thrilling.
Programming the bot to use these effectively added layers of strategy to the project.
🛠️ Debugging with Visual Cues
To better understand the bot’s decisions, I added visual overlays directly into the game. These overlays included lines for the bot’s current path, enemy trajectories, and avoidance zones. This real-time feedback made debugging much easier and helped fine-tune the bot’s logic.
🎛️ Adding a Control Panel
To dynamically adjust the bot’s behavior, I embedded a control panel using dat.GUI. This lightweight library allowed me to modify parameters like speed, aggression, and strategy thresholds on the fly. It also enabled toggling strategies without editing the code, speeding up experimentation.
🧠 Used Techniques
Creating a bot capable of surviving and thriving in KartWars.io required several core techniques:
🛡️ Enemy Surrounding Detection
The bot analyzed enemy positions and trajectories to detect when it was surrounded.
It calculated escape routes and accelerated toward safety, steering clear of threats.
🛡️ Weapon-Based Enemy Avoidance
By identifying enemies equipped with specific power-ups, the bot created avoidance zones (polygons) around them. It avoided these zones unless it had a shield or a strong counterattack strategy.
🎯 Enemy Trajectory Prediction
The bot predicted enemy movements to dodge attacks or intercept projectiles.
For offense, it calculated precise interception paths to maximize missile hits.
⚔️ Dodge, Defend, Attack
The bot’s ability to dodge, defend, and attack made it a formidable player. It activated shields under attack, fired missiles at vulnerable opponents, and evaded dangerous zones. With trajectory prediction and strategic power-up usage, it was able to outmaneuver human players frequently.
🤖 The Joy of Watching It Work
The most satisfying moment came when the bot began outperforming me. Watching it survive longer, climb the leaderboard, and execute strategies in real time was exhilarating. While it occasionally exhibited “bot-like” quirks, its ability to compete with real players made the effort worthwhile.
📚 Lessons Learned
Building this bot wasn’t just fun - it was an invaluable learning experience. From reverse-engineering WebSocket communications to implementing real-time decision-making, I gained skills in problem-solving, debugging, and AI logic.
Though KartWars.io is no longer available, the lessons I learned continue to shape my projects. Whether it’s a game or a complex system at work, the thrill of breaking down problems and building innovative solutions remains unmatched.
If you’ve ever tinkered with bots or automated something just for the fun of it, share your story—I’d love to hear about your experiences!