⚡ Part 5 · Advanced Module

Adding AI to Your
MUGEN Characters

Make your characters fight on their own from simple button-mashing to smart attack patterns.

⏱ ~60 minutes 📋 6 steps 🎯 Intermediate 🛠 Fighter Factory 3 + Notepad

How MUGEN AI Works

MUGEN AI lives inside your character's .CMD file. It tells the game which moves to use, and when. You're writing a list of decisions the smarter the list, the smarter the fighter.

AI Module — Step 1 of 6
What is MUGEN AI?

When a player is not controlling a character, MUGEN takes over and runs it as a CPU opponent. The rules for what that CPU does are called the AI Artificial Intelligence.

Without AI code, a CPU character will literally do nothing. It just stands there. Your job is to write simple rules that tell it when to attack, jump, block, or throw a special move.

All the AI code lives in one place your character's .CMD file. It's a plain text file you can open in Notepad or Fighter Factory 3.

FileWhat it controls
.CMDInputs, commands, and AI logic
.CNSStates, physics, damage values
.SFFSprites (images)
.AIRAnimations
.SNDSounds
How a CPU round works: MUGEN reads your AI rules top to bottom every single game tick (there are 60 ticks per second). The first rule that passes gets activated then MUGEN stops checking and acts on it.
🔍 Before You Start

Open your character's folder and find the .CMD file. Open it in Notepad. Scroll all the way to the bottom that's where we'll add all our AI code.

AI Module Step 2 of 6
The [AILevel] Header

Before you write any moves, you need to tell MUGEN that everything below is AI code. You do this with a special header block.

Scroll to the very bottom of your .CMD file and add this:

; ══════════════════════════ ; AI SECTION add at the bottom of your .CMD file ; ══════════════════════════ [AILevel] value = 1 ; Minimum AI difficulty level to use these rules

Everything you write after this block is AI code. Everything before it is normal player input commands they stay exactly as they are.

Important: The [AILevel] header tells MUGEN "only use these rules when the character is CPU-controlled." Without it, your AI triggers might fire during player-controlled rounds too!

Next, you'll write a Statedef block. Every set of AI actions lives inside a Statedef. Start with state -3 this is a special "always active" state that runs no matter what the character is doing:

[Statedef -3] ; -3 = runs in ALL states, including during attacks
Tip: State -3 is your best friend for AI. It keeps running even when the character is mid-attack or knocked down. Always use -3 for your AI block.
✏️ Try It

Add the [AILevel] header and the [Statedef -3] block to the bottom of your .CMD file. Save it. This is the foundation everything else builds on.

AI Module Step 3 of 6
Writing AI Triggers

Each AI action is a State Controller that uses a ChangeState type. You tell MUGEN: "If these conditions are true, switch to this state (do this move)."

Here's the basic template for one AI action:

[State -3, MyAIMove] type = ChangeState value = 200 ; the state number of the move to perform trigger1 = AILevel > 0 ; only run as CPU trigger1 = Random < 200 ; 200/1000 chance = 20% trigger1 = ctrl ; character must be free to act

Let's break down what each line means:

LineWhat it means
value = 200Go to state 200 that's your punch, kick, fireball, etc.
AILevel > 0Only trigger as CPU never for the human player
Random < 200Random number 0–999. Less than 200 = 20% chance per tick
ctrlShort for "control = 1" character isn't in the middle of another move

All triggers on the same number (trigger1) must ALL be true for the action to fire. It's like saying "AND" between each condition.

; A simple jab fires often when close [State -3, AI_Jab] type = ChangeState value = 200 trigger1 = AILevel > 0 trigger1 = ctrl trigger1 = p2bodydist X < 80 ; within 80 pixels horizontally trigger1 = Random < 300
✏️ Try It

Add one AI action to your character a basic punch or kick. Find the state number of that move in Fighter Factory 3, then write a [State -3] block for it with a Random chance between 100 and 400.

AI Module Step 4 of 6
Attack Priorities & Special Moves

A good AI mixes different attacks normals when close, special moves at range, supers when conditions are right. The key is order put the most important rules first, because MUGEN stops reading as soon as one fires.

Here's a layered attack setup with three tiers:

; TIER 1: SUPER (rare, only when life is low or has enough power) [State -3, AI_Super] type = ChangeState value = 3000 ; your super move state trigger1 = AILevel > 0 trigger1 = ctrl trigger1 = power >= 3000 ; needs full power bar trigger1 = life < 200 ; only when health is low trigger1 = p2bodydist X < 120 trigger1 = Random < 500 ; ── TIER 2: SPECIAL (medium range, medium chance) [State -3, AI_Fireball] type = ChangeState value = 1000 ; fireball state trigger1 = AILevel > 0 trigger1 = ctrl trigger1 = p2bodydist X > 80 ; far away = use projectile trigger1 = Random < 150 ; ── TIER 3: NORMAL (close range, high chance) [State -3, AI_Jab] type = ChangeState value = 200 trigger1 = AILevel > 0 trigger1 = ctrl trigger1 = p2bodydist X < 80 trigger1 = Random < 300
TriggerWhat it checksExample
p2bodydist XHorizontal distance to opponent< 80 = within punching range
p2bodydist YVertical distance to opponent< -40 = opponent is above you
lifeYour current life points< 200 = dangerously low
p2lifeOpponent's current life< 100 = they're nearly dead
powerYour power/super meter>= 1000 = one bar full
p2statetypeOpponent's stance= A = they're airborne
Rule of thumb: Start supers high, specials middle, normals last. If supers are at the bottom, they'll rarely fire because a normal move will trigger first.
✏️ Try It

