Path Finding

From Spiki

Jump to: navigation, search

Contents

Consol Hax

ai_nodes
Kald den to gange for at se noders ID'er

Kilde Trix

A* algoritmen

ai_pathfinding.cpp @ line: 1473

Udskriv ID'et af den node der er tættest på der hvor man peger hen

void PointingPlayer( void ) 
{
	CBasePlayer* pPlayer = UTIL_GetCommandClient();
	trace_t tr;
	Vector forward, v;
	pPlayer->EyeVectors( &forward );
	AI_TraceLine(pPlayer->EyePosition(),
		pPlayer->EyePosition() + forward * MAX_TRACE_LENGTH,MASK_NPCSOLID, 
		pPlayer, COLLISION_GROUP_NONE, &tr );

	if(tr.fraction < 1.0)
	{
		v = tr.endpos;
		CAI_Network*		ai = g_pBigAINet; /// <--- Global variabel der får fat i info_node netværket!!!!!!!!
		
		int i = ai->NearestNodeToPoint(v, false);
		Msg("Player pointing: (%.1f, %.1f, %.1f), Nearest Node is: %i\n", v.x, v.y, v.z, i);
	}
}

static ConCommand pointat("pointat", PointingPlayer, "Gap", FCVAR_CHEAT);



sdk_firstnpc.cpp

Jeg har fået en npc jeg selv har kogt til at vade over til en entity by name. Så du på npc'en sætter navnet på en entity og så kan du kalde en funktion gennem hammer der får den til at vade over til entityen.

Jeg tog udgangspunkt i "npc_go" kommandoen (Find in all files "npc_go").

Det sjove i NPC'en er InputGo funktionen.


sdk_firstnpc.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.
//
// Leave this file in the build until we ship! Allowing 
// this file to be rebuilt with the rest of the game ensures
// that it stays up to date with the rest of the NPC code.
//
// Replace occurances of CFirstNPC with the new NPC's
// classname. Don't forget the lower-case occurance 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 occurances
// 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 "npc_antlion.h"
#include "hl2_shareddefs.h"
#include "engine/IEngineSound.h"
#include "physics_prop_ragdoll.h"

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


//int AE_ANTLION_MELEE1_SOUND;
//int AE_ANTLION_MELEE2_SOUND;

//=========================================================
// Private activities
//=========================================================
int	ACT_FIRSTNPCACTIVITY = -1;

//=========================================================
// Custom schedules
//=========================================================
enum
{
	SCHED_MYCUSTOMSCHEDULE = LAST_SHARED_SCHEDULE,
};

//=========================================================
// Custom tasks
//=========================================================
enum 
{
	TASK_MYCUSTOMTASK = LAST_SHARED_TASK,
};


//=========================================================
// Custom Conditions
//=========================================================
enum 
{
	COND_MYCUSTOMCONDITION = LAST_SHARED_CONDITION,
};


//=========================================================
//=========================================================
class CFirstNPC : public CAI_BaseNPC
{
	DECLARE_CLASS( CFirstNPC, CAI_BaseNPC );

public:
	void	Precache( void );
	void	Spawn( void );
	Class_T Classify( void );

	DECLARE_DATADESC();

	HSOUNDSCRIPTHANDLE	m_hFootstep;

	int SelectSchedule( );


	// This is a dummy field. In order to provide save/restore
	// code in this file, we must have at least one field
	// for the code to operate on. Delete this field when
	// you are ready to do your own save/restore for this
	// character.
	int		m_iDeleteThisField;
	void InputGo( inputdata_t &inputData );
	string_t m_szTarget;

	DEFINE_CUSTOM_AI;
};

LINK_ENTITY_TO_CLASS( npc_firstnpc, CFirstNPC );
IMPLEMENT_CUSTOM_AI( npc_citizen, CFirstNPC );


