Skip to content
Rijam edited this page Aug 2, 2024 · 3 revisions

When using vanilla Town NPC AI, NPC.ai[] and NPC.localAI[] are used to keep track of different things. Here are what some of them mean and a few little things we can do with them. This is not a complete list.

public override void PostAI() {
	// NPC.ai[0] = current action
	//  0 == no action
	//  1 == walking
	//  2 == 
	//  3-4 == chatting
	//  5 == sitting
	//  7 == Talking to player
	//  8 == Decide to run from enemy? Immediately set to 1.
	//  9 == Go sit in a chair?
	//  10 == attacking
	//  12-15 == attacking related
	//  16 == player rock paper scissors
	//  19 == Talking to player (responding to the player)
	//  24 == magic attack aura
	//  25 == change to a different action

	// NPC.ai[1] = time before changing actions
	//  299 when talking to a player

	// NPC.ai[2] = The player index that the Town NPC is speaking to? (Player.whoAmI) 
	//  Reset to 0 most of the time

	// NPC.ai[3] =
	//  Used by the Old Man

	// NPC.localAI[0] is open
	//  This is used by the Mechanic to know to stay still while her boomerang is returning to her.
	// NPC.localAI[1] = Attacking cooldown
	//  0 most of the time
	// NPC.localAI[2] =
	//  1 most of the time
	// NPC.localAI[3] = Attacking animation time
	//  -1 or 0 most of the time when not attacking
	//  -99 when talking to a player

	// If just started to attack.
	if (NPC.ai[0] == 10 && NPC.localAI[3] == 1) {
		EmoteBubble.NewBubble(EmoteID.ItemSword, new WorldUIAnchor(NPC), 120); // Display an emote above their head.
	}
	// Standing still about to do something else and very hurt.
	if (NPC.ai[0] == 0 && NPC.ai[1] == 10 && NPC.life < NPC.lifeMax * 0.25f) {
		EmoteBubble.NewBubble(EmoteID.ItemTombstone, new WorldUIAnchor(NPC), 120); // Display an emote above their head.
	}
	// If talking to the player.
	if (NPC.ai[0] == 7 || NPC.ai[0] == 19) {
		NPC.AddBuff(BuffID.Lovestruck, 2); // Give them a buff.
	}
}