I believe that the following formula determines the “base happiness” of a city, which is to say the state of happiness of a city before luxuries, buildings, military units, wonders, and specialists are applied to it.
CiNbr is the universal number of the city in question, counting from 0.
CiOw is the number of cities owned by the player, subject to certain conditions below.
MaxC is the maximum number of cities (based on difficulty level and government), described in a post above.
DfCt is the default content citizens (1 at Deity)
Z = (CiOw – MaxC), if CiOw – MaxC >= 0
Z = 0, if CiOw – MaxC < 0
Quotient[x, y] is the division x/y, rounded down
Remainder[x, y] is the remainder of the division x/y
Extr = 1, if Remainder[(CiNbr + Z), MaxC] < Remainder[Z, MaxC]
Extr = 0, if Remainder[(CiNbr +Z), MaxC] >= Remainder[Z, MaxC]
DfCt – Quotient[Z, MaxC] – Extr = Base Happiness
If Base Happiness is positive, then there will be default content citizens in the city. If Base Happiness is 0, then all citizens will unhappy. If Base Happiness is negative, there will be double unhappy citizens (Black Hats) in the city by default.
The Calculation for Extr (the extra unhappy citizen) has been of the most interest, and is new. If you have the (universal) number of the city, and the number of cities owned by the player, you can find out if the city will suffer the extra unhappy citizen (cities where Extr = 1 will have the extra unhappy citizen).
If CiOw is less than MaxC (not enough cities to trigger the riot factor), then Z will be 0, Remainder[Z, MaxC] will be 0, and it will be impossible any number to be less than that, so Extr will be 0 for all cities, and no cities will suffer from the riot factor.
CiOw is not actually the number of cities owned. Rather, it is increased by 1 every time a city is built by the player, and is decreased by 1 every time a city owned by the player is destroyed. It is also reduced by 1 when a city is disbanded (for any cities processed after the city that was disbanded). CiOw does not change when cities are captured or lost. Finally, CiOw is set to the number of cities currently owned by the player each turn, after all the cities are processed. Essentially, if you capture cites, you will get a turn of production without them adding to your unhappiness; but if you lose cities they will still contribute to your unhappiness during your next turn’s production.
There are still two problems to be looked at:
How are cities given a city number, and do the numbers change during the game?
How would you go about finding out the numbers of your cities while playing a game?
Example: Deity as the Romans, regular map, despotism. 20 cities (0-19), 5 owned by the Romans (0, 7, 10, 13, 17).
Find Extr (important part)
CiOw = 5 (no cities captured or lost since cities last processed)
MaxC = 4
Z = (CiOw - MaxC) = 5 - 4 = 1
Remainder[Z, MaxC] = Remainder[1, 4] = 1
City 0 (first city built):
Remainder[CiNbr + Z, MaxC] = Remainder[0+1, 4] = Remainder[1, 4] = 1
1 < 1 is FALSE, so Extr = 0
City 0 DOES NOT suffer from the Riot Factor.
City 7 (8th city built):
Remainder[7+1, 4] = Remainder[8, 4] = 0
0 < 1 is TRUE, so Extr = 1
City 7 SUFFERS from the Riot Factor.
City 10 (11th built):
Remainder[10+1, 4] = Remainder[11, 4] = 3
3 < 1 is FALSE, so Extr = 0
City 10 DOES NOT suffer from the Riot Factor.
City 13 (14th built):
Remainder[13+1, 4] = Remainder[14, 4] = 2
2 < 1 is FALSE, so Extr = 0
City 13 DOES NOT suffer from the Riot Factor.
City 17 (18th built):
Remainder[17+1, 4] = Remainder[18, 4] = 2
2 < 1 is FALSE, so Extr = 0
City 17 DOES NOT suffer from the Riot Factor.
Part 2: Romans build 6th City, city 20 (21st city built).
CiOw = 6
MaxC = 4
Z = CiOw - MaxC = 6 - 4 = 2
Remainder[Z, MaxC] = Remainder[2, 4] = 2
City 0:
Remainder[0 + 2, 4] = Remainder[2, 4] = 2
2 < 2 is FALSE, so Extr = 0
City 0 DOES NOT suffer from the Riot Factor.
City 7:
Remainder[7+2, 4] = Remainder[9, 4] = 1
1 < 2 is TRUE, Extr = 1
City 7 SUFFERS from the Riot Factor.
City 10:
Remainder[10+2, 4] = Remainder[12, 4] = 0
0 < 2 is TRUE, Extr = 1
City 10 SUFFERS from the Riot Factor.
City 13:
Remainder[13+2, 4] = Remainder[15, 4] = 3
3 < 2 is FALSE, Extr = 0
City 13 DOES NOT suffer from the Riot Factor.
City 17:
Remainder[17+2, 4] = Remainder[19, 4] = 3
3 < 2 is FALSE, so Extr = 0
City 17 DOES NOT suffer from the Riot Factor.
City 20:
Remainder[20+2, 4] = Remainder[22, 4] = 2
2 < 2 is FALSE, so Extr = 0
City 20 DOES NOT suffer from the Riot Factor.