• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Fighting Game AI

D

discocar

Guest
I wanted to make a fighting game but between AI. It seems pretty hard haha.

I was wondering if there is any straight forward way with states.

Just make the enemies punch each other and move back and forth.

I've tried following some tutorials but there is soo much coding haha. I just get lost in it.

I'm not used to doing AI like this. Would be a start if I can have AI see each other, walk up and punch each other. While moving around a bit. Then I can probably build from that. Any help would be great or any really easy to follow tutorials.

All fighting tutorials I found are very complex with too much going on jump, back flip attacks, firing lasers while doing back flips and they just keep going! I Just want one dude to punch another dude. haha
 

Relic

Member
Make use of states as you suggested:
State idle for standing, watching for enemies
State move for moving toward the enemy
State attack for throwing the punch
State retreat for backing away from the enemy

Coming up with states is the easy part though- Does the AI need to act like a human player? Deciding on the triggers to change state to act like a human player is more challenging than having a highly predictable enemy AI that walks up and punches a dude
 
B

basement ape

Guest
I have a state-based system going that works fairly well. I can't login to GMS right now to check but the simplified version works something like this:

AI:
Check distance to enemy(player).
Decide on state based on distance. Set a timer for staying in that state.
if the timer runs out -> Check distance again and set new state.
If the enemy(player) does something that requires a reaction -> check distance again and set new state.

The decision stage also takes into account what the player/enemy is doing; is he in the air? Is he moving towards the AI (= probably wants to attack)?
You'll want checks on player/enemy behavior while in a state so that the AI can break out of the state prematurely to adapt to the new circumstances. To make the AI more life-like and fair you can have delay timers so that the AI doesn't immediately react to what players are doing. It's also a good idea to add a bit of randomness for the timer used to stay in any specific state so the AI isn't too predictable.
 
D

discocar

Guest
Yeah, I would need them to be more human, instead of just walking up and punching, but I think two AI which act more human would be pretty hard, I was thinking maybe try turn based and making it speed up or miss attacks. will give it the illusion of AI fighting in real time.
 

Smiechu

Member
Haha, it's not possible to make even simplest AI without reasonable amount of coding haha. Stop being lazy, haha.
 
B

basement ape

Guest
Try and structure your AI something like this...

Make some states using constants i.e:
Code:
AI_IDLE = 0;
AI_ENGAGE = 1;
AI_RETREAT = 2;
etc.

Declare some variables for your AI to use:
Code:
AI_State = 0;
distanceEnemy = 0;
AI_State_Timer = 0;

Check distance to enemy:
Code:
with(enemy) { with(other)          // Assuming you've got a pointer "enemy" sorted out already
{
      distanceEnemy = other.x - x;
} }

if AI_State == AI_IDLE and (distanceEnemy > 300 or distanceEnemy < -300) and AI_State_Timer <= 0
{
      AI_State = AI_ENGAGE;
      AI_State_Timer = 3.0;          // give the AI 3 seconds in the new state
}

Carry out actions for this state and determine if we need to break out of it:
Code:
if AI_State == AI_ENGAGE
{
      if AI_State_Timer > 0
      {
               if distanceEnemy <= 40 and distanceEnemy >= -40        // close enough to throw a punch
               {
                       AI_Press_Punch = true;
                       AI_State = AI_IDLE;        // reset state after attack input
                       AI_State_Timer = 0.5;
               }

               if enemy.Action == actionAttack and distanceEnemy <= 60 and distanceEnemy >= -60
               {
                      AI_State = AI_RETREAT;   // break off into defensive state
                      AI_State_Timer = 1.5;     // stay in that state for 1.5 seconds.
               }
      }
      else
      {
             AI_State = AI_IDLE;
             AI_State_Timer = 0.5;         // give the AI half a second to decide on its next move
      }
}

This is just to show how it works. You'd need to expand on it a whole lot for it to be any fun obviously.
 
Top