Add a special move AI trigger above your basic punch. Give it a distance check so it only fires when the opponent is further away. Test in MUGEN does the AI start mixing moves?

AI Module — Step 5 of 6
Defensive AI Jumping & Blocking

A character that only attacks is easy to beat. Real AI also knows when to jump away, back off, or activate blocking. Let's add some defensive behaviour.

Making the AI jump:

; Jump backward when health is very low [State -3, AI_JumpBack] type = ChangeState value = 40 ; state 40 = jump trigger1 = AILevel > 0 trigger1 = ctrl trigger1 = life < 150 trigger1 = p2bodydist X < 60 ; opponent very close trigger1 = Random < 80 ; Walk forward when far from opponent [State -3, AI_WalkIn] type = ChangeState value = 20 ; state 20 = walk forward trigger1 = AILevel > 0 trigger1 = ctrl trigger1 = p2bodydist X > 150 ; opponent far away trigger1 = Random < 250

Making the AI block:

Blocking in MUGEN is handled by holding the back direction while standing or crouching. You can trigger this with a VarSet controller that holds a command. A cleaner approach used by most characters is the Guard controller:

; Stand block when opponent attacks and is close [State -3, AI_Block] type = ChangeState value = 130 ; state 130 = standing guard trigger1 = AILevel > 0 trigger1 = ctrl trigger1 = p2stateno = 200 ; opponent threw a punch (state 200) trigger2 = p2stateno = 210 ; or a kick (state 210) trigger2 = OR trigger1 = p2bodydist X < 100 trigger1 = Random < 400
Trigger1 vs Trigger2: All conditions on the same trigger number must ALL be true (AND). Different trigger numbers are checked separately (OR). So trigger1 = A + trigger1 = B means "A AND B". But trigger2 = C adds another path "OR C" that also activates if true.
✏️ Try It

Add a walk-forward rule and a jump rule to your AI. Watch your character in CPU mode does it move around more naturally now? Try lowering and raising the Random values to see how it changes behaviour.

AI Module Step 6 of 6
Testing & Tuning Your AI

Now that you have AI code written, it's time to load it into MUGEN and see it in action. Here's the best workflow:

StepWhat to do
1. Save your .CMDMake sure you've saved the file in Notepad or FF3.
2. Launch MUGENOpen MUGEN normally it reloads character files each session.
3. Watch ModeIn MUGEN, go to Watch or set both P1 and P2 to CPU. This lets you observe the AI without playing.
4. Debug ModePress Ctrl+D in a MUGEN match to see real-time debug info state numbers, variables, and triggers firing.
5. Adjust Random valuesIf the AI attacks too much, lower the Random cap. Too passive? Raise it.

Common problems and fixes:

; PROBLEM: AI won't do anything ; FIX: Make sure [AILevel] header is there and Statedef is -3 ; Also check ctrl = 1 character might be stuck in an animation ; PROBLEM: AI does the same move over and over ; FIX: Lower the Random cap on that move, or add distance conditions ; PROBLEM: AI fires moves during player-controlled rounds ; FIX: Make sure every trigger has: trigger1 = AILevel > 0 ; PROBLEM: AI never uses super ; FIX: Check the power condition try printing power in debug ; Also make sure the super block is BEFORE normal attack blocks
Quick tuning guide: Start with every move at Random < 200. After testing, raise the values for moves you want more often, and lower them for moves that feel spammy. A good AI feels unpredictable but fair.
AILevel scaling: You can make AI harder on higher difficulty by using AILevel in your Random calculation:
trigger1 = Random < (AILevel * 50)
At difficulty 1 this is 50, at difficulty 8 it's 400 the AI attacks much more aggressively on harder settings.
; FULL EXAMPLE a simple but functional AI block [AILevel] value = 1 [Statedef -3] ; Walk forward when far [State -3, AI_Walk] type = ChangeState value = 20 trigger1 = AILevel > 0 trigger1 = ctrl trigger1 = p2bodydist X > 120 trigger1 = Random < (AILevel * 40) ; Fireball at range [State -3, AI_Special] type = ChangeState value = 1000 trigger1 = AILevel > 0 trigger1 = ctrl trigger1 = p2bodydist X > 80 trigger1 = Random < (AILevel * 30) ; Jab when close [State -3, AI_Jab] type = ChangeState value = 200 trigger1 = AILevel > 0 trigger1 = ctrl trigger1 = p2bodydist X < 80 trigger1 = Random < (AILevel * 50) ; Jump escape when low health [State -3, AI_Jump] type = ChangeState value = 40 trigger1 = AILevel > 0 trigger1 = ctrl trigger1 = life < 200 trigger1 = p2bodydist X < 60 trigger1 = Random < 60
🏆 Final Challenge

Set your character to CPU vs CPU in MUGEN Watch mode. Can you tune your AI to win more than half its matches against a default character? Adjust the Random values until you're happy with how it plays!

Grab the Resources

Everything you need to follow along with this workshop.

📄
AI Cheat Sheet
Quick reference for the most common AI triggers distances, life checks, power, and state types. Print and keep it next to you while coding.
🤖
Starter AI Template
A ready-to-paste AI block with walk, jab, special move, and jump escape all with EDIT ME comments. Drop it into any .CMD file.
🎯
Project Brief
A structured task sheet to guide students through adding and tuning AI to their character with achievement targets at each stage.
🔙
Back to Workshop
Return to the main MUGEN Workshop page to explore character creation, Fighter Factory 3, stage building, and more.
Go to Workshop