TeamFightTactics - Coding an AI - Part 1
Description
This part 1 of my series on coding an AI to play TFT. This gives an overview of my problem-solving process. Topics covered: Choosing a project, Risk Management, Software Architecture & Planning, Proof-Of-Concept or Minimum-Viable-Product, & Incremental Improvement.
Learn the basics of how an AI works, including techniques like Neural Networks, Fuzzy Logic, Heuristics, & Genetic Algorithms.
Technologies discussed: NodeJS, Typescript, MemoryJS, GraphicsMagick, TesseractOCR, TensorFlow, SqlLite, & RobotJS.
TFT by Riot Games is a great project due to its strategy elements & simple user-interface.
I might do a series in the future on other games, such as: Hearthstone, Clash Royale, Polytopia, Chess, & Clash of Clans. If you have a strategy game suggestion, request it in the comments.
Transcript
Hey it’s AI Gamer here where I teach you to code by building AIs to play video games.This is my series on TeamFight Tactics, which is an auto-battler built by Riot Games for mobile & PC. Although it has some randomness, it also has a lot of strategy, which makes it a good fit for an AI. This video will give a general overview of my goals for this project and shows step-by-step how I’m building it.
Here’s my process:
Brainstorm Projects
There are a ton of awesome games, but I have to focus on just one.
Risk Management
I need to plan ahead to avoid potential road blocks
Define Architecture
There are too many possible technologies, I need to narrow it down
Proof-Of-Concept
Before I code advanced features, I need to get the basics working
Incremental Improvement
Rome wasn’t built in a day & my AI won’t be either
My first step was brainstorming what type of project to build. Since I’m a coder and a gamer, building a gaming AI sounded like a fun project. Most of my professional coding experience is with business applications, so this will be a nice change of pace. My main criteria for picking a game was something with strategy & something I’m passionate about. Although League of Legends is my favorite game, it’s team-based & very mechanical, both of which makes it extremely challenging for an AI to handle. I’d love to try it some day, but I want to start with an easier project first. I ranked plat in TFT last year, so I already know how to play it & I’d love to see if I can rank higher, so it seems like the perfect fit..
Now, just to clarify, I’m doing this project for fun and I have no intention of ruining the gamer community. I won’t release my bot for others to use & I won’t ruin people’s games. Since TFT is a last-man-standing game, I won’t be hurting any teammates if my bot plays poorly. I’m also not trying to create any exploits, like infinite lives, I want the AI to replicate human behavior as closely as possible.
My next step was risk management. I don’t want to start a project that I’m not confident I can accomplish. I also don’t have a budget, so I need to verify that there are plenty of open-source tools and libraries I can leverage. It needs to be a project I’m passionate about so I don’t lose interest & also small enough that I can accomplish it on my own without help & without getting stuck or burned out. I think a TFT bot matches all these criteria.
Determining scope is very important. For version 1, my goals are:
Automate UI
I want it to be able to play the entire game without any human interaction
Code basic strategy
I want to verify that it’s making semi-intelligent decisions, not just completely random choices
Win a single unranked 1st place game
This is my initial criteria to say that my basic AI is working
This probably sounds too simple, but it’s better to start with a basic proof-of-concept and make improvements over time. It’s fun to daydream about a perfect product with every possible feature. However, if my initial project is too difficult it’ll never get finished. In the real world, at some point a project has to be labeled “completed” so it can be shipped to customers, despite its flaws. There’s always room for improvements in version 2.0.
Before I can start coding, I need to architect the software. It’s too hard to visualize the project as a whole, so I need to break it down into smaller tasks. Then I can document the high-level requirements of each one and evaluate any potential risks that would prevent me from completing them.
For this project, I think the main problems to solve are:
Gather Data
The AI doesn’t have eyes, it needs a way to understand what’s happening during the game
Train
AIs aren’t magic, they need to learn (or be taught) the strategy in order to play the game
Act
The bot needs to simulate user input as if it were a human
These are still too big to actually start coding, so later on I’ll break them down into even smaller pieces.
Gathering in-game data
The concept of “Gathering in-game data” sounds relatively simple. At the most basic level, the AI needs to know what cards I have in my hand & which units are on the board, so it can make decisions. This is actually a bit trickier than it sounds. The easiest way would be to read the internal memory of the program while it’s running. The risk here is that I know Riot has coded anti-cheat mechanisms to try to prevent this. Because TFT has a PC, Mac, & Mobile version, I think I can get it to work on atleast one of these platforms. If I choose to attempt this method, I should first test if it works before I get too far down that rabbit-hole. One way is using an app called CheatEngine. It can also change memory, for hacking things like “infinite lives”, but I will only be reading memory. Even if I wanted to cheat, exploits that modify process memory usually don’t work in online games since the majority of the logic is actually run on the server, not on my local PC.
If CheatEngine fails, my homegrown code will also, so I should test that first before I waste time coding it. It may also be possible to bypass these protections by running the app through an emulator or virtual machine to circumvent these protections.
Another option is to use computer vision techniques. These can analyze the graphics being displayed on the screen. For simple graphics like icons and text, this is pretty easy, but for 3D models with animations, this is actually very difficult. For some games, like first-person shooters, the 3D graphics & animations give crucial info to the user about where to move and how to aim. Since TFT is basically a card game, I think the simple text & icons will give me most of the information I need, I can probably ignore the fancy graphics as pure eye-candy.
Once I’m ready to code this task, I’ll determine which approach to use. For now, it’s enough to simply recognize that I have multiple options so I’m not concerned about failing.
AI for strategy & decision making
The heart of this coding project will be building the AI. Strategy games intentionally give you multiple compelling choices so you have to make a gut decision. Unfortunately, computers were designed as calculators which follow precise instructions - they can’t naturally judge a decision by weighing pros and cons or learn from their experiences or mistakes. This makes beating a strategy game difficult with traditional coding methods. Luckily, multiple types of AI techniques have been invented which attempt to make computers more flexible in their decision making.
For example: Neural Networks, Fuzzy Logic, Heuristics, and Genetic Algorithms. These strategies are each very different & have their own unique strengths/weaknesses, so I’ll need to be smart about how I utilize them.
Neural Networks & Genetic Algorithms are self-learning. This would be ideal for TFT, but the difficulty is that they require a large amount of training data. Depending on the complexity of the problem to solve, it may require thousands or millions of examples. The issue is that TFT does not have a very good match history, it only shows the player’s final units, not their actions chosen each round. It also doesn’t show the units positioning on the grid. TFT also doesn’t support spectating other people’s games, so gathering training data will be difficult.
Fuzzy logic allows the computer to make these types of decisions. The problem is Fuzzy logic requires hard-coded rules, so they don’t allow the AI to learn new strategies. This can work for simple strategies, but not very complex ones.
I think the lack of training data will be a significant risk to this task so I may need to find workarounds for that. However, I think the AI will still work fine, it just might not learn to be as “smart” as I’d prefer.
Simulating User Input
I think simulating mouse and keyboard input to remote-control the app should be fairly easy. The only complexity is that each operating system and device are slightly different. Ideally, I should decide which platform I’m gaming on as early as possible so I can avoid re-writing any code. However, if I use an open-source library that is cross-platform, it should alleviate some of that risk.
Another risk is that Riot Games has put significant effort into prevent cheating, I want to code my bot in a way that’s not easily detectable to prevent my account from getting banned. For that reason, I may need to act human-like by moving and clicking the mouse slowly and not sending keyboard shortcuts too quickly.
Architecture
Now that I’ve documented the high-level task requirements, I need to make a simple blueprint to architect the project.
For now, I’m planning to code this against the Windows PC version of the game, but I can fall back to the mobile version of the game (or an emulator) if needed. I’m going to code it on the NodeJS platform using TypeScript as my programming language. This allows me to leverage a lot of open-source APIs available on the NPM website. Most of these libraries are cross-platform, so hopefully if I need to switch to a different operating system some of the code will still work.
I found these open-source libraries to help me code this project:
MemoryJS
Which extracts programming variables out of the game while running
GraphicsMagick
Which has various image processing functions, for example to search for the location of a specific champion on the screen
TesseractOCR
Once letters or numbers are drawn on the screen, they’re converted to a picture. Tesseract analyzes the pixels in an image to extract them back out.
TensorFlow
This allows us to set up a Neural Network really easily
SQLite
This is a database, we’ll use it to store training data
RobotJS
This will help us simulate user input when the AI needs to perform an action
We’ll use all of these pieces together to build this project.
If I were coding this for a business with a team of engineers, we’d break each piece down into smaller chunks and estimate the amount of work required to complete each task. A project manager would schedule the tasks on a calendar, prioritized by bottlenecks & high-risks first. That would allow business owners to see a high-level overview of our progress. Each coder would be assigned a different task, but we’d collaborate to make sure the pieces fit together. Since I’m just coding this by myself for fun, I’m going to skip this step.
That’s a general overview of the project I’m attempting to build and my overall design process. In future videos I’ll go through step-by-step how I’m coding each piece so we can learn together. Let me know in the comments below if you have suggestions on how I can improve my design as well as your prediction on what competitive rank you think my bot can achieve.