Deepflame AI Tutorial #1

 

Hello and welcome to my AI tutorial. I'm going to make a series out of it, constantly making the AI a bit more advanced. This tut will cover a new monster who can sit in your map, and run away if you shoot at it. Taking cover is pretty usefull =)

Ok, create a new .cpp file in your workspace, and name it monster.cpp and paste this in it:

//=========================================================
// monster.cpp - a new monster
//=========================================================

#include	"extdll.h"
#include	"util.h"
#include	"cbase.h"
#include	"monsters.h"
#include	"schedule.h"

//=====================
// Monsters Class Definition
//=====================

class CMonster : public CBaseMonster
{
public:
	void Spawn( void );
	void Precache( void );
	void SetYawSpeed ( void );

	int Classify ( void );
};

LINK_ENTITY_TO_CLASS( monster_monster, CMonster );

//=========================================================
// Classify - indicates this monster's place in the 
// relationship table.
//=========================================================

int	CMonster :: Classify ( void )
{
	return	CLASS_PLAYER_ALLY; // This is a friendly monster
}

//=========================================================
// SetYawSpeed - allows each sequence to have a different
// turn rate associated with it.
//=========================================================

void CMonster :: SetYawSpeed ( void )
{
	pev->yaw_speed = 90; // Turning with 90
}

//=========================================================
// Spawn
//=========================================================

void CMonster :: Spawn()
{
	Precache( );

	SET_MODEL(ENT(pev), "models/bullsquid.mdl"); // We need a model anyway

   	UTIL_SetSize(pev, Vector(-12, -12, 0), Vector(12, 12, 24)); // For it's hitbox

	pev->solid			= SOLID_SLIDEBOX;
	pev->movetype		= MOVETYPE_STEP;
	m_bloodColor		= BLOOD_COLOR_GREEN;
	pev->effects		= 0;
	pev->health			= 100; // 100 health is enough
	pev->view_ofs		= Vector ( 0, 0, 20 );// position of the eyes relative to monster's origin.
	m_flFieldOfView		= 0.5;// indicates the width of this monster's forward view cone ( as a dotproduct result )
	m_MonsterState		= MONSTERSTATE_NONE;

	MonsterInit(); // Starts the monsters AI
}

//=========================================================
// Precache - precaches all resources this monster needs
//=========================================================

void CMonster :: Precache()
{
	PRECACHE_MODEL("models/bullsquid.mdl"); //Loads the model in the game
}
That's it, now you have a monster sitting in your map, being idle unless you shoot it.
It'll run to info_node entities to take cover from the big bad you.

For any questions/bugs mail me.
- Deepflame - tox.rwld@rendo.dekooi.nl