Jump to content


Photo

Another Ai Topic: Finite State Machines


  • Please log in to reply
46 replies to this topic

#21 ~Dannyboy~

~Dannyboy~

    ~hoqhuue(|~

  • GMC Member
  • 2144 posts
  • Version:GM8

Posted 10 January 2009 - 11:39 AM

I've had another brainwave! Expanding on the one script per state idea... Why not use a switch statement, but for the events instead of the states. eg.
switch (argument0) {    case ev_step_normal:        // Called in step event        break;    case ev_enter:        state_index = zombie_roaming // Change to script index        // Called when state is entered        break;    case ev_exit:        // Called when state is exited        break;    case ev_create:        // Initalises any variables for the state        break;    case ev_destroy:        // Cleans up any memory used by the state        break;    default:        // Event not used for this state        break;}
This script now becomes a new resource called... a state! This way states can have as many events as they need and they are kept nicely together in one package. They also keep the advantages of having enter and exit events. Custom events are as easy as adding a constant in global game settings.

To use the states call in the create event something like this.
// Create states
script_execute(zombie_roaming, ev_create);
script_execute(zombie_mauling, ev_create);

// Enter roaming state
script_execute(zombie_roaming, ev_enter);
In the step event call
script_execute(state_index, ev_step_normal);
And the destroy event call
// Exit current state
script_execute(state_index, ev_exit);

// Destroy states
script_execute(zombie_roaming, ev_destroy);
script_execute(zombie_mauling, ev_destroy);
Etc.

For convenience, another script (state_change) can be easily created
// state_change(state)
script_execute(state_index, ev_exit);
script_execute(argument0, ev_enter);
So what do you have to say about this method? I personally think it looks really good, I'm going to try and make an AI using it. I'll post it here if it turns out OK.

Edited by ~Dannyboy~, 10 January 2009 - 11:44 AM.

  • 0

#22 TheMyst

TheMyst

    GMC Member

  • New Member
  • 166 posts

Posted 11 January 2009 - 06:06 AM

The AI I'm working on right now for my game is an FSM using the "If-then" approach and I can say this, the whole thing has become one big cluster-****!I'm tempted to try out the modular approach that Dannyboy mentioned. The AI in my game is not as complex as some of the AI you guys are talking about, but it has become too complex to keep contained in one object. The only trouble I can see myself having is transfering...

Ah crap, it won't work for me. My game requires multiple instances of the same object and has to loop through those instances to get a value that will be used later... hmm... That modular method will only work smoothly if you're going to have a single instance of the one "object".

Going to dannyboy's idea of using scripts that seems like the best idea so far. Might as well give it a shot.

Edited by TheMyst, 11 January 2009 - 06:49 AM.

  • 0

#23 ~Dannyboy~

~Dannyboy~

    ~hoqhuue(|~

  • GMC Member
  • 2144 posts
  • Version:GM8

Posted 11 January 2009 - 09:30 AM

While prying deeper into the wonderful world of zombieJim, I have found that at times it would be really useful to have several layers of states running at the same time. The closest representation I can think of is multi-threading. For example, while zombieJim is in the "wander around" state it would be useful to start a movement state as another layer (ie. in another state thread). These movement states would be basic things such as "turn left" or "move forward". Rather than having a transition from the "wander around" state to "turn left" than having to backtrack to "wander around" once we've finished turning, I believe they should be running together. The currently running "state threads" could be organised in a ds_list or similar so that all relevant events can be called for all threads. Does that make any sense to anyone else? Or am I completely overcomplicating the idea?
  • 0

#24 TheMyst

TheMyst

    GMC Member

  • New Member
  • 166 posts

Posted 11 January 2009 - 06:47 PM

Seems like you are overcomplicating it a tad bit there. Threads would only be useful if you were making AI that learns from user input in my opinion, like a state thread that tracks the players actions while it is attacking... if that makes any sense.
  • 0

#25 gandalf20000

gandalf20000

    GMC Member

  • New Member
  • 478 posts

Posted 12 January 2009 - 10:49 PM

I just wanted to chime in that I think imagining FSMs down to a sense of "personality" or "mood" in a game is giving the AI waaaayyy too much detail. Honestly, what personality does an enemy in a game have? If you are making a dating sim, then sure... personality is something you may want to consider. But the personality of most AIs in games is "aggressive murderer." Thus, you really only need states of awareness (idle, alert, etc.) Putting things in like mood and feelings just muddies up the AI. The AI doesn't need to be angst-ridden. It just needs to be fun and dynamic. In fact, the less attribute checks you have to make concerning the mood of the AI, the more checks you can make concerning stimulus and behavioral processes.

Not in the mood? Moods are a thing for cattle and loveplay. Not fighting!
-Gurney Halleck, Dune


I see your point, but personalities would be useful. It would affect the way an enemy would attack you. If they're distracted from being bored, happy, etc., they might not see you coming. If they have a very cautious personality, they might see you coming, and take a sniper rifle and blast your head off. If they're very prideful and flashy, they might try and go for a more theatrical battle and defeat you that way. If they're a coward, they'll run away.

Using the Halo comparison:
If you notice in Halo, each group of aliens has a different personality.
Standard grunts are cowards. They fire, run away when you shoot them or move towards them, and try to kill you if you can't see them.
The larger Covenant (the red and blue) will fire at you and fight relatively intelligently while they have a force shield. When that wears out, they charge towards you.
The yellow Covenant that usually carry energy blades will charge you and try to slash you.
The small Covenant with the shields will shoot at you from behind, stand their ground while they can, and retreat when you find a spot where you can get past the shield.
The Flood simply shoot at you, and when they run out of ammo, they swarm you.

Each group of enemy has a different personality. Granted, it's not as complex as the one that was suggested, but it's still like that. Slightly random personalities add for interesting battles without having to have hundreds of different enemies made. Because it's an enemy, you only need to cover a few emotions. You also need to cover some basic skills, which are a little more reasonable in battle (e.g., speed, intelligence, accuracy, sight range, etc.), so if you need to, for speed reasons, you can cut out the emotions and give each enemy the same emotions, just different skills.
  • 0

#26 TheMyst

TheMyst

    GMC Member

  • New Member
  • 166 posts

Posted 13 January 2009 - 12:37 AM

If I may add, the Grunts do gain a bit of courage when they are behind an bigger ally. Alot of how the AI acts depends on the circumstances. I remember watching a video prior to the release of Halo 3 about the "Brute Pack", and it shows in the gameplay. A leader will give an order and the lower ones will follow it. There were instances where I had 5 or 6 grenades coming at me when I played.

Edited by TheMyst, 13 January 2009 - 12:40 AM.

  • 0

#27 Sinaz

Sinaz

    MCP Killer

  • Retired Staff
  • 2751 posts
  • Version:GM8

Posted 13 January 2009 - 01:05 AM

Using the Halo comparison:
If you notice in Halo, each group of aliens has a different personality.
Standard grunts are cowards. They fire, run away when you shoot them or move towards them, and try to kill you if you can't see them.
The larger Covenant (the red and blue) will fire at you and fight relatively intelligently while they have a force shield. When that wears out, they charge towards you.
The yellow Covenant that usually carry energy blades will charge you and try to slash you.
The small Covenant with the shields will shoot at you from behind, stand their ground while they can, and retreat when you find a spot where you can get past the shield.
The Flood simply shoot at you, and when they run out of ammo, they swarm you.

Each group of enemy has a different personality. Granted, it's not as complex as the one that was suggested, but it's still like that. Slightly random personalities add for interesting battles without having to have hundreds of different enemies made. Because it's an enemy, you only need to cover a few emotions. You also need to cover some basic skills, which are a little more reasonable in battle (e.g., speed, intelligence, accuracy, sight range, etc.), so if you need to, for speed reasons, you can cut out the emotions and give each enemy the same emotions, just different skills.

I get what you mean. But don't forget that FSMs are behavioral, while personality quirks and traits are conveyed almost entirely through the implementation of art and animation.

I backpedal slightly in saying that "moods" can be a good thing when your game mechanics capitalize them. Metal Gear Solid series really flirts with this level of AI depth. AI can become distracted, bored, tired, lazy, etc. You just have to be prepared to devote the processing time to such complex systems. The Sims is a game where the FSM attributes constitute a "personality." You just have to ask yourself... how many personalities do you want in your game? How many can you afford to process?
  • 0

#28 TheMyst

TheMyst

    GMC Member

  • New Member
  • 166 posts

Posted 13 January 2009 - 12:39 PM

I get what you mean. But don't forget that FSMs are behavioral, while personality quirks and traits are conveyed almost entirely through the implementation of art and animation.


Not true mate, not true. If that was the case, then you could have a rocket launcher toting GTA hooker. As awesome as that would be, it would be a bit out of place. The animations and artwork work in conjunction with how the object as a whole thinks.
  • 0

#29 Yourself

Yourself

    The Ultimate Pronoun

  • Retired Staff
  • 7341 posts
  • Version:Unknown

Posted 13 January 2009 - 12:50 PM

I get what you mean. But don't forget that FSMs are behavioral, while personality quirks and traits are conveyed almost entirely through the implementation of art and animation.


Not true mate, not true. If that was the case, then you could have a rocket launcher toting GTA hooker. As awesome as that would be, it would be a bit out of place. The animations and artwork work in conjunction with how the object as a whole thinks.


What?
  • 0

#30 TheMyst

TheMyst

    GMC Member

  • New Member
  • 166 posts

Posted 13 January 2009 - 12:58 PM

I get what you mean. But don't forget that FSMs are behavioral, while personality quirks and traits are conveyed almost entirely through the implementation of art and animation.


Not true mate, not true. If that was the case, then you could have a rocket launcher toting GTA hooker. As awesome as that would be, it would be a bit out of place. The animations and artwork work in conjunction with how the object as a whole thinks.


What?


Yes... I went there...

In all seriousness, I meant that it is impossible to portray mood and emotion through animation alone. The object has to know what it is thinking or how it is thinking in order to portray the emotion in the first place.

Lets take the characters from Team Fortress 2 for example. Despite the fact that they are player controlled, each and every one of them has their own AI in the form of an FSM that processes the actions of the player and the events that occur in the game. If you go on a killing spree for example, the AI knows that you are kicking some serious ass and will play sounds and animations that are appropriate to the situation ("I'M DRUNK! YOU DON'T HAVE NO SKILLS!"). Conversely, if a bomb lands at your feet or explodes nearby, the AI will display a reaction("OH SHI-").

Now am I making sense? In order to impliment emotions in an FSM, you would have to set up variables that change the output of the different functions for the different states.
if state = scared{
	objSpeed = objSpeed + 20;
}

Edited by TheMyst, 13 January 2009 - 01:08 PM.

  • 0

#31 Yourself

Yourself

    The Ultimate Pronoun

  • Retired Staff
  • 7341 posts
  • Version:Unknown

Posted 13 January 2009 - 01:07 PM

No, that still doesn't make sense. It's not really AI. The personality of the characters is still conveyed entirely through their animation (and what they say). The fact that appropriate animations are selected based on the state of the world is largely irrelevant.
  • 0

#32 TheMyst

TheMyst

    GMC Member

  • New Member
  • 166 posts

Posted 13 January 2009 - 01:12 PM

No, that still doesn't make sense. It's not really AI. The personality of the characters is still conveyed entirely through their animation (and what they say). The fact that appropriate animations are selected based on the state of the world is largely irrelevant.


Cogito, ergo sum.

I think, therefore I am.


It still acts automatically after processing what is going on around it, therefore it is safe to say that it is a limited form of AI. All the animations do is make the emotions visible. If a character is upset, it has to know it is upset before it can display something like a scowl or a shaking fist.

It has to be pissed before it can shake a fist. (Sorry, thought of that while talking to a friend of mine and just had to put it down.)

Edited by TheMyst, 13 January 2009 - 01:54 PM.

  • 0

#33 gandalf20000

gandalf20000

    GMC Member

  • New Member
  • 478 posts

Posted 13 January 2009 - 08:55 PM

We're talking about AI, not animations. Animations are important, as they display the AI's reactions, but they are not required. Many of them are also superfluous. I mean, if you were in a firefight, surrounded by a bunch of enemies that would love nothing more than to blast your sorry head off with their shotgun, are you really going to tick them off and taunt them? You wouldn't want to do it after they're dead, because they won't see, as they are currently lying in a pile of their own stuff and spawning somewhere else.

The thing is, emotions allow for proper AI. Personalities help shape the emotions. Granted, you don't need full blown personalities, but just the parts that affect battle. For example, you don't need the sort of things in a personality that tell how a player reacts when he gets hungry. If you're being chased by a bunch of guys carrying machine guns, you're not going to be thinking about your stomach.

In a personality affecting emotions FSM type AI, you're just going to need these traits and emotions (for a basic AI):
Traits:
Speed
Intelligence
Temper
Distraction
Accuracy
Strength
Emotions:
Happy
Bored
Angry

Also, to make things simpler, an AI is only being affected by one emotion at a time, otherwise you risk two conflicting emotions affecting the AI or bogging down the system. You can't be angry and happy at the same time. Just like you can't be angry and bored, or happy and bored, as when you are either of those, it means you have something to do or think about, which is why you're happy or angry. When you are none of these emotions, you are considered content, and therefore, nothing is inhibiting or enhancing certain actions.
  • 0

#34 Pie Person!

Pie Person!

    GM 6+ Lover

  • GMC Member
  • 1973 posts

Posted 13 January 2009 - 10:00 PM

Here at bungie publications, there are lots of papers about Halo's AI. Finite State Machines and every thing. I think you guys should check them out.

They pretty much nail every thing that we are talking about in this topic.
  • 0

#35 T-Bird

T-Bird

    GMC Member

  • New Member
  • 1326 posts

Posted 14 January 2009 - 02:30 AM

Well, besides talking about halo, or restating the original story as a more entertaining novel version, I think I'll actually try to expand the conversation.

I think the biggest thing zombieJim was missing was a reward/punishment system.

Now it stands to reason that zombieJim is a hungry guy, what with that big hole in his stomach and all, and not a very inquisitive one - seeing as how that last shotgun shell took out his brain's left hemisphere - so he should be much more entertained by eating (slower decay). But he should also have a long-term memory, recalling to mind that littleGirlSuzie was a quicker catch than sprinterJose. He should learn that hunterBob did some serious damage whereas kittyCat did very little. He should learn that examining chair gave him no benefit whereas chasing human led to feasting.

Basically, every encounter should trigger some sort of reward/punishment. Every time zombieJim is shot, he is punished 3 points. Every time he trips he is punished 1. Every time he eats he is rewarded with 10 points over the amount of time it took to catch said food. If his random meanderings are followed by a reward, zombieJim is rewarded further with an extra point or two. If his meanderings are followed by punishment, he likewise receives further punishment. Storing the total number of points received for each item in a long term memory (perhaps also with some decay) will allow zombieJim to "learn".

So iteration one, zombieJim is placed in a room with a nearby hunter, a mid-range sprinter, and a distant old man he goes for the hunter the nearest catch. He is injured and now hunter has a punishment associated with him. Hunter will decay from memory slower, and have a lower priority.

Iteration two, zombieJim sees all three again, but hunter gives him bad vibes and has a much lower priority than last iteration so zombieJim determines sprinter is a better target. After chasing sprinter for some ways he looses sight of sprinter and is left considerably more tired and no less hungry. Sprinter also receives punishment points and has a lower interest to zombieJim (though since no actual harm came the punishment would be less than hunter, making sprinter more enticing).

Iteration three, zombieJim has ruled out both hunter and sprinter as viable food and begins to wander. As he wanders he comes across oldMan and the chase ensues. zombieJim catches oldMan very quickly and thus gets a large reward. Old man now has a much higher priority and decays from memory slower. When oldMan is encountered he zombieJim will target him more readily and take note of him longer. Also zombieJim learned something new today. Wandering can lead to potential discoveries. Since wandering was followed by a reward wandering receives reward points and will have a slightly higher priority. zombieJim will now be more willing to forgo the closest object and look around for something more enticing.

An alternate to iteration three could be that zombieJim decides to wander and finds himself in a trap. Since wandering is followed by punishment, it gets punishment points, leading to a very timid zombieJim that waits for food to come to him rather than finding it.
  • 0

#36 Potnop

Potnop

    GMC Member

  • GMC Member
  • 3101 posts

Posted 14 January 2009 - 04:45 AM

Yeah I don't think TF2 uses AI to determine what to say. I think it's more like determining how well the player is doing based on kills per minute and objectives completed and stuff.

As much as I love FSM's I would probably not use it for something like that. Heh the AI in my Atlantis Wars game is an FSM. Works pretty nicely. The AI is either Idle, Moving to Target, Running, In Combat, or In Pain.


If AI is idle it does nothing, unless it follows the player and is allied. It also reloads the weapon if needed. If enemy found it goes to move to target mode.

If it's moving to target it moves towards it until it has a shot at the enemy for 10 steps. It fires its weapon if enemy isn't blocked by walls. If it needs to reload or needs to heal it goes to run mode.

If it's attacking, it jumps and moves around randomly while firing its weapon. If the enemy is blocked by walls for 10 steps, it goes to move to target mode. If it needs to reload or needs to heal it goes to run mode.

If it's running, it moves in opposite direction of the enemy. Then once the enemy is blocked by a wall, the AI crouches. Once healed and done reloading it goes to move to target mode.

If it's in pain mode it stands still and goes to move to target mode once done with the pain animation.



If at any time the AI's target is killed, it goes to idle mode and finds another target. Very simple but works great.

Edited by Potnop, 14 January 2009 - 04:45 AM.

  • 0

#37 gandalf20000

gandalf20000

    GMC Member

  • New Member
  • 478 posts

Posted 14 January 2009 - 04:38 PM

Well, besides talking about halo, or restating the original story as a more entertaining novel version, I think I'll actually try to expand the conversation.

I think the biggest thing zombieJim was missing was a reward/punishment system.

Now it stands to reason that zombieJim is a hungry guy, what with that big hole in his stomach and all, and not a very inquisitive one - seeing as how that last shotgun shell took out his brain's left hemisphere - so he should be much more entertained by eating (slower decay). But he should also have a long-term memory, recalling to mind that littleGirlSuzie was a quicker catch than sprinterJose. He should learn that hunterBob did some serious damage whereas kittyCat did very little. He should learn that examining chair gave him no benefit whereas chasing human led to feasting.

Basically, every encounter should trigger some sort of reward/punishment. Every time zombieJim is shot, he is punished 3 points. Every time he trips he is punished 1. Every time he eats he is rewarded with 10 points over the amount of time it took to catch said food. If his random meanderings are followed by a reward, zombieJim is rewarded further with an extra point or two. If his meanderings are followed by punishment, he likewise receives further punishment. Storing the total number of points received for each item in a long term memory (perhaps also with some decay) will allow zombieJim to "learn".

So iteration one, zombieJim is placed in a room with a nearby hunter, a mid-range sprinter, and a distant old man he goes for the hunter the nearest catch. He is injured and now hunter has a punishment associated with him. Hunter will decay from memory slower, and have a lower priority.

Iteration two, zombieJim sees all three again, but hunter gives him bad vibes and has a much lower priority than last iteration so zombieJim determines sprinter is a better target. After chasing sprinter for some ways he looses sight of sprinter and is left considerably more tired and no less hungry. Sprinter also receives punishment points and has a lower interest to zombieJim (though since no actual harm came the punishment would be less than hunter, making sprinter more enticing).

Iteration three, zombieJim has ruled out both hunter and sprinter as viable food and begins to wander. As he wanders he comes across oldMan and the chase ensues. zombieJim catches oldMan very quickly and thus gets a large reward. Old man now has a much higher priority and decays from memory slower. When oldMan is encountered he zombieJim will target him more readily and take note of him longer. Also zombieJim learned something new today. Wandering can lead to potential discoveries. Since wandering was followed by a reward wandering receives reward points and will have a slightly higher priority. zombieJim will now be more willing to forgo the closest object and look around for something more enticing.

An alternate to iteration three could be that zombieJim decides to wander and finds himself in a trap. Since wandering is followed by punishment, it gets punishment points, leading to a very timid zombieJim that waits for food to come to him rather than finding it.


I like this idea. It would be give each different enemy individuality by going through different experiences. Giving each different enemy their own personality and individuality would be very important, since it would be more realistic. A player would get bored very easily by learning an enemy's tactics, and therefore always knowing what they'll do. When one makes enemy unique, it achieves a whole new level of realism, because the player no longer knows how the enemy will react.

Back on ZombieJim, treating him as an enemy in a game.
The player runs tromping through ZombieJim's home with a big shotgun. If ZombieJim wasn't made with a punishment and reward system, he would be like a stereotypical zombie, and run after you thinking, "LUNCH!!!!" and try to eat you. However, he learned from his experience with the hunter that big boom boom sticks means dangerous. He tries to sneak around you and be very quiet. However, the player has very well honed hearing skills, and hears the zombie. But, the player isn't that accurate, which is why he carries a shotgun. He spins around, shoots at ZombieJim, and misses him, but some shrapnel catches ZombieJim in the arm. He runs off, moaning and retching like he usually does. On the player's next encounter with ZombieJim, he gets eaten, because ZombieJim was much quieter, and the player underestimated the zombie's abilities.

I think that if ZombieJim manages to eat or defeat a very difficult target, such as a sprinter or a runner, he should be rewarded a little bit more than on a target he gets all the time, because he's learned that he's good enough to chomp down on them with no problems.
  • 0

#38 xDanielx

xDanielx

    GMC Member

  • GMC Member
  • 1001 posts
  • Version:Unknown

Posted 14 January 2009 - 07:59 PM

FSMs are poor learning machines inandof themselves, though. One could simulate very weak intelligence with states like "run from shotgun" or "pursue men on sight" (in an FSM that's separate from the main one), but it's generally much better to use neural networks or other systems which aren't limited to discrete states. They can, of course, be used in conjunction with FSMs -- FSMs handle the main behavioral state of an animat, while a neural network handles the state transitions (and potentially other things outside the scope of the FSM).
  • 0

#39 jakman4242

jakman4242

    Vertical Shmup Junkie

  • New Member
  • 3222 posts

Posted 15 January 2009 - 02:13 AM

FSMs are poor learning machines inandof themselves, though. One could simulate very weak intelligence with states like "run from shotgun" or "pursue men on sight" (in an FSM that's separate from the main one), but it's generally much better to use neural networks or other systems which aren't limited to discrete states. They can, of course, be used in conjunction with FSMs -- FSMs handle the main behavioral state of an animat, while a neural network handles the state transitions (and potentially other things outside the scope of the FSM).


Well, FSMs never need to be that specific.

You could simply have actions such as "Run Away" or "Pursue". You could then have a variable to determine the object of importance or significance. Such as "Shotgun" or "Men".
  • 0

#40 T-Bird

T-Bird

    GMC Member

  • New Member
  • 1326 posts

Posted 15 January 2009 - 05:56 AM

Of course FSM are poor learning machines. But that's not the point of this topic. This topic is about building powerful/useful FSMs and one way to do that is to give it a "memory" table so it imitates simplistic learning.
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users