Guide:NPC Happiness

The NPC happiness mechanic poses a complex puzzle to avoid paying excessively high prices. Many NPCs who like each others' company prefer different biomes, and some NPCs like another but are disliked or hated in return. Additionally, purchasing pylons in order to set up a network is impossible without putting thought into NPC happiness.

There are several different ways to address the situation, this guide is intended to discuss them. The last section gives a single general solution for all NPCs, but before that it is worth discussing more flexible strategies.

Basic constraints
The exact rules are listed on NPCs § Factors affecting happiness, but the issues can be summarized (all of these factors combine):
 * NPCs get a 12% or 6% bonus for being in a loved or liked biome, or a penalty for being in a hated or disliked one.
 * They get a similar bonus or penalty for being housed near another NPC they love, like, dislike, or hate.
 * "Near" in this context means that their housing flags are within 25 tiles (50 feet) of each other.
 * They get an "overcrowding" penalty for having more than two "nearby" NPC, no matter the relationship. The penalty is 5% for each additional "nearby" NPC, i.e., for the third, the fourth, etc. "nearby" NPC.
 * Conversely, they get a 5% "solitude" bonus for not having more than two NPCs nearby, and having no more than three other NPCs (that is, the two "roommates" do not count) within 120 tiles.
 * Note that this much larger range may sometimes represent over a full screen's width.

The above factors affect a variety of transactions with NPCs:
 * Vendor prices for selling their items will be lower if they are happy, higher if they are unhappy.
 * Conversely, vendor prices for buying the player's items will be higher if they are happy, lower if they are unhappy.
 * This does affect the Nurse's fees for healing and the Goblin Tinkerer's reforging costs.
 * This also affects the Angler's quest rewards – if happy or unhappy, he will give rewards as if the player had completed more or fewer quests, respectively.
 * This even affects the Tax Collector's revenue stream – if happy, he will not only collect more money in a given time, but gather a higher maximum amount, and the reverse if he is unhappy.

And two other factors come into play, but for these happiness is irrelevant – all that matters is the NPC's presence:
 * NPCs also suppress enemy spawning nearby. In Normal mode, 3 NPCs "on screen" are needed to completely suppress the spawning of enemies, which is usually desirable for a town.
 * To remain functional, a pylon needs to have two NPCs housed within 169×124-tile rectangle centered on the pylon.

Depending on game progress, there are also up to three town pets available. These count as NPCs for pylons and enemy suppression, but not for crowding/solitude calculations; while it is sometimes claimed that they can affect the happiness of nearby NPCs, this is not the case either positively or negatively, rendering them useful primarily for keeping a town eligible for pylons and enemy suppression when other NPCs are not available (for instance, keeping a dog and cat in the same duplex as the Nurse and Arms Dealer would prevent you from accidentally disabling their pylon if you often relocate the two of them for boss fights), or for extending a town's enemy suppression zone further without crowding its inhabitants.

Casual strategy
Some more general points to consider:
 * Empty houses are not considered – even if a given space could house an NPC, if it does not, then it has no effect on NPC happiness.
 * Not all NPCs are created equal:
 * The Goblin Tinkerer will get a lot of business throughout the game, so any discounts you can get from him will add up.
 * Similarly, the Tax Collector is an ongoing revenue stream, so you want to keep him happy.
 * The Nurse's prices do not necessarily matter much, in that when you need her you will pay what she asks, but if you are likely to use her much, you should probably try to keep her happy. While a Potion of Return can let you drop by wherever she is, this will not be desirable during some boss fights, hence if you are bringing her to the fight, you might want to bring a roommate along to keep her happy.
 * If you rarely purchase from a given NPC or their wares are cheap (like the Painter's), you will not have to care much about their happiness – and with a little advance warning, you can move them into a better environment when you do need to make a big purchase.
 * You want at least one vendor to be as happy as possible when buying your items, and probably at least one for each of your major bases.
 * The Guide's happiness has no effect at all and can be disregarded entirely.

Examining the rules, it seems that a reasonable strategy is as follows:
 * Pick locations for your pylons (and perhaps some non-pylon bases), and loosely cluster two or three "duplex" NPC homes at each of them, each duplex separated by over 25 tiles.
 * The "duplex" part means that each of these homes would also include a "guest room", a second housing-suitable space. This does not need to be filled up front, but it lets you bring in a liked or loved roommate on short notice, and also serves as ready housing for new arrivals until you pick a new spot for them.
 * This allows you to give all the NPCs some space, while making sure the pylon stays live.
 * The third NPC, or a town pet, comes in handy in case you need to pull an NPC elsewhere on short notice.

This general arrangement can evolve as new NPCs arrive, with the guest rooms doubling as ready housing for newcomers. You can also pair off your more valuable NPCs as desired without disturbing their neighbors, while leaving the less popular ones on pylon duty or exile.

Calculated strategy
An "optimal solution" for pairing off all the NPCs is listed below, maximizing average happiness across all NPCs. This includes all NPCs including Hardmode (except for the Princess), and represents a single setup meant to be left unchanged. By nature, such an optimization cannot take full account of issues such as easy access or pylon placement, and this one is fairly simple – focused on average happiness, no priority for the "important" NPCs mentioned above, and it assumes all NPCs are paired or alone.

It is assumed that for each NPC A, there are at maximum two other NPCs within 25 tiles and there are at maximum three other NPCs between 25 and 120 tiles away. The final values include the 5% bonus granted for this "solitary" setup.