NPC spawning

In Terraria, monsters will spawn off camera and pursue the player based on their AI. The rate at which monsters are spawned, the maximum number of monsters, and the type of monster spawned are all dependent on the time of day, biome, the tile type of the ground, and other factors.

Spawning Basics
Each player in 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.

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 73 tiles to the left or right, and 45 tiles up or down. If the picked tile is 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. 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 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 less than 54 tiles away left or right, or 33 tiles up or 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, moon phase
 * Depth
 * Relative X location on the map
 * Current Zone (Corruption, Meteor, Jungle, etc...)
 * Ground Tile Type
 * Randomness

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