//---------------------------------------------------------
// Save/Restore
//---------------------------------------------------------
BEGIN_DATADESC( CFirstNPC )

	DEFINE_FIELD( m_iDeleteThisField, FIELD_INTEGER ),
	DEFINE_FIELD( m_szTarget, FIELD_STRING ),
	DEFINE_KEYFIELD( m_szTarget, FIELD_STRING, "moveTarget" ),
	// Links our input name from Hammer to our input member function
	DEFINE_INPUTFUNC( FIELD_VOID, "Go", InputGo ),

END_DATADESC()


//-----------------------------------------------------------------------------
// Purpose: Initialize the custom schedules
// Input  :
// Output :
//-----------------------------------------------------------------------------
void CFirstNPC::InitCustomSchedules(void) 
{
	INIT_CUSTOM_AI(CFirstNPC);

	ADD_CUSTOM_TASK(CFirstNPC,		TASK_MYCUSTOMTASK);

	ADD_CUSTOM_SCHEDULE(CFirstNPC,	SCHED_MYCUSTOMSCHEDULE);

	ADD_CUSTOM_ACTIVITY(CFirstNPC,	ACT_FIRSTNPCACTIVITY);

	ADD_CUSTOM_CONDITION(CFirstNPC,	COND_MYCUSTOMCONDITION);
	//ADD_CUSTOM_ANIMEVENT(CFirstNPC,	 AE_ANTLION_WALK_FOOTSTEP );

}

//-----------------------------------------------------------------------------
// Purpose: 
//
//
//-----------------------------------------------------------------------------
void CFirstNPC::Precache( void )
{
	PrecacheModel( "models/antlion.mdl" );
	m_hFootstep = PrecacheScriptSound( "NPC_Antlion.Footstep" );

	BaseClass::Precache();
}


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

	SetModel( "models/antlion.mdl" );
	SetHullType(HULL_MEDIUM);
	SetHullSizeNormal();
	SetDefaultEyeOffset();
	
	SetNavType( NAV_GROUND );

	SetSolid( SOLID_BBOX );
	AddSolidFlags( FSOLID_NOT_STANDABLE );
	SetMoveType( MOVETYPE_STEP );
	SetBloodColor( BLOOD_COLOR_RED );
	
	SetCollisionGroup( HL2COLLISION_GROUP_ANTLION );

	m_iHealth			= 20;
	m_flFieldOfView		= 0.5;
	m_NPCState			= NPC_STATE_NONE;

	CapabilitiesClear();
	CapabilitiesAdd( bits_CAP_MOVE_GROUND | bits_CAP_INNATE_MELEE_ATTACK1 | bits_CAP_INNATE_MELEE_ATTACK2 );

	m_nSkin = 0;

	//CapabilitiesAdd( bits_CAP_NONE );

	NPCInit();
}


//-----------------------------------------------------------------------------
// Purpose: 
//
//
// Output : 
//-----------------------------------------------------------------------------
Class_T	CFirstNPC::Classify( void )
{
	return	CLASS_ANTLION;
}

//-----------------------------------------------------------------------------
// Purpose: Handle a tick input from another entity
//-----------------------------------------------------------------------------
void CFirstNPC::InputGo( inputdata_t &inputData )
{
	Msg("InputGo: %s\n", m_szTarget );

	CBaseEntity *pNewEntity = NULL;

	pNewEntity = gEntList.FindEntityByName(pNewEntity, m_szTarget);


	if(pNewEntity != NULL) {

		Vector v = pNewEntity->GetAbsOrigin();
		
		TranslateNavGoal( pNewEntity, v );

		m_vecLastPosition = v;

		SetSchedule( SCHED_FORCED_GO );

		//m_flMoveWaitFinished = gpGlobals->curtime;
		Msg("InputGo: Wee (%.1f, %.1f, %.1f) \n", v.x, v.y, v.z );
	}
	else 
	{
		Msg("InputGo: null\n" );
	}
}

Der skal self også en entry i FGD filen:

@NPCClass base(BaseNPC) studio("models/antlion.mdl") = npc_firstnpc : "My First NPC"
[
	moveTarget(target_destination) : "MoveTarget"
	input Go(void) : "Makes the ant go to moveTarget"
]
Personal tools