NPC spawning

In Terraria, enemies, critters, and other NPCs will spawn off-screen and enter the screen area based on their AI. The rate at which enemies are spawned, the maximum number of enemies, and the type of enemies spawned are all dependent on the time of day, biome, the tile type of the ground, and other factors.

Basics
The game determines new enemy spawns every tick.✅ There are two variables it takes into consideration when doing so: spawn rate and max spawns, with the spawn rate limiting the chance of an enemy spawning at the tick, and the max spawns limiting the number of enemies that are active at a time.

The default spawn rate, for instance, is 1/600. This means that each tick, there is a chance of 1/600 that an enemy is spawned. Since there are 60 ticks in 1 second, the chance each second is 1/10; hence with a spawn rate of 1/600, an enemy is spawned, on average, every 10 seconds.

The default max spawns, for instance, is 5. This means that a new enemy can only be spawned if the sum of the NPC slots of all current active enemies is lower than 5. All enemies occupy 1 NPC slot by default; see the following table for a list of all enemies with a higher or lower number of NPC slots. Active enemies exclude, for example, body and tail parts of worms, because a Giant Worm, which has 6–8 segments (i.e. individual enemies), would otherwise occupy a disproportional number of NPC slots. Town NPCs are not considered active NPCs for the purposes of NPC spawning. See the following table for a list of enemies that are not considered active.

Natural spawn rates
Spawn rate and max spawns have set values, which are different depending on the environment. They are formally defined for the environments mentioned in the following tables. Some events employ separate mechanisms to control spawn rates; bosses usually occupy enough NPC slots to block most other spawns.

Some biomes will override each other when determining the spawn rate. Corruption, Dungeon, Meteor and Jungle are all mutually exclusive spawn rates. Dungeon has the highest priority, then jungle, corruption, and finally meteor. For example, if a meteor hits the jungle, it will use jungle spawn rates.

Additionally, the spawn rate will be modified by the number of active enemies in the area. The fewer active enemies there are, the faster new ones will spawn: (Note that all products are rounded down to the nearest integer.)
 * If the sum of the NPC slots of all active enemies is lower than 20% of max spawns, the denominator of the spawn rate will be multiplied by 0.6.
 * If the sum of the NPC slots of all active enemies is lower than 40% and greater or equal 20% of max spawns, the denominator of the spawn rate will be multiplied by 0.7.
 * If the sum of the NPC slots of all active enemies is lower than 60% and greater or equal 40% of max spawns, the denominator of the spawn rate will be multiplied by 0.8.
 * If the sum of the NPC slots of all active enemies is lower than 80% and greater or equal 60% of max spawns, the denominator of the spawn rate will be multiplied by 0.9.

Examples:

Manual alteration of spawn rates
Certain items can be used to change the spawn rate and max spawns when used or placed, as per the following table. All effects are multiplicative rather than additive.

Water and Peace Candles cancel each other out, even though they do not alter spawn rates by the same absolute values. If both are active (e.g. a Peace Candle is held while a Water Candle is placed nearby), none is applied.

There is one exception to this: Water Candles placed (explicitly not held) in Space always increase spawn rates, regardless of nearby Peace Candles. Their effectiveness depends on nearby Peace Candles, however:

Up to this point, spawn rates cannot be increased to fractions greater than 1/ and max spawns cannot be increased to values greater than. Both will be cut off there. The spawn rates and max spawns of the invasion events listed in the tables above are exempt from this.

Effect of Town NPCs on spawn rates
The presence of nearby town NPCs generally reduces spawn rates and max spawns. This effect is adjusted in the Underworld.

When 3 or more town NPCs, or the Skeleton Merchant are present nearby, no enemies will spawn. This is only valid for areas outside of the Underworld and only for normal worlds – in, there is still a 1/30 chance an enemy may spawn that tick. Events and bosses (and their servants) will bypass this restriction.

Journey Mode
Spawning rates can be manually altered in Journey Mode, from 0× (disabling spawns) to 10× the normal rate. Even with the spawn rate set to 0×, Bosses may still be summoned, and mining up Tombstones can still cause Ghosts to spawn.

Spawning Basics
Each player in the game will spawn their own enemies. If two players are in the same area, monsters will appear twice as fast as they would with only one player. Max Spawns rule are still used, which means that even if 2 players are in the same spot, you cannot gather 30 normal monsters near the spot. (Don't forget, the bats count as 1/2, and some special enemies count as 2 or more.)

Choosing a spot to spawn the enemy
When the game decides to spawn an enemy, it will first choose a random tile within a rectangle around the player (specifically, the tile containing the top left corner of the player's hitbox). The maximum distance from the player it can choose from is 84 tiles east, 83 west, 46 up, and 45 down. If the picked tile is a solid block, it will try another one. Tiles that are actuated out do not count as solid. If the picked tile has a player-placed wall that blocks enemies from spawning, it will also try another tile.

Once an empty tile is found, it moves down until it finds the ground. (Note that this tile can be more than 45 tiles below from the player. If it finally is outside the "Active Rectangle" it will be instantly despawned, anyway.) If there is enough space for an enemy to be spawned at this location, this will be the tile it chooses. The exception to this is when the player is in an area where Harpies and many other flying enemies can be spawned. In this case, the game does not attempt to find a ground tile.

The chosen tile is then checked to make sure it is not within the "safe" area around the player. It cannot be within 62 tiles east or west, or 34 tiles up or down. Also, the chosen tile space must be completely outside 2088 × 1172 pixel rectangles centered on the center of each player's hitbox. (If the player is not riding a mount, the top edge of this is 34 + 3/16 tiles above the top left corner tile mentioned above, meaning that NPCs can spawn on ground exactly 35 tiles above a player standing on half blocks, but not a player standing on full blocks.)

Using certain items causes monsters to spawn further out than normal. Equipping a Rifle Scope or Sniper Scope does so only when the player has a compatible gun selected, whether or not the player is zoomed out.

On the, the no-spawn area around the player is 68 by 48 tiles, and the spawn area is 112 by 62 tiles. In addition, enemies cannot spawn closer than 34 tiles to the sides and 24 tiles up and down. They also cannot spawn farther than 56 tiles to the sides and 31 tiles up and down.

Choosing a type of enemy to spawn
The type of enemy spawned will depend on many factors including, but not limited to:


 * Time of day
 * Weather (e.g., Angry Nimbus and Ice Golem require Rain/Blizzard)
 * Depth
 * Relative X location on the map (Ocean)
 * Current biome (Corruption, Meteor, Jungle, etc...)
 * Ground tile type
 * Wall behind player (e.g. Dungeon Brick Wall, Lihzahrd Brick Wall)
 * Special events (Blood Moon, Solar Eclipse, invasion events, etc)
 * Randomness