Page 1 of 1
AI Design
Posted: Thu Mar 18, 2010 5:42 pm
by Maevik
I'm having an issue with the AI design of the SHMUP game I'm building at the moment and was hoping the community might have some insight or input to help me out.
While planning out my AI procedures, I'm finding that for a lot of the functions I wan't to know the location of the player so that I can do things like:
Trigger things by proximity
Fly directly at the player
Shoot directly at the player
Avoid the Player
Is there a good method for allowing all instances of my Enemy class knowing at all times where my instances of Player class are?
My only real option I can see is passing a ptr to the player every time I update the enemy. And do that for each enemy I guess. I would also have to do that for each player when I implement multiplayer. Something like:
Code: Select all
myEnemy.update( player1 , player2 );
Re: AI Design
Posted: Thu Mar 18, 2010 6:21 pm
by avansc
what you want is shared memory
lets assume that no enemy knows where the player is.
when a enemy sees the player it post the location to the shared memory of the AI, that way all enemies know the location.
you can go in pretty complex with this, but that is the simple version.
Re: AI Design
Posted: Thu Mar 18, 2010 6:32 pm
by Maevik
Ok, not exactly that far just yet, but in a way you hit the nail right on the head. I guess I'm essentially asking "How do I get my enemies to SEE the player?"
I suppose the easy answer would be what I presented in the OP, where you just update the enemy with the player location ever frame by passing it into the update function. Are there better algorithms for this? What other ways can I get my Enemies to see my Players?
Re: AI Design
Posted: Thu Mar 18, 2010 7:00 pm
by dandymcgee
I think what I did last time I came across this issue is store a target pointer. Essentially your Enemy class has a member pointer (or maybe an array, list or vector in your case).
Code: Select all
class Enemy{
vector target<*Player>;
}
Store 'em once and they're available whenever you need them. Varying targets are a bit more work as you have to remember to add the new instances where needed and avoid storing dead pointers.
May not be the best method, but it's all I can think of atm.
Re: AI Design
Posted: Thu Mar 18, 2010 7:12 pm
by Innerscope
Maevik wrote:Ok, not exactly that far just yet, but in a way you hit the nail right on the head. I guess I'm essentially asking "How do I get my enemies to SEE the player?"
I suppose the easy answer would be what I presented in the OP, where you just update the enemy with the player location ever frame by passing it into the update function. Are there better algorithms for this? What other ways can I get my Enemies to see my Players?
Do you want the enemy to always know where the player is?(omniscient A.I)
or do you want the enemy to have a "sense of sight" and not know the player's position until he's spotted?
For the first one, it's pretty easy, you can just have the player's position global. (feel free to harp at me about bad programming practices
)
The second one you would have something similar to a collision algorithm only the range would be the enemies line of sight or range of vision. You could then have some kind of flag for when the player's spotted and that could be relayed to other bots. The bots can have a targetLocation property (similar to what dandymcgee was saying), and within the sight algorithm you could have bots copy that property from other bots within range.
Re: AI Design
Posted: Thu Mar 18, 2010 7:18 pm
by thejahooli
You could just store a pointer to a Player object and when you initialize the enemy either pass a pointer to the player object that allocates it or have a function which registers the Player object.
Code: Select all
class Enemy
{
public:
Enemy(Player *player)
{
this->player = player;
}
private:
Player *player;
This would have the same output as passing the Player pointer to the Update function, but it is neater this way as you only have to send the object once.
Re: AI Design
Posted: Thu Mar 18, 2010 7:55 pm
by dandymcgee
thejahooli wrote:You could just store a pointer to a Player object and when you initialize the enemy either pass a pointer to the player object that allocates it or have a function which registers the Player object.
Code: Select all
class Enemy
{
public:
Enemy(Player *player)
{
this->player = player;
}
private:
Player *player;
This would have the same output as passing the Player pointer to the Update function, but it is neater this way as you only have to send the object once.
Don't take this the wrong way, but how is that different from what I just said?