views:

136

answers:

3

I am looking now for some time about how can a programmer simulate a AI decision based on percentages of actions for the final fantasy tactic-like games (strategy game).

Say for example that the AI character has the following actions:

Attack 1: 10%
Attack 2: 9%
Magic : 4%
Move : 1%

All of this is far from equaling 100%

Now at first I though about having an array with 100 empty slots, attack would have 10 slots, attack 2 9 slots on the array. Combining random I could get the action to do then. My problem here is it is not really efficient, or doesn't seem to be. Also important thing, what do I do if I get on an empty slot. Do I have to calculate for each character all actions based on 100% or define maybe a "default" action for everyone ?

Or maybe there is a more efficient way to see all of this ? I think that percentage is the easiest way to implement an AI.

+4  A: 

The best answer I can come up with is to make a list of all the possible moves you want the character to have, give each a relative value, then scale all of them to total 100%.

EDIT:

For example, here are three moves I have. I want attack and magic to be equally likely, and fleeing to be half as likely as attacking or using magic:

  • attack = 20
  • magic = 20
  • flee = 10

This adds up to 50, so dividing each by this total gives me a fractional value (multiply by 100 for percentage):

  • attack = 0.4
  • magic = 0.4
  • flee = 0.2

Then, I would make from this a list of cumulative values (i.e. each entry is a sum of that entry and all that came before it):

  • attack = 0.4
  • magic = 0.8
  • flee = 1

Now, generate a random number between 0 and 1 and find the first entry in the list that is greater than or equal to that number. That is the move you make.

gnovice
Interesting, I will need to check that.So basically,1- declare2- While loop, add up3- Get the random4- Loop and 1st up or equal is the action to do.I'll try something like that tonight and be back with some results.
Erick
Another possible formulation: 1) In a loop, set "value" for each move and add to a total "sum". 2) Generate random number and multiply it by "sum". 3) In a loop, add "value" for each move to a running total and stop when it is larger than the random value.
gnovice
after some verifications it seems it will be the easiest one. Thanks for the tip :-D
Erick
Glad to help! =)
gnovice
+2  A: 

No, you just create threshholds. One simple way is:

0 - 9 -> Attack1

10 - 18 -> Attack 2

19 - 22 -> Magic

23 -> Move

Something else -> 24-99 (you need to add up to 100)

Now create a random number and mod it by 100 (so num = randomNumber % 100) to define your action. The better the random number to close to a proper distribution you will get. So you take the result and see which category it falls into. You can actually make this even more efficient but it is a good start.

BobbyShaftoe
+1  A: 

Well if they don't all add up to 100 they aren't really percentages. This doesnt matter though. you just need to figure out the relative probability of each action. To do this use the following formula...

prob = value_of_action / total_value_of_all_actions

This gives you a number between 0 and 1. if you really want a percentage rather than a fraction, multiply it by 100.

here is an example:

prob_attack = 10 / (10 + 9 + 4 + 1)
            = 10 / 24
            = 0.4167

This equates to attack being chosen 41.67% of the time.

you can then generate thresholds as is mentioned in other answers. And use a random number between 0 and 1 to choose your action.

Jack Ryan