Terraria ModLoader  0.11.7.8
A mod to make and play Terraria mods
GlobalNPC.cs
Go to the documentation of this file.
1 using Microsoft.Xna.Framework;
2 using Microsoft.Xna.Framework.Graphics;
3 using System;
4 using System.Collections.Generic;
5 
6 namespace Terraria.ModLoader
7 {
11  public class GlobalNPC
12  {
16  public Mod mod {
17  get;
18  internal set;
19  }
20 
24  public string Name {
25  get;
26  internal set;
27  }
28 
29  internal int index;
30  internal int instanceIndex;
31 
37  public virtual bool Autoload(ref string name) {
38  return mod.Properties.Autoload;
39  }
40 
46  public virtual bool InstancePerEntity => false;
47 
48  public GlobalNPC Instance(NPC npc) => InstancePerEntity ? npc.globalNPCs[instanceIndex] : this;
49 
54  public virtual bool CloneNewInstances => false;
55 
61  public virtual GlobalNPC Clone() => (GlobalNPC)MemberwiseClone();
62 
69  public virtual GlobalNPC NewInstance(NPC npc) {
70  if (CloneNewInstances) {
71  return Clone();
72  }
73  GlobalNPC copy = (GlobalNPC)Activator.CreateInstance(GetType());
74  copy.mod = mod;
75  copy.Name = Name;
76  copy.index = index;
77  copy.instanceIndex = instanceIndex;
78  return copy;
79  }
80 
85  public virtual void SetDefaults(NPC npc) {
86  }
87 
94  public virtual void ScaleExpertStats(NPC npc, int numPlayers, float bossLifeScale) {
95  }
96 
101  public virtual void ResetEffects(NPC npc) {
102  }
103 
109  public virtual bool PreAI(NPC npc) {
110  return true;
111  }
112 
117  public virtual void AI(NPC npc) {
118  }
119 
124  public virtual void PostAI(NPC npc) {
125  }
126 
132  public virtual void FindFrame(NPC npc, int frameHeight) {
133  }
134 
141  public virtual void HitEffect(NPC npc, int hitDirection, double damage) {
142  }
143 
149  public virtual void UpdateLifeRegen(NPC npc, ref int damage) {
150  }
151 
157  public virtual bool CheckActive(NPC npc) {
158  return true;
159  }
160 
166  public virtual bool CheckDead(NPC npc) {
167  return true;
168  }
169 
174  public virtual bool SpecialNPCLoot(NPC npc) {
175  return false;
176  }
177 
183  public virtual bool PreNPCLoot(NPC npc) {
184  return true;
185  }
186 
191  public virtual void NPCLoot(NPC npc) {
192  }
193 
200  public virtual void OnCatchNPC(NPC npc, Player player, Item item) {
201  }
202 
210  public virtual bool CanHitPlayer(NPC npc, Player target, ref int cooldownSlot) {
211  return true;
212  }
213 
221  public virtual void ModifyHitPlayer(NPC npc, Player target, ref int damage, ref bool crit) {
222  }
223 
231  public virtual void OnHitPlayer(NPC npc, Player target, int damage, bool crit) {
232  }
233 
240  public virtual bool? CanHitNPC(NPC npc, NPC target) {
241  return null;
242  }
243 
252  public virtual void ModifyHitNPC(NPC npc, NPC target, ref int damage, ref float knockback, ref bool crit) {
253  }
254 
263  public virtual void OnHitNPC(NPC npc, NPC target, int damage, float knockback, bool crit) {
264  }
265 
273  public virtual bool? CanBeHitByItem(NPC npc, Player player, Item item) {
274  return null;
275  }
276 
286  public virtual void ModifyHitByItem(NPC npc, Player player, Item item, ref int damage, ref float knockback, ref bool crit) {
287  }
288 
298  public virtual void OnHitByItem(NPC npc, Player player, Item item, int damage, float knockback, bool crit) {
299  }
300 
307  public virtual bool? CanBeHitByProjectile(NPC npc, Projectile projectile) {
308  return null;
309  }
310 
320  public virtual void ModifyHitByProjectile(NPC npc, Projectile projectile, ref int damage, ref float knockback, ref bool crit, ref int hitDirection) {
321  }
322 
331  public virtual void OnHitByProjectile(NPC npc, Projectile projectile, int damage, float knockback, bool crit) {
332  }
333 
344  public virtual bool StrikeNPC(NPC npc, ref double damage, int defense, ref float knockback, int hitDirection, ref bool crit) {
345  return true;
346  }
347 
353  public virtual void BossHeadSlot(NPC npc, ref int index) {
354  }
355 
361  public virtual void BossHeadRotation(NPC npc, ref float rotation) {
362  }
363 
369  public virtual void BossHeadSpriteEffects(NPC npc, ref SpriteEffects spriteEffects) {
370  }
371 
378  public virtual Color? GetAlpha(NPC npc, Color drawColor) {
379  return null;
380  }
381 
387  public virtual void DrawEffects(NPC npc, ref Color drawColor) {
388  }
389 
397  public virtual bool PreDraw(NPC npc, SpriteBatch spriteBatch, Color drawColor) {
398  return true;
399  }
400 
407  public virtual void PostDraw(NPC npc, SpriteBatch spriteBatch, Color drawColor) {
408  }
409 
415  public virtual void DrawBehind(NPC npc, int index)
416  {
417  }
418 
427  public virtual bool? DrawHealthBar(NPC npc, byte hbPosition, ref float scale, ref Vector2 position) {
428  return null;
429  }
430 
437  public virtual void EditSpawnRate(Player player, ref int spawnRate, ref int maxSpawns) {
438  }
439 
448  public virtual void EditSpawnRange(Player player, ref int spawnRangeX, ref int spawnRangeY,
449  ref int safeRangeX, ref int safeRangeY) {
450  }
451 
457  public virtual void EditSpawnPool(IDictionary<int, float> pool, NPCSpawnInfo spawnInfo) {
458  }
459 
466  public virtual void SpawnNPC(int npc, int tileX, int tileY) {
467  }
468 
474  public virtual bool? CanChat(NPC npc) {
475  return null;
476  }
477 
483  public virtual void GetChat(NPC npc, ref string chat) {
484  }
485 
492  public virtual bool PreChatButtonClicked(NPC npc, bool firstButton) {
493  return true;
494  }
495 
501  public virtual void OnChatButtonClicked(NPC npc, bool firstButton) {
502  }
503 
510  public virtual void SetupShop(int type, Chest shop, ref int nextSlot) {
511  }
512 
518  public virtual void SetupTravelShop(int[] shop, ref int nextSlot) {
519  }
520 
526  public virtual bool? CanGoToStatue(NPC npc, bool toKingStatue) {
527  return null;
528  }
529 
536  public virtual void OnGoToStatue(NPC npc, bool toKingStatue) {
537  }
538 
544  public virtual void BuffTownNPC(ref float damageMult, ref int defense) {
545  }
546 
553  public virtual void TownNPCAttackStrength(NPC npc, ref int damage, ref float knockback) {
554  }
555 
562  public virtual void TownNPCAttackCooldown(NPC npc, ref int cooldown, ref int randExtraCooldown) {
563  }
564 
571  public virtual void TownNPCAttackProj(NPC npc, ref int projType, ref int attackDelay) {
572  }
573 
581  public virtual void TownNPCAttackProjSpeed(NPC npc, ref float multiplier, ref float gravityCorrection,
582  ref float randomOffset) {
583  }
584 
590  public virtual void TownNPCAttackShoot(NPC npc, ref bool inBetweenShots) {
591  }
592 
598  public virtual void TownNPCAttackMagic(NPC npc, ref float auraLightMultiplier) {
599  }
600 
607  public virtual void TownNPCAttackSwing(NPC npc, ref int itemWidth, ref int itemHeight) {
608  }
609 
617  public virtual void DrawTownAttackGun(NPC npc, ref float scale, ref int item, ref int closeness) {
618  }
619 
628  public virtual void DrawTownAttackSwing(NPC npc, ref Texture2D item, ref int itemSize, ref float scale, ref Vector2 offset) {
629  }
630  }
631 }
virtual bool CanHitPlayer(NPC npc, Player target, ref int cooldownSlot)
Allows you to determine whether an NPC can hit the given player. Return false to block the NPC from h...
Definition: GlobalNPC.cs:210
virtual bool PreNPCLoot(NPC npc)
Allows you to determine whether or not the NPC will drop anything at all. Return false to stop the NP...
Definition: GlobalNPC.cs:183
virtual void OnHitByProjectile(NPC npc, Projectile projectile, int damage, float knockback, bool crit)
Allows you to create special effects when an NPC is hit by a projectile.
Definition: GlobalNPC.cs:331
virtual bool CheckDead(NPC npc)
Whether or not an NPC should be killed when it reaches 0 health. You may program extra effects in thi...
Definition: GlobalNPC.cs:166
virtual void DrawTownAttackSwing(NPC npc, ref Texture2D item, ref int itemSize, ref float scale, ref Vector2 offset)
Allows you to customize how a town NPC&#39;s weapon is drawn when the NPC is swinging it (the NPC must ha...
Definition: GlobalNPC.cs:628
virtual void ModifyHitNPC(NPC npc, NPC target, ref int damage, ref float knockback, ref bool crit)
Allows you to modify the damage, knockback, etc., that an NPC does to a friendly NPC.
Definition: GlobalNPC.cs:252
virtual bool CanChat(NPC npc)
Allows you to determine whether this NPC can talk with the player. Return true to allow talking with ...
Definition: GlobalNPC.cs:474
virtual void SetupShop(int type, Chest shop, ref int nextSlot)
Allows you to add items to an NPC&#39;s shop. The type parameter is the type of the NPC that this shop be...
Definition: GlobalNPC.cs:510
virtual void OnCatchNPC(NPC npc, Player player, Item item)
Allows you to make things happen when an NPC is caught. Ran Serverside.
Definition: GlobalNPC.cs:200
A struct that stores information regarding where an NPC is naturally spawning and the player it is sp...
Definition: NPCSpawnInfo.cs:6
virtual void TownNPCAttackStrength(NPC npc, ref int damage, ref float knockback)
Allows you to determine the damage and knockback of a town NPC&#39;s attack before the damage is scaled...
Definition: GlobalNPC.cs:553
virtual void SpawnNPC(int npc, int tileX, int tileY)
Allows you to customize an NPC (for example, its position or ai array) after it naturally spawns and ...
Definition: GlobalNPC.cs:466
virtual void ResetEffects(NPC npc)
This is where you reset any fields you add to your subclass to their default states. This is necessary in order to reset your fields if they are conditionally set by a tick update but the condition is no longer satisfied.
Definition: GlobalNPC.cs:101
virtual void EditSpawnRange(Player player, ref int spawnRangeX, ref int spawnRangeY, ref int safeRangeX, ref int safeRangeY)
Allows you to modify the range at which NPCs can spawn around the given player. The spawnRanges deter...
Definition: GlobalNPC.cs:448
virtual void SetupTravelShop(int[] shop, ref int nextSlot)
Allows you to add items to the traveling merchant&#39;s shop. Add an item by setting shop[nextSlot] to th...
Definition: GlobalNPC.cs:518
virtual void GetChat(NPC npc, ref string chat)
Allows you to modify the chat message of any NPC that the player can talk to.
Definition: GlobalNPC.cs:483
virtual bool Autoload(ref string name)
Allows you to automatically load a GlobalNPC instead of using Mod.AddGlobalNPC. Return true to allow ...
Definition: GlobalNPC.cs:37
virtual void ScaleExpertStats(NPC npc, int numPlayers, float bossLifeScale)
Allows you to customize an NPC&#39;s stats in expert mode.
Definition: GlobalNPC.cs:94
virtual void UpdateLifeRegen(NPC npc, ref int damage)
Allows you to make the NPC either regenerate health or take damage over time by setting npc...
Definition: GlobalNPC.cs:149
virtual void BossHeadRotation(NPC npc, ref float rotation)
Allows you to customize the rotation of an NPC&#39;s boss head icon on the map.
Definition: GlobalNPC.cs:361
virtual void OnHitNPC(NPC npc, NPC target, int damage, float knockback, bool crit)
Allows you to create special effects when an NPC hits a friendly NPC.
Definition: GlobalNPC.cs:263
virtual void ModifyHitByProjectile(NPC npc, Projectile projectile, ref int damage, ref float knockback, ref bool crit, ref int hitDirection)
Allows you to modify the damage, knockback, etc., that an NPC takes from a projectile.
Definition: GlobalNPC.cs:320
virtual void OnHitByItem(NPC npc, Player player, Item item, int damage, float knockback, bool crit)
Allows you to create special effects when an NPC is hit by a melee weapon.
Definition: GlobalNPC.cs:298
virtual void AI(NPC npc)
Allows you to determine how any NPC behaves. This will only be called if PreAI returns true...
Definition: GlobalNPC.cs:117
virtual void DrawBehind(NPC npc, int index)
When used in conjunction with "npc.hide = true", allows you to specify that this npc should be drawn ...
Definition: GlobalNPC.cs:415
virtual void ModifyHitByItem(NPC npc, Player player, Item item, ref int damage, ref float knockback, ref bool crit)
Allows you to modify the damage, knockback, etc., that an NPC takes from a melee weapon.
Definition: GlobalNPC.cs:286
virtual void TownNPCAttackProjSpeed(NPC npc, ref float multiplier, ref float gravityCorrection, ref float randomOffset)
Allows you to determine the speed at which a town NPC throws a projectile when it attacks...
Definition: GlobalNPC.cs:581
virtual void FindFrame(NPC npc, int frameHeight)
Allows you to modify the frame from an NPC&#39;s texture that is drawn, which is necessary in order to an...
Definition: GlobalNPC.cs:132
virtual bool CanGoToStatue(NPC npc, bool toKingStatue)
Whether this NPC can be telported a King or Queen statue. Return true to allow the NPC to teleport to...
Definition: GlobalNPC.cs:526
virtual void OnHitPlayer(NPC npc, Player target, int damage, bool crit)
Allows you to create special effects when an NPC hits a player (for example, inflicting debuffs)...
Definition: GlobalNPC.cs:231
virtual void PostAI(NPC npc)
Allows you to determine how any NPC behaves. This will be called regardless of what PreAI returns...
Definition: GlobalNPC.cs:124
virtual void DrawEffects(NPC npc, ref Color drawColor)
Allows you to add special visual effects to an NPC (such as creating dust), and modify the color in w...
Definition: GlobalNPC.cs:387
virtual bool CanBeHitByItem(NPC npc, Player player, Item item)
Allows you to determine whether an NPC can be hit by the given melee weapon when swung. Return true to allow hitting the NPC, return false to block hitting the NPC, and return null to use the vanilla code for whether the NPC can be hit. Returns null by default.
Definition: GlobalNPC.cs:273
virtual bool CanBeHitByProjectile(NPC npc, Projectile projectile)
Allows you to determine whether an NPC can be hit by the given projectile. Return true to allow hitti...
Definition: GlobalNPC.cs:307
virtual void SetDefaults(NPC npc)
Allows you to set the properties of any and every NPC that gets created.
Definition: GlobalNPC.cs:85
virtual bool PreAI(NPC npc)
Allows you to determine how any NPC behaves. Return false to stop the vanilla AI and the AI hook from...
Definition: GlobalNPC.cs:109
Mod mod
The mod to which this GlobalNPC belongs.
Definition: GlobalNPC.cs:16
virtual void BossHeadSlot(NPC npc, ref int index)
Allows you to customize the boss head texture used by an NPC based on its state. Set index to -1 to s...
Definition: GlobalNPC.cs:353
virtual bool CheckActive(NPC npc)
Whether or not to run the code for checking whether an NPC will remain active. Return false to stop t...
Definition: GlobalNPC.cs:157
This class allows you to modify and use hooks for all NPCs, including vanilla mobs. Create an instance of an overriding class then call Mod.AddGlobalNPC to use this.
Definition: GlobalNPC.cs:11
virtual bool StrikeNPC(NPC npc, ref double damage, int defense, ref float knockback, int hitDirection, ref bool crit)
Allows you to use a custom damage formula for when an NPC takes damage from any source. For example, you can change the way defense works or use a different crit multiplier. Return false to stop the game from running the vanilla damage formula; returns true by default.
Definition: GlobalNPC.cs:344
virtual void TownNPCAttackShoot(NPC npc, ref bool inBetweenShots)
Allows you to tell the game that a town NPC has already created a projectile and will still create mo...
Definition: GlobalNPC.cs:590
virtual void HitEffect(NPC npc, int hitDirection, double damage)
Allows you to make things happen whenever an NPC is hit, such as creating dust or gores...
Definition: GlobalNPC.cs:141
virtual bool DrawHealthBar(NPC npc, byte hbPosition, ref float scale, ref Vector2 position)
Allows you to control how the health bar for the given NPC is drawn. The hbPosition parameter is the ...
Definition: GlobalNPC.cs:427
virtual GlobalNPC NewInstance(NPC npc)
Create a new instance of this GlobalNPC for an NPC instance. Called at the end of NPC...
Definition: GlobalNPC.cs:69
virtual void TownNPCAttackProj(NPC npc, ref int projType, ref int attackDelay)
Allows you to determine the projectile type of a town NPC&#39;s attack, and how long it takes for the pro...
Definition: GlobalNPC.cs:571
virtual void ModifyHitPlayer(NPC npc, Player target, ref int damage, ref bool crit)
Allows you to modify the damage, etc., that an NPC does to a player.
Definition: GlobalNPC.cs:221
virtual void EditSpawnRate(Player player, ref int spawnRate, ref int maxSpawns)
Allows you to modify the chance of NPCs spawning around the given player and the maximum number of NP...
Definition: GlobalNPC.cs:437
virtual void EditSpawnPool(IDictionary< int, float > pool, NPCSpawnInfo spawnInfo)
Allows you to control which NPCs can spawn and how likely each one is to spawn. The pool parameter ma...
Definition: GlobalNPC.cs:457
virtual bool PreDraw(NPC npc, SpriteBatch spriteBatch, Color drawColor)
Allows you to draw things behind an NPC, or to modify the way the NPC is drawn. Return false to stop ...
Definition: GlobalNPC.cs:397
Mod is an abstract class that you will override. It serves as a central place from which the mod&#39;s co...
Definition: Mod.cs:24
virtual bool SpecialNPCLoot(NPC npc)
Allows you to call NPCLoot on your own when the NPC dies, rather then letting vanilla call it on its ...
Definition: GlobalNPC.cs:174
virtual bool PreChatButtonClicked(NPC npc, bool firstButton)
Allows you to determine if something can happen whenever a button is clicked on this NPC&#39;s chat windo...
Definition: GlobalNPC.cs:492
virtual void DrawTownAttackGun(NPC npc, ref float scale, ref int item, ref int closeness)
Allows you to customize how a town NPC&#39;s weapon is drawn when the NPC is shooting (the NPC must have ...
Definition: GlobalNPC.cs:617
virtual void NPCLoot(NPC npc)
Allows you to make things happen when an NPC dies (for example, dropping items and setting ModWorld f...
Definition: GlobalNPC.cs:191
virtual Color GetAlpha(NPC npc, Color drawColor)
Allows you to determine the color and transparency in which an NPC is drawn. Return null to use the d...
Definition: GlobalNPC.cs:378
virtual void TownNPCAttackMagic(NPC npc, ref float auraLightMultiplier)
Allows you to control the brightness of the light emitted by a town NPC&#39;s aura when it performs a mag...
Definition: GlobalNPC.cs:598
virtual bool CanHitNPC(NPC npc, NPC target)
Allows you to determine whether an NPC can hit the given friendly NPC. Return true to allow hitting t...
Definition: GlobalNPC.cs:240
virtual void PostDraw(NPC npc, SpriteBatch spriteBatch, Color drawColor)
Allows you to draw things in front of this NPC. This method is called even if PreDraw returns false...
Definition: GlobalNPC.cs:407
virtual void OnChatButtonClicked(NPC npc, bool firstButton)
Allows you to make something happen whenever a button is clicked on this NPC&#39;s chat window...
Definition: GlobalNPC.cs:501
virtual void TownNPCAttackSwing(NPC npc, ref int itemWidth, ref int itemHeight)
Allows you to determine the width and height of the item a town NPC swings when it attacks...
Definition: GlobalNPC.cs:607
virtual void BossHeadSpriteEffects(NPC npc, ref SpriteEffects spriteEffects)
Allows you to flip an NPC&#39;s boss head icon on the map.
Definition: GlobalNPC.cs:369
string Name
The name of this GlobalNPC instance.
Definition: GlobalNPC.cs:24
virtual void OnGoToStatue(NPC npc, bool toKingStatue)
Allows you to make things happen when this NPC teleports to a King or Queen statue. This method is only called server side.
Definition: GlobalNPC.cs:536
virtual void TownNPCAttackCooldown(NPC npc, ref int cooldown, ref int randExtraCooldown)
Allows you to determine the cooldown between each of a town NPC&#39;s attack. The cooldown will be a numb...
Definition: GlobalNPC.cs:562
virtual void BuffTownNPC(ref float damageMult, ref int defense)
Allows you to modify the stats of town NPCs. Useful for buffing town NPCs when certain bosses are def...
Definition: GlobalNPC.cs:544