Npc New.cpp: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
No edit summary
(Provided the correct npc_New.cpp from "https://web.archive.org/web/20061022062326/http://developer.valvesoftware.com/wiki/Npc_New.cpp")
Line 1: Line 1:
Source file npc_new.cpp is also known as monster_dummy.cpp; found in newly created mod folders and used as a template for creating a new npc from scratch.


 
== npc_New.cpp ==
== monster_dummy.cpp ==


<pre>//========= Copyright © 1996-2005, Valve Corporation, All rights reserved. ============//
<pre>//========= Copyright © 1996-2005, Valve Corporation, All rights reserved. ============//
Line 9: Line 7:
// NPC and add the copy to the build.
// NPC and add the copy to the build.
//
//
// Leave this file in the build until we ship! Allowing
// Replace occurrences of CNPC_New with the new NPC's
// this file to be rebuilt with the rest of the game ensures
// classname. Don't forget the lower-case occurrence in  
// that it stays up to date with the rest of the NPC code.
//
// Replace occurances of CNewNPC with the new NPC's
// classname. Don't forget the lower-case occurance in  
// LINK_ENTITY_TO_CLASS()
// LINK_ENTITY_TO_CLASS()
//
//
Line 21: Line 15:
//
//
// You're making a character based on CAI_BaseNPC. If this  
// You're making a character based on CAI_BaseNPC. If this  
// is not true, make sure you replace all occurances
// is not true, make sure you replace all occurrences
// of 'CAI_BaseNPC' in this file with the appropriate  
// of 'CAI_BaseNPC' in this file with the appropriate  
// parent class.
// parent class.
Line 40: Line 34:
#include "ai_basenpc.h"
#include "ai_basenpc.h"
#include "engine/IEngineSound.h"
#include "engine/IEngineSound.h"
#define NPC_NEW_MODEL "models/mymodel.mdl"


// memdbgon must be the last include file in a .cpp file!!!
// memdbgon must be the last include file in a .cpp file!!!
Line 45: Line 41:


//=========================================================
//=========================================================
// Private activities
// Private animevents
//=========================================================
//=========================================================
int ACT_MYCUSTOMACTIVITY = -1;
int NEWNPC_AE_ANIMEVENT;
int NEWNPC_AE_ANIMEVENT2;


//=========================================================
//=========================================================
// Custom schedules
// Private activities
//=========================================================
//=========================================================
enum
Activity ACT_NEWNPC_ACTIVITY;
{
Activity ACT_NEWNPC_ACTIVITY2;
SCHED_MYCUSTOMSCHEDULE = LAST_SHARED_SCHEDULE,
};


//=========================================================
//=========================================================
// Custom tasks
// Shared interaction
//=========================================================
//=========================================================
enum
int g_interactionExample = 0; // REMEMBER TO ADD THIS TO AI_Interactions.h
{
int g_interactionExample2 = 0; // REMEMBER TO ADD THIS TO AI_Interactions.h
TASK_MYCUSTOMTASK = LAST_SHARED_TASK,
};
 


//=========================================================
// -----------------------------------------------
// Custom Conditions
// > Squad slots
//=========================================================
// -----------------------------------------------
enum  
enum SquadSlot_T
{
{
COND_MYCUSTOMCONDITION = LAST_SHARED_CONDITION,
SQUAD_SLOT_EXAMPLE = LAST_SHARED_SQUADSLOT,
SQUAD_SLOT_EXAMPLE2,
};
};


//=========================================================
//=========================================================
//=========================================================
//=========================================================
class CNewNPC : public CAI_BaseNPC
class CNPC_New : public CAI_BaseNPC
{
{
DECLARE_CLASS( CNewNPC, CAI_BaseNPC );
DECLARE_CLASS( CNPC_New, CAI_BaseNPC );
DECLARE_DATADESC();
DEFINE_CUSTOM_AI;


public:
public:
Line 85: Line 79:
void Spawn( void );
void Spawn( void );
Class_T Classify( void );
Class_T Classify( void );
private:
enum
{
SCHED_NEWNPC_SCHEDULE = BaseClass::NEXT_SCHEDULE,
SCHED_NEWNPC_SCHEDULE2,
NEXT_SCHEDULE
};


DECLARE_DATADESC();
enum
 
{
// This is a dummy field. In order to provide save/restore
TASK_NEWNPC_TASK = BaseClass::NEXT_TASK,
// code in this file, we must have at least one field
TASK_NEWNPC_TASK2,
// for the code to operate on. Delete this field when
NEXT_TASK
// you are ready to do your own save/restore for this
};
// character.
int m_iDeleteThisField;


DEFINE_CUSTOM_AI;
enum
{
COND_NEWNPC_CONDITION = BaseClass::NEXT_CONDITION,
COND_NEWNPC_CONDITION2,
NEXT_CONDITION
};
};
};


