Skip to content

OnNpcTarget

AI targetting other BaseEntity. Rust.AI.Gen2 targetting other BaseEntity.

Usage

  • No return behavior

Example Autogenerated

csharp
private void OnNpcTarget(  )
{
    Puts( "OnNpcTarget works!" );
}
csharp
private void OnNpcTarget(  )
{
    Puts( "OnNpcTarget works!" );
}
csharp
private object OnNpcTarget( BaseNpc npc, BaseEntity target )
{
    Puts( "OnNpcTarget works!" );
    return null;
}
csharp
private object OnNpcTarget( BaseEntity npc, BaseEntity target )
{
    Puts( "OnNpcTarget works!" );
    return null;
}

Location

  • AIBrainSenses::GetNearest(System.Collections.Generic.List`1<BaseEntity> entities, float rangeFraction)
  • HumanNPC::GetBestTarget()
  • RustCore::IOnNpcTarget(BaseNpc npc, BaseEntity target)
  • RustCore::IOnNpcTarget(SenseComponent sense, BaseEntity target)
csharp
//---
	}
	float num = float.PositiveInfinity;
	BaseEntity result = null;
	foreach (BaseEntity current in entities)
	{
		if (!(current == null) && current.Health() > 0f && Interface.CallHook("OnNpcTarget", this.owner, current) == null)
		{
			float num2 = Vector3.Distance(current.transform.position, this.owner.transform.position);
			if (num2 <= rangeFraction * this.maxRange && num2 < num)
			{
				result = current;
//---
csharp
//---
{
	BaseEntity result = null;
	float num = -1f;
	foreach (BaseEntity current in this.Brain.Senses.Players)
	{
		if (!(current == null) && current.Health() > 0f && Interface.CallHook("OnNpcTarget", this, current) == null)
		{
			float value = Vector3.Distance(current.transform.position, base.transform.position);
			float num2 = 1f - Mathf.InverseLerp(1f, this.Brain.SenseRange, value);
			float value2 = Vector3.Dot((current.transform.position - base.eyes.position).normalized, base.eyes.BodyForward());
			num2 += Mathf.InverseLerp(this.Brain.VisionCone, 1f, value2) / 2f;
//---
csharp
private object IOnNpcTarget(BaseNpc npc, BaseEntity target)
{
	if (Interface.CallHook("OnNpcTarget", npc, target) != null)
	{
		npc.SetFact(BaseNpc.Facts.HasEnemy, 0);
		npc.SetFact(BaseNpc.Facts.EnemyRange, 3);
		npc.SetFact(BaseNpc.Facts.AfraidRange, 1);

		//TODO: Find replacements of those:
		// npc.AiContext.EnemyPlayer = null;
		// npc.AiContext.LastEnemyPlayerScore = 0f;

		npc.playerTargetDecisionStartTime = 0f;
		return 0f;
	}

	return null;
}
csharp
private object IOnNpcTarget(SenseComponent sense, BaseEntity target)
{
	if (!sense || !target)
	{
		return null;
	}
	BaseEntity baseEntity = sense.baseEntity;
	if (!baseEntity)
	{
		return null;
	}
	if (Interface.CallHook("OnNpcTarget", baseEntity, target) != null)
	{
		return false;
	}
	return null;
}

Released under the MIT License.