this is an example on emotions in a NPC
Code:
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <ctime>
using namespace std ;
class emotion
{
public :
char* name ;
double x ; //arousal
double y ; //stance
double z ; //valance
void fill( char* nm
, double a , double b , double c )
{
name = nm ;
x = a ;
y = b ;
z = c ;
}
} ;
const int accepting = 0 ;
const int alert = 1 ;
const int anger = 2 ;
const int calm = 3 ;
const int disgust = 4 ;
const int fear = 5 ;
const int happy = 6 ;
const int joy = 7 ;
const int sooted = 8 ;
const int sorrow = 9 ;
const int suprice = 10 ;
const int tired = 11 ;
const int unhappy = 12 ;
emotion emo[ 20 ] = {
{ "accepting" , 0.0 , 8.0 , 0.0 }
, { "alert" , 4.0 , 0.0 , 0.0 }
, { "anger" , 3.0 , -5.0 , 0.0 }
, { "calm" , 0.0 , 0.0 , 0.0 }
, { "disgust" , -1.0 , -5.0 , 0.0 }
, { "fear" , 3.0 , 5.0 , 3.0 }
, { "happy" , 0.0 , 0.0 , 8.0 }
, { "joy" , 2.0 , 0.0 , 3.0 }
, { "sooted" , -3.0 , 0.0 , 3.0 }
, { "sorrow" , -2.5 , -0.0 , -3.5 }
, { "suprice" , 8.0 , 0.0 , 0.0 }
, { "tired" , -4.0 , 0.0 , 0.0 }
, { "unhappy" , 0.0 , 0.0 , -4.0 } } ;
double dist( emotion a , emotion b )
{
emotion c ;
c.fill( " "
, a.x - b.x
, a.y - b.y
, a.z - b.z ) ;
return sqrt(
c.x * c.x
+ c.y * c.y
+ c.z * c.z )
+ 1e-10 ;
}
emotion Eadd( emotion a , emotion b )
{
emotion c ;
c.fill( " "
, a.x + b.x
, a.y + b.y
, a.z + b.z ) ;
return c ;
}
emotion move(
emotion a , emotion to , double m )
{
double q = dist( a , to ) ;
emotion b ;
b.fill( " "
, ( to.x - a.x ) / q * m
, ( to.y - a.y ) / q * m
, ( to.z - a.z ) / q * m ) ;
return Eadd( a , b ) ;
}
double rnd()
{
return (double)rand()/(double)RAND_MAX ;
}
int main(int argc, char *argv[])
{
char in = ' ' ;
srand( static_cast<unsigned int>( clock())) ;
emotion now = { " " , 0 , 0 , 0 } ;
emotion add ;
while ( in != 'y' )
{
int i , e ;
double d = 100 ;
for ( i = 0 ; i < 13 ; i++ )
{
if ( d >
dist( now , emo[ i ] ) )
{
e = i ;
d = dist( now , emo[ i ] ) ;
}
}
cout << "\n\n\n\n" ;
cout << "I feel " ;
cout << emo[ e ].name << " .\n\n" ;
cout << "Menu :\n" ;
cout << "q = show big spider\n" ;
cout << "w = show smal spider\n" ;
cout << "e = give hug\n" ;
cout << "r = give kick\n" ;
cout << "t = random reaction\n" ;
cout << "y = quit sim .\n\n" ;
cout << "choice = " ;
cin >> in ;
switch ( in )
{
case 'q' : //big spider
now = move( now , emo[ fear ] , 2 ) ;
break ;
case 'w' : //smal spider
now = move( now , emo[ fear ] , 1 ) ;
break ;
case 'e' : //hug
now = move( now , emo[ sooted ] , 2 ) ;
break ;
case 'r' : //kick
now = move( now , emo[ anger ] , 3 ) ;
break ;
default :
add.x = rnd() * 4 - 2 ;
add.y = rnd() * 4 - 2 ;
add.z = rnd() * 4 - 2 ;
now = Eadd( now , add ) ;
}
if ( now.x < -8 ) now.x = -8 ;
if ( now.x > 8 ) now.x = 8 ;
if ( now.y < -8 ) now.y = -8 ;
if ( now.y > 8 ) now.y = 8 ;
if ( now.z < -8 ) now.z = -8 ;
if ( now.z > 8 ) now.z = 8 ;
}
return 0 ;
}