Advertisement

AI idea needs opinions

Started by February 01, 2002 08:57 AM
5 comments, last by Vendayan 22 years, 9 months ago
I''m trying to make a game where the world will appear functional, I want citizens of a city to have different states, (naturally) such as work, play, defending, fleeing, attacking, and more more specfic ones that may determine the way an NPC will attack or defend. I''m wondering about the natural part of the AI. I''m wanting the NPC''s to walk in a semi-random fashion. When a character is created he will be given different things that he or she might do in any given situation. For instance my AI designer will let a guard patrol along a predetermined path for 80% of his "work" time, every time he finishes a task, he will randomly choose another, he may patrol his area 4 times and then decide to "go to the kitchen for food", then when he is done he will more than likely patrol again, although he may decide to "use the restroom" or "talk to other guard". My AI designer will allow a programmer to determine all of the points that my npc will need to walk to as a script and then at the end (if the NPC is not disturbed or attacked beforehand) he will randomly choose another reasonable action. The scripts will include a list of actions such as, goto( point ), goto( character ), pause( time ), etc.. Each action will also have a list of things that can change the characters state and what they are likely to be changed to. For instance the guard atacked while using the commode is much more likely to defend than attack. Tell me what you think. I know its nothing original but is it a good idea or does anyone have a better one? ~Vendayan
"Never have a battle of wits with an unarmed man. He will surely attempt to disarm you as well"~Vendayan
By the way, would anyone see a problem in implementing this simply by making the character class have a "state" member and a function pointer to his current action? Which would be ran once per frame?

~Vendayan
"Never have a battle of wits with an unarmed man. He will surely attempt to disarm you as well"~Vendayan
Advertisement
Sounds like the way a lot of RPGs do it. I wouldn''t use function pointers though. Hard to describe what I''d do instead, but I wouldn''t link it directly from 1 state -> 1 function.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]
Could you try giving a general idea of how it should be done? I only assumed that a function pointer would be the best way to go about it. A function pointer to a Moveto(); function would not only be a quick way of setting the NPC into action but it would also be a way of checking to see what action he is currently in. The Moveto function would only move him the distance he should have moved since the last frame btw, if it wasnt obvious as i said it.

~Vendayan
"Never have a battle of wits with an unarmed man. He will surely attempt to disarm you as well"~Vendayan
I don''t know if this is on the lines that you''re on about but i''ll have a go at explaing how i''d do it..

Each NPC would have a set of information about him/herself which
controls their actions, like age, gender, bladder, hunger,
boredom, etcetc.

So with that suppose we have a guard who''s happily stopping you from entering castle Boojum to save King FooBar(ok i''m not a story writer), anyway, his bladder amount is increasing as you
wait, and then when it hits the magical mark 80/100 then off
he walks to the nearest toilet, allowing you to run in. Ok, so
thats not the best example.

Another example, boredom, you could have a guy sat on a wall
somewhere, and when he see''s a female roughly the same age as him he''ll hop off the wall and go in for the kill , nah, u know wot i mean

How the sub-states for each NPC would be calculated would be up to you, the older they are the faster bladder goes up, younger people get bored more easily, etcetc.

That was either some help or a load of rubbish on my part

DarkStar
You would have to call MoveTo quite a few times before you reach your destination. So that means you have to remember where you are moving to across all those calls. Where is that information going to be stored, and in what form? Say you just add a couple of variables to NPC to accommodate this... how are you then going to accommodate a ''FollowOtherNPC'' state? Or an ''AttackPC'' state? You''ll end up with a load of context-dependent variables that are useless 90% of the time.

The way I would approach anything like this is to use a pointer to a State object, which has subclasses of various states, including something like MoveTo. You initialise the MoveTo object with the destination so it remembers it for you and keeps the NPC class uncluttered. When MoveTo is completed (signal this with a return value from one of MoveTo''s functions or something), you can replace that state with a different one, such as WaitUntil (which is initialised with a time), or so on.

Any clearer?

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]
Advertisement
quote: Original post by Vendayan
I''m wondering about the natural part of the AI. I''m wanting the NPC''s to walk in a semi-random fashion. When a character is created he will be given different things that he or she might do in any given situation. For instance my AI designer will let a guard patrol along a predetermined path for 80% of his "work" time, every time he finishes a task, he will randomly choose another, he may patrol his area 4 times and then decide to "go to the kitchen for food", then when he is done he will more than likely patrol again, although he may decide to "use the restroom" or "talk to other guard".


This is a perfect scenario for using a Probabilistic Finite State Machine. In the deterministic Finite State Maching - which is just a finite state automaton - the transition from one state to the next is determined purely by a set of predefined, deterministic causes.

In a Probabilistic Finite State Machine - also known as a stochastic finite state automaton - the transition out of a state is expressed as a conditional probability distribution (in this case a table, a CPT) with elements Pr(new_state=statei|current_state=statej). For each statej there will be a table with one entry for each statei. The sum of each element in this statej''s table should be equal to 1. To determine which state to move to from state j, simply sample from it''s CPT. This will give a slight element of unpredictability to the NPCs actions.

Cheers,

Timkin

This topic is closed to new replies.

Advertisement