LINK_ENTITY_TO_CLASS( npc_newnpc, CNewNPC );
LINK_ENTITY_TO_CLASS( npc_newnpc, CNPC_New );
IMPLEMENT_CUSTOM_AI( npc_citizen,CNewNPC );
 


//---------------------------------------------------------
//---------------------------------------------------------
// Save/Restore
// Save/Restore
//---------------------------------------------------------
//---------------------------------------------------------
BEGIN_DATADESC( CNewNPC )
BEGIN_DATADESC( CNPC_New )
 
DEFINE_FIELD( m_iDeleteThisField, FIELD_INTEGER ),


END_DATADESC()
END_DATADESC()


//-----------------------------------------------------------------------------
AI_BEGIN_CUSTOM_NPC(CNPC_New)
// Purpose: Initialize the custom schedules
// Input  :
// Output :
//-----------------------------------------------------------------------------
void CNewNPC::InitCustomSchedules(void)  
{
INIT_CUSTOM_AI(CNewNPC);
 
ADD_CUSTOM_TASK(CNewNPC, TASK_MYCUSTOMTASK);


ADD_CUSTOM_SCHEDULE(CNewNPC, SCHED_MYCUSTOMSCHEDULE);
AI_END_CUSTOM_NPC()
 
ADD_CUSTOM_ACTIVITY(CNewNPC, ACT_MYCUSTOMACTIVITY);
 
ADD_CUSTOM_CONDITION(CNewNPC, COND_MYCUSTOMCONDITION);
}


