Guide:NPC Happiness

The new 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. And then there are the Pylons to consider....

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's worth discussing more flexible strategies.

Basic Constraints
The exact rules are listed here, but the issues can be summarized (all of these factors combine):
 * NPCs get a 10% or 5% 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. This is not very far; for a typical desktop this is a small fraction of the screen.  A typical screen might be over 100 tiles wide and 60 tiles high.
 * They get an "overcrowding" penalty for having more than one "nearby" NPC, no matter the relationship. The penalty is 4% for each "nearby" NPC beyond the first.
 * Conversely, they get a 10% "solitude" bonus for not having more than one NPC nearby, and having no more than three other NPCs (that is, the "roommate" doesn't count) within 120 tiles.
 * Note that this much larger range represents over a full screen's width.

The above factors do affect the Nurse's prices, the Goblin Tinkerer's reforging costs, and even the Tax Collector's revenue stream.

And two other factors come into play:
 * 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 tile wide by 124 tile high rectangle centered on the Pylon.
 * Note that this range is a good deal wider than even the range for the "solitude" bonus.

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. They are also reported to raise the happiness of nearby NPCs; while exact details have not been provided, it seems reasonable to guess that they will apply a 5% or 10% bonus, similar to a liked or loved NPC.

A Casual Strategy
Some more general points to consider:
 * Empty houses are not considered -- even if a given space could house an NPC, if it 'doesn't, 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 don't necessarily matter much, in that when you need her you'll pay what she asks, but if you are likely to use her much, you should probably try to keep her happy. A Potion of Return can let you drop by wherever she is, but some bosses won't be too happy with that; 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 (hello, Painter) you don't 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 stuff, and probably at least one for each of your major bases.
 * And the poor Guide gets the short end of the stick again....

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 doesn't 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.

Grand strategy
The Discord user ShatteredSunlight brute-forced a general "optimal solution" for pairing off all the NPCs listed below. This includes all NPCs including Hardmode, 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, but it does minimize prices in general.

All values are final happiness and include 0.9 bonus from having <=1 NPC within 25 tiles and assuming no other NPCs within 120 tiles. NPCs appearing by themselves are to be solitary. Happiness is shown as a price modifier for NPC A.