I am new to game programming and am trying to make a basic 2d top-down space game with 2 space ships that fight each other. I am doing well with the user controlled space ship, but have no idea how to even start programming an AI. Are certain methods/patterns better for this situation? Where would I even begin?
|
|
The Strategy pattern is great for determining what to do but not when to do it. You're also going to need to use Finite State Machines to know which state your AI is in and what appropriate decisions are available. |
||||
|
|
|
If this is your first time developing AI, you don't have to worry about things like the state pattern, behavioural maths or even get a book on the subject. You can literally get away with something like this:
The main aspect of getting AI correct is by thinking about what behaviours you want the character to have and how this can be achieved realistically. So for a basic spaceship enemy, I'd imagine the possible actions it could carry out would be to:
And when could these actions happen?
After you have done this due to the low number of "states" that your character can be in, you can use a simple if statement like the one above. I would then recommend you look at Steering Behaviours as these are very simple behaviours to implement and can result in really good looking movement. More can be found here: http://www.red3d.com/cwr/steer/ When you are really comfortable with these, I would suggest looking at the State and Strategy Patterns. The point is to start out small and simple. Don't worry about all these terms that everyone keeps flinging around about FSMs and design patterns and whatever. Design and build first of all, then worry about how you can improve it by using existing methods or standards. Hope that helps! |
||||
|
|
|
You might want to look at OpenSteer, and the Steering Behaviors documentation that goes with it. The source code is not beginner-level, but the concepts behind it should give you some good ideas. |
|||||||||||
|
|
You might want to look at the Strategy design pattern. Essentially write up same basic strategies of how the ships will behave:
Then you will use logic (state machines) to choose between these strategies. For example: If the ships shields have fallen below 50%, then run away from the target and move toward power ups/healing items and so forth. |
|||||||
|
|
May I suggest that you buy the book Artificial Intelligence for Games by Ian Millington - it's excellent! :) http://www.ai4g.com/ The source code is at Github - MIT license. Of course, if you're not using C/C++, then this might be less relevant. But really an awesome introduction to the world of Artificial stupidity/intelligence. Things you're going to need from it are steering and state machines. For starters. |
|||
|
|
|
Some practical advice if you go for state machines: beware the megastate. It's tempting to have a "flee" state which handles setting a position to flee to, setting a high target speed, clearing all targets, etc. It's much better to instead have many state machines, each handling one or few ship controls - eg a state machine that handles selecting a target position, one that handles guns, etc. This may seem stupid, as you'd have to add flee states to each individual machine, but since you're constrained to only one or two outputs, your states end up very small so this should not be a problem. Also, since each control is now under individual control, you can end up selecting a flee target position while still having the attack state control your guns. You can strafe an enemy while still conserving engine power because of a coolant leak. You can steer to avoid a collision while still dumping countermeasures. All these would be impossible with a single all-encompassing state machine. |
|||
|
|
|
Behavioral Mathematics for Game AI Adding this book to the list of starting points for learning AI programming. With its focus on observation and behavior implementation rather than algorithms of limited application, I believe it is well suited to the task. |
|||
|
|
|
How to create AI is a tricky question, because it consists of many parts:
Other posters suggested great books and I'm fond of AI4Games suggested by jacmoe, even though it's quite general in parts (like a lot of books, which is of course understandable). Of course, each type of game has its specific needs, and arcade/shooter games don't need complex architectures to support interesting gameplay. Simple steering libs will do the job as the basis of that behavior, and then you add details for various types of enemies (some, shoot periodically, some shoot by predicting the player's movement, some turn in order to face the enemy, others may coordinate their attacks etc).
|
|||
|
|
|
Think about what you do when you play the game. Ask yourself some questions.
Then, what I do is have the AI look at the play field and make a list of possible actions or movements that the AI could make based on those questions. I rank and sort these moves with a "smartness factor". Then, it's as simple as picking and executing a movement/action based on some combination of randomness and whatever difficulty level the player chose at the beginning of the game. Then, after 500 milliseconds, I do it again. It doesn't have any learning abilities or anything but with enough tweaking it can be pretty convincing, which it all that really matters |
|||
|
|
|
I suggest you to first, try to make a pacman!! I don't have any other suggestion sorry :S |
|||||||||
|