Artificial Humanity
|
OK, this is going to seem like a complete rip off, but it isn't. Seriously. The AI coding article is detailed and pretty thorough, but as far as above view/isometric opponents went, I thought it was a little over simplified. Therefore this article is going to focus on AI for that sort of game, and constitutes the core of the AI that I am using for one of my current projects. Antimatter entertainment stressed that there were many ways of doing pretty much anything, so I hope he doesn't mind if I give you mine. Be warned, this is going to be pretty complex, and to save me from going insane I'm not going to detail every event, but just give you the basic idea of how to do this. Also, be aware that this method is extremely active-object intensive, and for larger numbers of enemies you're far better of with the Antimatter entertainment method. I'm also going to assume that you know how to limit the range of an active object. To do this simply always add 1 to one of it's alterable values, and when that value is equal to the required amount (probably around twenty for the sake of this article, but the exact amount is entirely up to you) then destroy the object. One more thing, everything here is possible on KnP up, so don't be put off.
The basic process of programming AI requires you to think how a human would react. A person would not necessarily shoot you the moment he saw you, for example. He wouldn't bounce his way around walls to get to you. He would react to the sight of a body, and he would probably tell his co-workers if he saw you, or heard you. He would investigate where he thinks you are, and not necessarily your actual location. He would see you in front, but if you were loud he could hear you behind. I'm going to tell you how to do all of this, and hopefully you'll be able to improvise some little touches of your own (enemies retreating, etc.).
So, here's how you go about it. Your enemies should be on bounce ball movement. Put in a few random events, like every .2 of a second pick one at random and set direction to any (select all directions). Use the same method to stop and start some of them randomly. This will make them a lot less predictable, and more human. You don't want them to walk into walls, so tell them to shoot an object (speed 100) that looks like a growing barrier in front of them roughly every .3 or.4 seconds, and limit it's range (make sure the object is rotated for all directions). This should look like a radar. Tell the object to change animation to user animation 1 if it collides with the backdrop, and set direction to it's own direction plus 16. User animation 1 (you can name it if it'll help) should look like a ball, not nearly so large as the radar. You'll also probably want to set the radar's alterable value (the one that limit's it's range) to only marginally less than the limiting value, so that the enemies do not turn away from a wall halfway across the screen. What should happen now is that the enemy will shoot out this radar (it'll help if you test with more than 1 of them) and if the enemies radar hits a backdrop object the radar will send a signal back to him telling him that the backdrop is there. You will have to experiment with limiting the radar range in order to perfect this. When the radar collides with the enemy, and the radar is playing that animation, then destroy the radar and set the enemies direction to it's own direction -8 +random (17). This will make the enemy turn a random amount left or right. Then add an event saying to always make the radar invisible, and that's your basic person.
Obviously the person needs to be able to see you. Good thing that radar is multipurpose. Tell it that when it collides with your character it should do exactly the same as with the backdrop, but this time change it to user animation 2 (which should be otherwise identical to user animation 1). Then, when this collides with the enemy, and enemy's value B is 0 (I'll tell you why in a minute,) tell it to set the enemies value B to 200 (assuming you want value A for health) and make the enemy look at your character. Value B represents the enemy's alert state. At 0 the enemy is not alert. At 1 he is alert. At >1<100, he is seeing you, and attacking, but at >100 he has seen you, but is still reacting (he's going to be surprised, after all by an elite anti terrorist agent/ robot from the future/ gun toting rabbit). Obviously, experiment with these values to get the reaction times you want, but from here on I'll be using the values given. You'll also need a brand new object, that'll represent where the enemies think you are, and when this event occurs this must be set to the position of the player.
Obviously, an enemy cannot react forever, so put an event in that says when value B>1 subtract 1 from value B. This will take the enemies alert state down to alert, but not attacking over time. Needless to say that when 1<value B<100 the enemy needs to attack. Make it so that if the radar collides with the enemy and user animation 2 is playing and 0<value B<100 value B will be set to 99 (otherwise make the event the same as above) so that once the enemy has seen you it will not react again, and since they are already alert they will attack straight away. Now make it so that if value B>0 and pick one at random every .4 of a second look in direction of the believed location object, so long as it's value A>1 (set it to a value, maybe 1000 every time it is positioned relative to the player, this way enemies will lose interest in that location after a while). By now you should have an enemy that will see you, take a moment to react, then attack and go after you, and go to where they think you are. Thy may lose sight of you quite easily, so you may want them to shoot another, thinner object (so it's less likely to hit the backdrop) that always looks at your character, and make it so that if that collides with your character it sets the believed location object relative to your character at (0,0). Make sure that if this collides with the backdrop it is destroyed.
By now, you've done all the hard work. From here you have the basis on which everything else can be easily built. For example, when their radar collides with a body, this can change it to another user animation, that (so long as the enemy is not more alert) set's their alert state to 1, so that when they see you they will react immediately -obviously useful for a stealth game where you want to encourage the player to hide bodies. Sound can consist of small ball-like objects; shot in a random direction when your character walks that cause the enemies to look at you upon collision. Communication between enemies can be handled in the same way, whereby alert enemies shoot objects that alert more relaxed opponents. The good news is that all of this works, very well in fact, and I speak from experience. It isn't hard to imagine how to improvise further complexities, like explosions that alert all enemies but temporarily distract them, thereby setting their alert state down to 1, and causing them to look at the explosion. Or how about the ability to create point of interest, that attract guards of all alert states -maybe one is created after an explosion, simulating the enemies instinct to investigate. It's all possible, and to be honest, none of it's that hard, it just requires you to think a little about how to make them behave like people, not a swarm of savage aliens.
Well, I hope at least the length of this article (1463 words, to save some people the effort) will convince some of you that it isn't a rip off, and I hope those of you who can be bothered to read it can benefit in some way. I guess a lot of you will have thought of at least some of this already, but hopefully some of you will benefit from this too.
|
|
|