Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (707)
Games in Android Showcase (208)
games submitted by our members
Games in WIP (781)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Any suggestions on how to create a basic AI?  (Read 2232 times)
0 Members and 1 Guest are viewing this topic.
Offline tyeeeee1
« Posted 2013-08-28 19:06:24 »


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.
Offline actual

JGO Coder

Medals: 25

« Reply #1 - Posted 2013-08-28 19:52:43 »

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.

Offline Several Kilo-Bytes

Senior Devvie

Medals: 11

« Reply #2 - Posted 2013-08-28 19:55:55 »

(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.)
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline tyeeeee1
« Reply #3 - Posted 2013-08-29 04:24:51 »

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. ^.^
Offline jonjava
« Reply #4 - Posted 2013-08-29 12:38:46 »

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

Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

Galdo (477 views)
2017-01-12 13:44:09

Archive (630 views)
2017-01-02 05:31:41

0AndrewShepherd0 (1102 views)
2016-12-16 03:58:39

0AndrewShepherd0 (1027 views)
2016-12-15 21:50:57

Lunch (1158 views)
2016-12-06 16:01:40

ral0r2 (1391 views)
2016-11-23 16:08:26

ClaasJG (1487 views)
2016-11-10 17:36:32

CoffeeChemist (1459 views)
2016-11-05 00:46:53

jay4842 (1541 views)
2016-11-01 19:04:52

theagentd (1260 views)
2016-10-24 17:51:53
List of Learning Resources
by elect
2016-09-09 09:47:55

List of Learning Resources
by elect
2016-09-08 09:47:20

List of Learning Resources
by elect
2016-09-08 09:46:51

List of Learning Resources
by elect
2016-09-08 09:46:27

List of Learning Resources
by elect
2016-09-08 09:45:41

List of Learning Resources
by elect
2016-09-08 08:39:20

List of Learning Resources
by elect
2016-09-08 08:38:19

Rendering resources
by Roquen
2016-08-08 05:55:21 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!