# Monster's AI in an Action-RPG

I'm developing an action rpg with some University colleagues. We've gotton to the monsters' AI design and we would like to implement a sort of "utility-based AI" so we have a "thinker" that assigns a numeric value on all the monster's decisions and we choose the highest (or the most appropriate, depending on monster's iq) and assign it in the monster's collection of decisions (like a goal-driven design pattern) .

One solution we found is to write a mathematical formula for each decision, with all the important parameters for evaluation (so for a spell-decision we might have mp,distance from player, player's hp etc). This formula also has coefficients representing some of monster's behaviour (in this way we can alterate formulas by changing coefficients).

I've also read how "fuzzy logic" works; I was fascinated by it and by the many ways of expansion it has. I was wondering how we could use this technique to give our AI more semplicity, as in create evaluations with fuzzy rules such as
IF player_far AND mp_high AND hp_high THEN very_Desiderable
(for a spell having an high casting-time and consume high mp) and then 'defuzz' it. In this way it's also simple to create a monster behaviour by creating ad-hoc rules for every monster's IQ category.

But is it correct using fuzzy logic in a game with many parameters like an rpg? Is there a way of merging these two techniques?
Are there better AI design techniques for evaluating monster's chooses?

• Does the fuzzy logic give you the results you want? Then it's "correct". Typically you have to define what you want, then how it's achieved is pretty straight forward. – Tetrad Aug 23 '12 at 19:50
• Yes, but in an rpg I don't think that a fuzzy approach would be optimal! We have about 10-15 parameters to cover-up! So even if fuzzy logic can be used, I have to do a big work, covering all those parameters [create Fuzzy linguistic variables etc]! With a utility-based system the only work is to construct a formula that can represent an evaluation! – Andrea Tucci Aug 29 '12 at 9:59
• "Fuzzy logic" is the only good logic for AI, the more complex the game the more so. AI's don't need to precise or too knowledgeable, they just need to be fun to fight, and no math in this world will make a AI fun. – API-Beast Nov 3 '12 at 2:53

Imagine :
* Health is beetween 0/100% = 0.0/1.0
and you normalize each value so that they fit in this range
* Distance would be 100% if player within reach, and 100%-10%*(timetoreach) otherwise ( here monster further from 10 seconds wouldn't react to the player ).
Aggressivity would be a specific factor for each 'race' (maybe with radom changes to spice up things)
* Anger would be 100% (or even more) if monster was hit or treasure stolen or (near) monster's friend is hit by player, but less than 100% if the monster just saw you, and much less if monster is cool and didn't see you. (Well... you could say also that if the monster didn't saw you, it shouldn't attack. But monsters talk together...)
* Bersek would be a special factor that would be >1 and greatly raise the chance the ennemy attack : imagine an alarm bell is ringing : all monsters are seeking a target (and talk together or hear a message on speakers ... :-) )
* Mistrust would be 100% or higher if you are running fast dressed only with a flashing pink pant and with a sword covered of blood, and Mistrust would be very low if you walk slowly dressed as a priest...

So once you have all those normalized factors f1, f2, f3, .... you multiply them P=f1*f2*...*fn and you get the probability the monster attack within a given time range. So if (Math.random()>P) you make the monster attack (and grunt)...

If you need to adjust the relative influence of a factor, use Math.pow(Probability, power) to 'shape' the probabilty of each factor. if you use a pow >1, you give more influence to a factor, if you use a 0 < pow < 1, you give less influence to it.
Expl1 : with pow=3 (>1). 0.2 -> 0.08 0.5 -> 0.125 0.8 -> 0.512
Expl2 : with pow = 0.3 (<1). 0.2 -> 0.6 0.5 -> 0.81 0.8 -> 0.94. --> Conclusion : With a shape pow < 1, when the factor is close to 0 , this does not bring down the whole attack probabilitiy --> the factor has less influence with pow <1, and the less influence pow is closer to zero. (and vice versa).

The probability would be now P = pow(f1,p1)* pow (f2,p2) * pow (f3,p3) * ...
and each 'time range' (?= 10 seconds ?) you throw the dice and see what happens. But you should also throw the dice on a monster on which an event happened ( the monster saw the player / heard him / heard/saw a fight / heard the alarm ringing /... ).
Rq : Some events should always trigger an attack (monster attacked by player/...).
Rq2 : you might want to shape the random function also, and use other shaping function(sin, ln, ...)

computation can be faster if you detect fi == 0 within the factors ( or 'low' fi / 'low' shaped fi).

Hope this helps.

• I think this is a good and (relatively) fast way to implement an ai, mentioned also in many books (evaluate something, give it a worth and multiply values). Thank you – Andrea Tucci Dec 7 '12 at 22:31

Check out this book on Behavioral Mathematics by Dave Mark:

http://www.amazon.com/gp/aw/d/1584506849

He has also done some excellent talks at GDC if you happen to have Vault access.

The book goes over the use of utility functions, response curves, and composition of behaviors with planning using mathematics and simple architecture. This lets you combine those utility functions with decision making without needing to write another architecture for it. I've personally seen this approach successfully used for RPG AI, procedural obstacle course generation, and action decision making.