well, you may use my own script. it was my first try to write an AI-script. i copied it from my old project and set comments. I dont know how experienced you are in programming c-script in general, so just ask if you have a question about this^^
the action guard will let the npc turn towards the player if he steps into his field of sight, and stop before him, attack until the player runs out of sigth or one of them dies. i also included some effects, like a random choice out of 6 different deathscreams, so the guards arent all the same. you must include 6 sounds for this or erase the part of the script about the deathscreams.
in addition to this, a music will start playing at low volume when the npc starts following you and become louder; if the guard dies the music decreases its volume and stops.
but before you can use this script, you will have to define everything, like variables, the skills for speed, health and damage and the sounds. i defined all this in another script and didnt want to do the work to write this again just for posting it here. the script should work fine like it is posted (hopefully), after you defined everything.
if a project using this AI goes public, i want to know and be credited^^
function faceme()
{
my.savepan=int(my.pan);
//save the direction i am looking for later purposes
while(my.health>0)//as long as i live
{
if(vec_dist(player.x,my.x) < my.sight)
//if the player enters the region i can sense
{
vec_set(temp,player.x);
vec_sub(temp,my.x);
vec_to_angle(my.pan,temp);
//these 3 lines rotate me until i look at the player
my.tilt = 0;//i dont look up and down
followme();//NOW i start walking
if(msc==null)
//musicpointer; if no music plays, do:
{
battlevol=10;
//volume of music=10%
media_play("battle.mid",null,battlevol);//play a battlemusic
msc=media_handle;//set the handle to "battle.mid"
}
if(battlevol<100)
{
battlevol+=1;
//become louder as the enemy comes closer!
}
}else
{//if the player has run out of my sight (coward...)
if(my.pan>my.savepan+1)
{
my.pan-=2;
}else
{
if(my.pan<my.savepan-1)
{
my.pan+=2;
}
//return me to the direction i had faced before following the player
}
}
wait(1);
}
}
function followme()
{
if(vec_dist(player.x,my.x) > 70)
{
//70 is the distance where the npc stops walking!
if(my.skill51<100)
{//walkanimation!
c_move(me,vector(my.speed,0,0),nullvector,ignore_passable | ignore_sprites);
//now i walk forward
my.skill51 += 3*time_step;
if (my.skill51 > 100)
{
my.skill51 -= 100;
}
ent_animate(me,"walk",my.skill51,anm_cycle);
}
}else
{
if(c_scan(my.x,my.pan,vector(100,0,100),use_polygon|ignore_me)>0)
{//if the npc looks at the player
if(you.health>0)//if the player lives
{
if(my.skill52<100)
{//attackanimation!
my.skill52 += 10*time_step;
ent_animate(me,"attack",my.skill52,null);
sleep(0.1);
}else
{
ent_playsound(me,swordsound,1000);
you.health=max((you.health-my.totaldmg),0);//you loose health or die
my.skill52=0;//reset the animation to the start
sleep(2);
}
}
}
}
}
action guard
{
if(my.sight==0)
{
my.sight=400;
}
if(my.health==0)
{
my.health=50;
}
if(my.health_max==0)
{
my.health_max=50;
}
if(my.speed==0)
{
my.speed=10;
}
if(my.damage==0)
{
my.damage=5;
}
my.enable_block=on;
my.enable_entity=on;
my.deathscream=int(random(6));//randomly choose out of 6 screams!
faceme();
while(me!=null)//as long as i exist
{
if(my.health>0)//as long as i live
{
wait(1);
}else
{
if(my.deathscream==0)
{
ent_playsound(me,scream1,500);
}else
{
if(my.deathscream==1)
{
ent_playsound(me,scream2,500);
}else
{
if(my.deathscream==2)
{
ent_playsound(me,scream3,500);
}else
{
if(my.deathscream==3)
{
ent_playsound(me,scream4,500);
}else
{
if(my.deathscream==4)
{
ent_playsound(me,scream5,500);
}else
{
if(my.deathscream==5)
{
ent_playsound(me,scream6,500);
}
}
}
}
}
}
my.passable=on;
my.skill50=0;
while(my.skill50<100)
{//deathanimation!
my.skill50 += 10*time_step;
ent_animate(me,"death",my.skill50,null);
sleep(0.05);
}
while(battlevol>0)
{
battlevol-=5;
sleep(0.1);
//if the enemy is dead, slowly stop the battlemusic
}
media_stop(msc);//stop the music with the pointer msc ("battle.mid")
msc=null;//so we dont have a music playing
sleep(5);
my.transparent=on;
my.alpha=100;
while(my.alpha>0)
{//let me disappear into the shadows
my.alpha-=1;
sleep(0.1);
}
ent_remove(me);
}
wait(1);
}
}
i used the animated version of the guard.mdl the gamestudio offers, so the animations are specially prepared for this modell.
i hope it works as claimed^^