Terraria ModLoader  0.11.1
A framework for 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 
201  public virtual bool CanHitPlayer(NPC npc, Player target, ref int cooldownSlot) {
202  return true;
203  }
204 
212  public virtual void ModifyHitPlayer(NPC npc, Player target, ref int damage, ref bool crit) {
213  }
214 
222  public virtual void OnHitPlayer(NPC npc, Player target, int damage, bool crit) {
223  }
224 
231  public virtual bool? CanHitNPC(NPC npc, NPC target) {
232  return null;
233  }
234 
243  public virtual void ModifyHitNPC(NPC npc, NPC target, ref int damage, ref float knockback, ref bool crit) {
244  }
245 
254  public virtual void OnHitNPC(NPC npc, NPC target, int damage, float knockback, bool crit) {
255  }
256 
264  public virtual bool? CanBeHitByItem(NPC npc, Player player, Item item) {
265  return null;
266  }
267 
277  public virtual void ModifyHitByItem(NPC npc, Player player, Item item, ref int damage, ref float knockback, ref bool crit) {
278  }
279 
289  public virtual void OnHitByItem(NPC npc, Player player, Item item, int damage, float knockback, bool crit) {
290  }
291 
298  public virtual bool? CanBeHitByProjectile(NPC npc, Projectile projectile) {
299  return null;
300  }
301 
311  public virtual void ModifyHitByProjectile(NPC npc, Projectile projectile, ref int damage, ref float knockback, ref bool crit, ref int hitDirection) {
312  }
313 
322  public virtual void OnHitByProjectile(NPC npc, Projectile projectile, int damage, float knockback, bool crit) {
323  }
324 
335  public virtual bool StrikeNPC(NPC npc, ref double damage, int defense, ref float knockback, int hitDirection, ref bool crit) {
336  return true;
337  }
338 
344  public virtual void BossHeadSlot(NPC npc, ref int index) {
345  }
346 
352  public virtual void BossHeadRotation(NPC npc, ref float rotation) {
353  }
354 
360  public virtual void BossHeadSpriteEffects(NPC npc, ref SpriteEffects spriteEffects) {
361  }
362 
369  public virtual Color? GetAlpha(NPC npc, Color drawColor) {
370  return null;
371  }
372 
378  public virtual void DrawEffects(NPC npc, ref Color drawColor) {
379  }
380 
388  public virtual bool PreDraw(NPC npc, SpriteBatch spriteBatch, Color drawColor) {
389  return true;
390  }
391 
398  public virtual void PostDraw(NPC npc, SpriteBatch spriteBatch, Color drawColor) {
399  }
400 
409  public virtual bool? DrawHealthBar(NPC npc, byte hbPosition, ref float scale, ref Vector2 position) {
410  return null;
411  }
412 
419  public virtual void EditSpawnRate(Player player, ref int spawnRate, ref int maxSpawns) {
420  }
421 
430  public virtual void EditSpawnRange(Player player, ref int spawnRangeX, ref int spawnRangeY,
431  ref int safeRangeX, ref int safeRangeY) {
432  }
433 
439  public virtual void EditSpawnPool(IDictionary<int, float> pool, NPCSpawnInfo spawnInfo) {
440  }
441 
448  public virtual void SpawnNPC(int npc, int tileX, int tileY) {
449  }
450 
456  public virtual bool? CanChat(NPC npc) {
457  return null;
458  }
459 
465  public virtual void GetChat(NPC npc, ref string chat) {
466  }
467 
474  public virtual bool PreChatButtonClicked(NPC npc, bool firstButton) {
475  return true;
476  }
477 
483  public virtual void OnChatButtonClicked(NPC npc, bool firstButton) {
484  }
485 
492  public virtual void SetupShop(int type, Chest shop, ref int nextSlot) {
493  }
494 
500  public virtual void SetupTravelShop(int[] shop, ref int nextSlot) {
501  }
502 
508  public virtual bool? CanGoToStatue(NPC npc, bool toKingStatue) {
509  return null;
510  }
511 
518  public virtual void OnGoToStatue(NPC npc, bool toKingStatue) {
519  }
520 
526  public virtual void BuffTownNPC(ref float damageMult, ref int defense) {
527  }
528 
535  public virtual void TownNPCAttackStrength(NPC npc, ref int damage, ref float knockback) {
536  }
537 
544  public virtual void TownNPCAttackCooldown(NPC npc, ref int cooldown, ref int randExtraCooldown) {
545  }
546 
553  public virtual void TownNPCAttackProj(NPC npc, ref int projType, ref int attackDelay) {
554  }
555 
563  public virtual void TownNPCAttackProjSpeed(NPC npc, ref float multiplier, ref float gravityCorrection,
564  ref float randomOffset) {
565  }
566 
572  public virtual void TownNPCAttackShoot(NPC npc, ref bool inBetweenShots) {
573  }
574 
580  public virtual void TownNPCAttackMagic(NPC npc, ref float auraLightMultiplier) {
581  }
582 
589  public virtual void TownNPCAttackSwing(NPC npc, ref int itemWidth, ref int itemHeight) {
590  }
591 
599  public virtual void DrawTownAttackGun(NPC npc, ref float scale, ref int item, ref int closeness) {
600  }
601 
610  public virtual void DrawTownAttackSwing(NPC npc, ref Texture2D item, ref int itemSize, ref float scale, ref Vector2 offset) {
611  }
612  }
613 }
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:201
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:322
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:610
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:243
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:456
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:492
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:535
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:448
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:430
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:500
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:465
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:352
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:254
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:311
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:289
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 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:277
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:563
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:508
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:222
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:378
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:264
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:298
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.
Definition: GlobalNPC.cs:344
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:335
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:572
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:409
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:553
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:212
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:419
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:439
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:388
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:23
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:474
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:599
virtual void NPCLoot(NPC npc)
Allows you to add drops to an NPC when it dies.
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:369
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:580
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:231
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:398
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:483
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:589
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:360
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:518
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:544
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:526