RustTip

OnNpcTarget

OxideNPC
object OnNpcTarget(BaseEntity owner, BaseEntity baseEntity)
object OnNpcTarget(HumanNPC humanNPC, BaseEntity baseEntity)
Works — the game method in this build is the one the hook was written for.

Parameters

owner
BaseEntity
baseEntity
BaseEntity

Return value

objectReturn non-null to stop the game method.

Example

object — you can affect the game: return a value to change or cancel the action.

private object OnNpcTarget(BaseEntity owner, BaseEntity baseEntity)
{
    Puts("OnNpcTarget");

    // change nothing — the game continues as usual
    return null;

    // to affect the game:
    // return false; // non-null cancels the game action
}

Works in both Oxide and Carbon: Carbon loads Oxide plugins as is.

Where it is called

Oxide core (Oxide.Rust)
RustCore::IOnNpcTarget(BaseNpc npc, BaseEntity target)

Oxide core code — the hook is called from here

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;
// …

Injection point 1

Rust.opj (Oxide)
BaseEntity AIBrainSenses.GetNearest(List<BaseEntity>, float)

Passes: BaseEntity owner, BaseEntity baseEntity

WorksReturnBehavior: ExitWhenNonNullAssembly-CSharp.dll

Where it is called (this branch's game code)

// …
	return null;
}
float num = float.PositiveInfinity;
BaseEntity result = null;
foreach (BaseEntity entity in entities)
{
	if (!(entity == null) && !(entity.Health() <= 0f))
	{
		if (Interface.CallHook("OnNpcTarget", this.owner, baseEntity) == null)
		{
			return;
		}
		float num2 = Vector3.Distance(entity.transform.position, owner.transform.position);
		if (num2 <= rangeFraction * maxRange && num2 < num)
		{
			result = entity;
		}
// …

Injection point 2

Rust.opj (Oxide)
BaseEntity HumanNPC.GetBestTarget()

Passes: HumanNPC humanNPC, BaseEntity baseEntity

WorksReturnBehavior: ExitWhenNonNullAssembly-CSharp.dll

Where it is called (this branch's game code)

public BaseEntity GetBestTarget()
{
	BaseEntity result = null;
	float num = -1f;
	foreach (BaseEntity player in Brain.Senses.Players)
	{
		if (!(player == null) && !(player.Health() <= 0f))
		{
			if (Interface.CallHook("OnNpcTarget", this, baseEntity) == null)
			{
				return;
			}
			float value = Vector3.Distance(player.transform.position, transform.position);
			float num2 = 1f - Mathf.InverseLerp(1f, Brain.SenseRange, value);
			float value2 = Vector3.Dot((player.transform.position - eyes.position).normalized, eyes.BodyForward());
			num2 += Mathf.InverseLerp(Brain.VisionCone, 1f, value2) / 2f;
			num2 += (Brain.Senses.Memory.IsLOS(player) ? 2f : 0f);
// …

Sources: Rust.opj from OxideMod/Oxide.Rust (MIT) and hook metadata of the Carbon documentation (MIT). Method code is decompiled from this branch's server.