Any suggestions on how to create a basic AI?


I’ve finally managed to get the program I’m working on to a point where I can get to work on a basic AI for the enemies. At the moment everything is controlled with a few if-else statements saying something along the lines of “If the player has less health than me I should cast a spell.” and other things like that. It’s pretty simple and it’s worked well enough for testing out various things, but now I need to start giving the enemies some sort of AI so that they can decide things on their own.

The only way I thought that this could work would be to either expand the if-else statements or to save the decisions made by both the player and creature from every battle that has/will ever be fought and then do some statistics math to figure out the best decision based on previous battles. Both of these ways seem a bit difficult so I was wondering if anyone else had some suggestions for how I could create the ‘AI’ for my creatures.

AI is a huge topic and there are lots of possible approaches (decision trees, state machines, GOAP). For a simple game, some if-then statements can actually take you pretty far. Pulling lots of historical data to do statistics on is a possible solution but I don’t think there is a particular need to do that here. For one, your Entities do not need to take the absolute best action at every step, only that they take actions that make sense.

If you have a reasonably short list of possible actions, one approach might be to loop through all of the possible actions the Entity could take and calculate a desirability score. The Entity would then execute the one with the highest score. The calculations could be very simple such as the desirability of the CastHealthSpellAction could be monster.getHealth() / monster.getMaxHealth();.

For a little variety you could:

  1. Have the Entity could randomly select between the 2-3 highest scoring Actions.
  2. Add a small bit of randomness to each calculated value so that the Entity does not always take the same Action in a given situation.
  3. Use “personality traits” of the Entity to help calculate values. For instance, give the Entity an AggressionScore. The desirability of a MeleAttack Action could then include the Aggression Score as part of the calculation. This way you could have Entities with a high aggression score that would be more likely to attack or those with a lower score who would be less likely.

Going further down this road can take you to full on GOAP (Goal Oriented Action Planning) but there is no reason for it to. Simply calculating the most desirable action based on the Entity’s personality and the current situation should be enough to give you a pretty decent AI.

(Actual posted right before me, I haven’t read what he said yet.)

I would just find some strategies a person would/did come up with and make the computer player emulate it. One way to make the AI “think” is to brute force/best first/min-max search possible moves over the next n turns and choose the best option assuming a rational opponent.

If you want to go with reusing statistics you can use a genetic algorithm. Give different tactics different trigger levels, vary it between monsters, and when you respawn monsters copy and mutate stats from the monsters that did the most damage before dieing. Although it can be gamed fairly easily if the player has enough control. (Just like real world evolution; where it is called domestication.)

Edit to reflect actual’s post:

Randomness is important. The weird thing about AI is that it only looks smart if you strike the right balance between artificial intelligence and artificial stupidity. An AI should not be predictable (where it can be defeated easily); even if the AI is perfect, in which case the battle seems more like a stand off than a game. (Read about the prisoners’ dilemma to get an idea of how counter intuitive completely rational decisions can be.)

Personality is a great addition. I think that is what some RPGs basically do when they have similar enemies with different tactics. (Brute force, healer, stat boost, poison, etc.)

Thanks for the replies!

Some of what you’ve both talked about goes along with what I’ve been thinking but the personality idea seemed really good. I’m thinking of having it work something like this…

Each creature will have an aggressiveness trait, just as you’ve suggested, and that will decide where it’s decision making process will start. I’ll have a few, probably around four or five, methods for each ‘level’ of aggressiveness and one will be chosen based on the creature’s aggressiveness; some creatures will have a set aggressiveness where as others will have one randomly generated within a certain range.
The next stage would be to take the creature’s intelligence stat, and depending on the stat, break off into the first level of if-else statements within whichever aggressiveness method is being used. Depending on the creature’s intelligence it’s actions will be more basic or more elaborate and, as the stat implies, intelligent.
If the creature is ‘dumb’ then it will, obviously, act dumb and probably only have a few branching statements depending on the health of the creature and the player as I’ve used for testing. Some of the ‘dumb’ decisions will be randomized but, for the most part, it will be set by health.
If the creature is intelligent then it’s decision making will factor in health, mana, armor points and various other things that it would be able to figure out if it were real. The branching statements would go somewhat deep but it should be sufficiently random since it will change depending on many different factors.

I pretty much just typed this out to remind myself of what I’m thinking at this moment but if anyone has some more suggestions it would be great. ^.^

I remember this lecture was one of my first introductions to AI (Game Trees).