NPC spawning

In Terraria, enemies, critters, and 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.

Spawn Rates
There is a 1 in "Spawn rate" chance an enemy will spawn every game tick. There are 60 ticks per second, so a spawn rate of 600 means, approximately, a 10% chance of at least one enemy spawning per second, and a spawn rate of 100 means approximately a 45% chance of at least one enemy spawning per second.

When the game is determining the maximum number of enemies in the area, the "Max Spawns" is used. Some enemies count towards this limit more than others. Most bats are considered 0.5, and uncommon enemies typically count as 3. Bosses count as 5. Critters do not count toward the limit. If The Destroyer is spawned, it will count way over the limits, ~80 segments * 5 for bosses = 400 enemies!

Note: the "Max Spawns" focuses only on monsters that are in a specific area whose center is the player, in other words, active enemies for the player. When an enemy is outside the 504*283.5 blocks rectangle (1920*1080 pixels screen multiplied with 4.2*4.2) and it is not a boss, the monster will not be considered active, thus not counted in when checking the max-spawns limit and other calculations. (Fortunately for the enemy and event farming: the enemy will despawn if it is outside another specific rectangle which is much smaller. More details, go to NPC Despawning)

The minimum (fastest) spawn rate under normal conditions is 60, and the maximum spawns is 15. The modifiers from Water Candle and Battle Potion are set before these limits are checked. Invasion events (including Goblin Army, Frost Legion, and Pirate Invasion and Martian Madness) and moon Events(Lunar Event/Pumpkin Moon/Frost Moon) are a special case; they ignore any modifiers or limits to the spawning rates or number of spawns (including water candles/ battle potions).

Drinking a Calming Potion decreases enemy spawn rate.

Additionally, the spawn rate will be modified by the number of active enemies in the area. The fewer active enemies there are, the faster they will spawn. If there are fewer than 20% of the enemies the maximum number of spawns allows, the spawn rate will be multiplied by 0.6. If fewer than 40% of the enemies exist, the spawn rate is multiplied by 0.7, 60% of the max spawns correspond to the multiplier 0.8 and 80% of the enemies means that the spawn rate is multiplied by 0.9. That means that if the player is in an area where the spawn rate is 600 and the max spawns are 10, but there are only 2 or less enemies, the new spawn rate is 360 until more enemies spawn.

The presence of friendly NPCs greatly reduces the rate at which enemies spawn, and also reduces the maximum enemies as well. When 3 or more NPCs, or the Skeleton Merchant are present, no enemies will spawn. Events such as Slime Rain, Pirate Invasion, Lunar Event, etc bypass this reduction.

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.

Spawning Basics
Each player in the game will spawn their own monsters. If two players are in the same area, monsters will appear twice as fast as they would with only one player. (But 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 half a monster, and some special monsters count as 2 or more.))

Choosing a spot to spawn the monster
When the game decides to spawn a monster, it will first choose a random tile within a rectangle around the player. The maximum distance from the player it can choose from is 84 tiles to the left or right, and 47 tiles up or down. If the picked tile is a solid block, it will try another one. If the picked tile has a wall that blocks monsters 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 47 tiles vertically from the player. If it finally is outside the "Active Rectangle" it will be instantly despawned, anyway.) If there is enough space for a monster 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. Monsters cannot spawn closer than 62 tiles to the sides and 35 tiles up and down. They also cannot spawn farther than 84 tiles to the sides and 47 tiles up and down.

Choosing a type of monster to spawn
The type of monster 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

Certain events, such as Goblin Army, can override or change certain rules about spawning. For example, Goblin Army spawning will not be stopped by walls that normally prevent spawning.