Help with AI!

This topic is 3917 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi again. I need some help with AI. Im making a simple(really simple) 2d shooter game, and im trying to figure out how to move the enemies. So far i've managed to make them scrolling (x axis) , but im thinking to add more complex movement functions. Any ideas how to do it?

Share on other sites
You said how to move. Is the problem actually how to move them or is the problem coming up with an AI algorithm that tells the entities when to move?

-me

Share on other sites
I'll just preempt my question and assume you mean how to design the AI. =)

One possible architecture would be:

1) a behavior chooser (a container for behaviors)
2) a set of behaviors (that live in the behavior)

Each behavior has a couple methods:

virtual void start();virtual void update();virtual bool canRun();virtual void stop();

Start does whatever happens at the start of the behavior (initiate firing, jump, start pathfinding, whatever)

update just updates the behavior (does it need to path follow, continue shooting) It should also check for end conditions (is the player dead, is he no longer near me, did i finish jumping, etc).

canRun() is a heuristic for if the behavior is an acceptible choice for the moment.

stop just ends the behavior

Then basically each frame the BehaviorChooser checks to see if a behavior is running, if none are running collect a list of all behaviors that canRun. then choose randomly between them and start the selection.

Might be overkill for a 2D shooter but it's a nice framework. =)

-me

Share on other sites
@Palidine:
Thanks , but thats kinda...complicated...i dont think that i can do that yet.

Quote:
 Original post by PalidineYou said how to move. Is the problem actually how to move them or is the problem coming up with an AI algorithm that tells the entities when to move?-me

Sorry , i thought that it has to do with AI...

What i actually want is to make them move in different patterns.

Share on other sites
I would say that your question does pertain to AI as you are trying to give your enemies the illusion of intelligence.

The question becomes what level of intelligence you are after. If you just want some simple “paths” then making a simple system involving nodes and linear interpolation might work just fine for your purposes.

To make this sort of a system you would need a list of points. You would start at the first point and use linear interpolation to move to the second point, then the third, ect. Then based on the path options you could choose to go back to the starting point.

Then there is always the option of using trig to help compute path nodes…

Really though hard paths are not the best way to deal with enemy intelligence. I would recommend representing each enemy as a finite state machine.

Each enemy would have a number of “states” like “patrolling”, “attacking”, and “escaping” and a number of rules for going from one state to another. To go from the “moving” state to the “patrolling” state, a rule could be to test if the player is the line of sight and if the enemy has ammo. If the player is in line of sight and the enemy doesn’t have ammo then we would do the “escaping” state. If the enemy is in the attacking state and then runs out of ammo then you would go to the escaping state. If the enemy loses sight of the enemy and is in attacking state then you would go to the patrolling state.

The key is that the enemy can only be in one state at a time and each state has different behaviors. By adding rules and by adding states, we can build a large layer of complexity.

Personally I like this tutorial on the subject: AI-Junkie agent tutorial

I hope this helps!

Share on other sites
Quote:
 Original post by 3Dgonewild@Palidine:Thanks , but thats kinda...complicated...i dont think that i can do that yet.

Heh. fair enough.

If you just want different movement patterns then code up a few and choose randomly between them.

If the question is how to make different patterns first define the "different patterns" that you want to make. You can't design code to make something happen unless you know exactly what you want. If you could describe in more detail exactly what type of movement you are after that would be a big help.

If you need help coming up with those types of movement could you describe your game a little more? What are the enemies: infantry, tanks, airplanes, etc.

-me

Share on other sites

If i turn my game(actually a cube vs mad triangles) into a space shooter? it will be alot easier right?

edit:
I wanted to make a shooter like "metal slug" ..but i guess i need more practice.
So , im going to make a space shooter.

Share on other sites
Well yes, that makes things easier from an AI perspective. Chances are you would just need to "move" and "shoot".

I personally feel that you should attempt to make the AI becuase it will give you experience in the area and it will give you a sense of accomplishment when you pull it off :).

Share on other sites
But for now , what kind of math functions i'll have to use?

Share on other sites
Never;mind.
I've just installed mame and played few games.
I think i understand how they move!

Share on other sites
If your having the enemies scroll from one side of the screen to the other, and you just want to add in some fancy variations, you could try using some sort of path-node system where nodes are different x,y coordinates on the screen, and the enemy just goes from node[0] to node[n] in sequential order.

Path1 //makes enemy move from left to right, in the center of a 800x600 screen
node[0]=(0,300);
node[1]=(800,300);

Path2//enemy zig zags accross screen
node[0]=(0,200);
node[1]=(200,100);
node[2]=(400,300);
node[3]=(600,200);
node[4]=(800,100);

etc..

Then for each enemy, you can pick randomly which path to choose...

Share on other sites
you can always put the enemies on different kinds of sin/cos waves.

eg
x++
y = (sin(x)+1)/2*480

Share on other sites
Quote:
 Original post by Hodgmanyou can always put the enemies on different kinds of sin/cos waves.egx++y = (sin(x)+1)/2*480

I tried this one , but they're moving like crazy!
So fast than you cant acutally see them(!!)

Share on other sites
heh, that was just a suggestion - i didnt mean for you to copy it exactly.

const float horizontalSpeed = 0.01f;
const float sineSpeed = 0.5f;
const float screenHeight= 480.0f;
x += horizontalSpeed;
y = (sin(x*sineSpeed)+1)/2*screenHeight;

Share on other sites
Nice , thanks!
I learned something new!

Share on other sites
Hellooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo!!!!!!!!. Im having a little problem with this code:

//OUT OF SCREEN?if (NPCY<0){NPCY+=1;}if (NPCY>=400) {NPCY-=1;}

After a while the object stays at position 400....what im doing wrong?

Share on other sites
Quote:
 Original post by 3DgonewildHellooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo!!!!!!!!. Im having a little problem with this code:*** Source Snippet Removed ***After a while the object stays at position 400....what im doing wrong?

// Let NPCY be 400if (NPCY<0) // is (400 < 0){  NPCY+=1;  // No..}if (NPCY>=400) // is (400 >= 400){  NPCY-=1; // yes, let NPCY be 399}// next iteration, NPCY is now 399if (NPCY<0) // is (399 < 0){  NPCY+=1;  // No..}if (NPCY>=400) // is (399 >= 400){  NPCY-=1; // no}// NPCY is now 399

You need to adjust direction using a delta.
NPCDY = 1; // initialize somewhere at start....if (NPCY<0) {  NPCDY = 1;  }if (NPCY>=400) {  NPCDY = -1; }NPCY += NPCDY;

Aha , i see..
Let me test it!