//Title: "Expert System" AI Snippet
//Credit to: Dan of Dantom
//Contributed by: Jtgibson
//I'll let Dan explain the whole thing. --Jtgibson
/*
"Well, one of the simplest (and often quite powerful) forms of AI
is a rule-based system (often called an "expert system"). You may have
heard of fuzzy-logic. That is another form of rule-based intelligence.
A perfect application is in the control of NPCs. For example, you
could give your mobs several emotional states (say SCARED, PEACEFUL,
CURIOUS, ANGRY, BLOODTHIRSTY). Then, based on events in the game, you
would need a set of rules defining the mob's reaction to events based
on its mood. Example:"
*/
var/const/MOOD_SCARED = -1
var/const/MOOD_PEACEFUL = 0
var/const/MOOD_CURIOUS = 1
var/const/MOOD_ANGRY = 2
var/const/MOOD_BLOODTHIRSTY = 3
mob
var
health
natural_health
mood
natural_mood
mob/enemy
mob/friend
proc
SeeViolence(Attacker,Target) //someone nearby attacks
SeeDeath(Target) //someone nearby dies
Tick() //call this every round
mob/SeeViolence(mob/Attacker,mob/Target)
if(Attacker == src) return
if(Target == src)
src.enemy = Attacker
if(src.health < src.natural_health * 0.1) src.mood = SCARED
else src.mood = ANGRY
if(Target == src.friend)
if(src.enemy) return //already have an enemy
src.enemy = Attacker
if(src.health < src.natural_health * 0.1) src.mood = SCARED
else src.mood = ANGRY
mob/SeeDeath(mob/Target)
if(Target == src.enemy)
src.enemy = null //clear our current target and go back to normal
src.mood = src.natural_mood
mob/Tick()
switch(src.mood)
if(ANGRY)
//TODO: attack enemy
;
if(SCARED)
//TODO: flee from enemy
;
if(BLOODTHIRSTY)
//TODO: find a new target to attack
;
if(CURIOUS)
//TODO: wander around
;
//I added a couple comments where they would be necessary.
//You'll have to call the SeeViolence(attacker, target) and SeeDeath(target)
// procs in your combat procedures, but otherwise this code is bulletproof.
//Note that the semicolons on their own lines by themselves mean to
//"do nothing". Obviously, you wouldn't use those in any normal case,
// but, in this situation, they're necessary to prevent the compiler from
// flagging the presently useless switch statement as an error. -- Jtgibson
/*
"I have left out some of the details (like actually calling the mob procs
at appropriate places), but I think you see the point. The rules are
generally in the form of an 'if' statement. The condition generally
involves some aspect of the mob's state (mood, enemy, etc.) and some new
piece of information (Attacker, Target, etc.). The action taken by the
rule is often a change of the mob's internal state. The mob in this
example is what computer scientists call a finite-state-machine. There
are millions of interesting possibilities. For example, you could make
the mobs more intelligent by keeping track of more than just one enemy
or friend at a time (possibly using lists). You could add more
emotional states to account for things like hunger, loyalty, morale,
etc. The beauty, though, is that with a small number of well chosen
rules, you can get some reasonably intelligent behavior.
There are other forms of AI that try to learn from past experience (i.e.
to generate new rules). It is probably best to start with a good set of
hand-coded rules first, and work in learning techniques as an added
feature. I personally think the most promising technique for use here
would be a genetic algorithm. The mobs would have a set of genes (i.e.
parameters governing their behavior) and those who do the best would be
used as the genetic stock for future generations of mobs. Hmm. Sounds
like fun." -- Dan of Dantom
*/
ID:195160
Nov 21 2006, 6:05 am (Edited on Nov 21 2006, 6:36 am)
|
|
throw in a random 'weighted' value to add to the mood, and then the mob can respond differently to similar situations.
or, for added 'realism', add more weight to certain values to set trends in a mob's behavior. for example, every time the mob sees another of its kind killed, it's mood swings towards 'bloodthirsty' a little bit more (use larger numbers for a higher resolution of mood). acts of kindness to the mob, swing it back towards peacefulness.