//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
Line 134: Line 120:
//
//
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void CNewNPC::Precache( void )
void CNPC_New::Precache( void )
{
{
PrecacheModel( "models/mymodel.mdl" );
PrecacheModel( NPC_NEW_MODEL );


BaseClass::Precache();
BaseClass::Precache();
Line 147: Line 133:
//
//
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void CNewNPC::Spawn( void )
void CNPC_New::Spawn( void )
{
{
Precache();
Precache();


SetModel( "models/mymodel.mdl" );
SetModel( NPC_NEW_MODEL );
SetHullType(HULL_HUMAN);
SetHullType(HULL_HUMAN);
SetHullSizeNormal();
SetHullSizeNormal();
Line 159: Line 145:
SetMoveType( MOVETYPE_STEP );
SetMoveType( MOVETYPE_STEP );
SetBloodColor( BLOOD_COLOR_RED );
SetBloodColor( BLOOD_COLOR_RED );
m_iHealth = 20;
m_iHealth = 20;
m_flFieldOfView = 0.5;
m_flFieldOfView = 0.5; // indicates the width of this NPC's forward view cone ( as a dotproduct result )
m_NPCState = NPC_STATE_NONE;
m_NPCState = NPC_STATE_NONE;


CapabilitiesClear();
CapabilitiesClear();
//CapabilitiesAdd( bits_CAP_NONE );
CapabilitiesAdd( bits_CAP_TURN_HEAD | bits_CAP_MOVE_GROUND );


NPCInit();
NPCInit();
Line 176: Line 162:
// Output :  
// Output :  
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
Class_T CNewNPC::Classify( void )
Class_T CNPC_New::Classify( void )
{
{
return CLASS_NONE;
return CLASS_NONE;
}</pre>
}</pre>
--[[User:M4pster|M4pster]] 22:08, 1 February 2013 (PST)

Revision as of 08:13, 22 June 2021

npc_New.cpp

//========= Copyright © 1996-2005, Valve Corporation, All rights reserved. ============//
// This is a skeleton file for use when creating a new 
// NPC. Copy and rename this file for the new
// NPC and add the copy to the build.
//
// Replace occurrences of CNPC_New with the new NPC's
// classname. Don't forget the lower-case occurrence in 
// LINK_ENTITY_TO_CLASS()
//
//
// ASSUMPTIONS MADE:
//
// You're making a character based on CAI_BaseNPC. If this 
// is not true, make sure you replace all occurrences
// of 'CAI_BaseNPC' in this file with the appropriate 
// parent class.
//
// You're making a human-sized NPC that walks.
//
//=============================================================================//
#include "cbase.h"
#include "ai_default.h"
#include "ai_task.h"
#include "ai_schedule.h"
#include "ai_hull.h"
#include "soundent.h"
#include "game.h"
#include "npcevent.h"
#include "entitylist.h"
#include "activitylist.h"
#include "ai_basenpc.h"
#include "engine/IEngineSound.h"

#define NPC_NEW_MODEL "models/mymodel.mdl"

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"

//=========================================================
// Private animevents
//=========================================================
int NEWNPC_AE_ANIMEVENT;
int NEWNPC_AE_ANIMEVENT2;

//=========================================================
// Private activities
//=========================================================
Activity ACT_NEWNPC_ACTIVITY;
Activity ACT_NEWNPC_ACTIVITY2;

//=========================================================
// Shared interaction
//=========================================================
int g_interactionExample = 0; // REMEMBER TO ADD THIS TO AI_Interactions.h
int g_interactionExample2 = 0; // REMEMBER TO ADD THIS TO AI_Interactions.h

// -----------------------------------------------
//	> Squad slots
// -----------------------------------------------
enum SquadSlot_T
{	
	SQUAD_SLOT_EXAMPLE = LAST_SHARED_SQUADSLOT,
	SQUAD_SLOT_EXAMPLE2,
};

//=========================================================
//=========================================================
class CNPC_New : public CAI_BaseNPC
{
	DECLARE_CLASS( CNPC_New, CAI_BaseNPC );
	DECLARE_DATADESC();
	DEFINE_CUSTOM_AI;

public:
	void	Precache( void );
	void	Spawn( void );
	Class_T Classify( void );
private:
	enum
	{
		SCHED_NEWNPC_SCHEDULE = BaseClass::NEXT_SCHEDULE,
		SCHED_NEWNPC_SCHEDULE2,
		NEXT_SCHEDULE
	};

	enum 
	{
		TASK_NEWNPC_TASK = BaseClass::NEXT_TASK,
		TASK_NEWNPC_TASK2,
		NEXT_TASK
	};

	enum 
	{
		COND_NEWNPC_CONDITION = BaseClass::NEXT_CONDITION,
		COND_NEWNPC_CONDITION2,
		NEXT_CONDITION
	};
};

LINK_ENTITY_TO_CLASS( npc_newnpc, CNPC_New );

//---------------------------------------------------------
// Save/Restore
//---------------------------------------------------------
BEGIN_DATADESC( CNPC_New )

END_DATADESC()

AI_BEGIN_CUSTOM_NPC(CNPC_New)

AI_END_CUSTOM_NPC()

//-----------------------------------------------------------------------------
// Purpose: 
//
//
//-----------------------------------------------------------------------------
void CNPC_New::Precache( void )
{
	PrecacheModel( NPC_NEW_MODEL );

	BaseClass::Precache();
}


//-----------------------------------------------------------------------------
// Purpose: 
//
//
//-----------------------------------------------------------------------------
void CNPC_New::Spawn( void )
{
	Precache();

	SetModel( NPC_NEW_MODEL );
	SetHullType(HULL_HUMAN);
	SetHullSizeNormal();

	SetSolid( SOLID_BBOX );
	AddSolidFlags( FSOLID_NOT_STANDABLE );
	SetMoveType( MOVETYPE_STEP );
	SetBloodColor( BLOOD_COLOR_RED );
	m_iHealth	= 20;
	m_flFieldOfView	= 0.5; // indicates the width of this NPC's forward view cone ( as a dotproduct result )
	m_NPCState	= NPC_STATE_NONE;

	CapabilitiesClear();
	CapabilitiesAdd( bits_CAP_TURN_HEAD | bits_CAP_MOVE_GROUND );

	NPCInit();
}


//-----------------------------------------------------------------------------
// Purpose: 
//
//
// Output : 
//-----------------------------------------------------------------------------
Class_T	CNPC_New::Classify( void )
{
	return	CLASS_NONE;
}