AI logic: revolutions/government change

tupi

Warlord
Joined
Jun 25, 2011
Messages
237
Location
Russia
So, how and when AI civs change their government.

1) every turn (after main city loop) AI checks if anarchy flag (diplomacy bit 'ALLY' with barbarians) is 1. If it's so, it calls 'change govt' function which in this case just sets anarchy flag to 0. This means that AI revolution always takes 1 turn. If you have embassy, you will get 'AI government changes to X!' message here.

2) human player, on the other hand, doesn't use this anarchy flag and instead changes its government to ANARCHY (0) during revolution. AI never goes through the anarchy as government number 0, it sets new govt immediately and instead of 'true anarchy' it just sets 'anarchy flag' to 1. Game uses 'anarchy flag' for human player only for 'democratic government collapsed' event. In this case, when game should give you a new govt selection menu it instead just sets 'anarchy flag' to 0. That's why in this case the anarchy period is much longer (8 turns max instead of 4). EDIT: actually, because govt selection menu is called right after city loop, there's a chance that "democracy collapsed" flag will be canceled immediately at the same turn, so anarchy duration in this case actually is not 5..8 turns, but 4..7 (normal anarchy duration is 1..4). Also, with Pyramids after collapse you will get govt selection menu immediately after city loop, at the same turn...

3) AI tries to change its govt every 8th turn, if remainder of (civ_number+game_turn)/8 is 0 (also if civ is not barbarians and not with bit flag 'human player'). If AI chose some govt, it will call 'change govt' function. If 'new' govt is different from the old one, you will get message 'The AI government has been overthrown!' and anarchy flag will be set to 1. Replay entry is also recorded here (when AI goes into anarchy state, not when it goes from it!). But even if 'new' govt is the same, AI will still change its tax rate (see https://forums.civfanatics.com/thre...ogic-tax-science-luxuries-distribution.657037). This basically means AI usually resets its tax rate every 8 turns.
Logic to change goverment here is following:
Code:
IF despotism_desire < 1
THEN
    IF civ DOESN'T know REPUBLIC advance OR republic_desire < 0
        THEN
        IF civ DOESN'T know COMMUNISM advance or number of cities < 11
            THEN IF civ DOESN'T know MONARCHY advance
                DON'T DO ANYTHING;
            ELSE
                SET GOVT TO MONARCHY;
        ELSE
            SET GOVT TO COMMUNISM;
    ELSE
        SET GOVT TO REPUBLIC;
ELSE
    SET GOVT TO DESPOTISM;
As you can see, there's no Pyramids in these rules. Pyramids are useless for AI. Also (as expected, because no one in 30 years has ever seen that) AI can't choose Democracy.
And about these 'despotism_desire' and 'republic_desire' variables and how they are calculated - in the next post...

4) when AI changes it's govt (goes to anarchy) it forgets all vendettas, means it will no more be upset about your (and others AIs) previous sneak attacks.
 
Last edited:
'republic desire'.
1) set it to 0 at the beginning of city loop

2) for every cultvated square in every city, if it produces at least 1 trade arrow: increase it by 1.

3) for every city, decrease it by
(marketplace_bonus-leader_temper)*units_outside_home_city
where
units_outside_home_city - same rules as for unhappy faces under republic (aircraft always +1, units with 0 attack always ignored)
marketplace_bonus is 5 if city has a marketplace and 7 otherwise
leader_temper is -1 for militaristic, +1 for civilized and 0 otherwise

4) for every city, check next conditions:
IF city total shields >= shields used for units support
AND gl_ai_handicap_flag == 1
AND number of techs of AI civ < number of techs of human player
THEN
set republic desire to -999

gl_ai_handicap_flag is 1 if human player is the best civ in the world and has more than 4 cities and don't have any nuclear bombs and game turn > 200 (see https://pastebin.com/kexMnwH6)
 
Last edited:
'despotism desire':

1) set it to 0 at the beginning of city loop

2) for every cultivated city square in every city and for every type of resource (food/shields/trade): if production of this resouce is more than 2 (without despotism penalty/republic bonus) and there's no We Love The Leader parade in the city: decrease despotism_desire by 2

3) for every city:
IF city_size > city_supported_units THEN increase despotism_desire by city_supported_units
ELSE increase despotism_desire by city_size.

4) for every city:
IF supported_units > city_total_shields THEN increase despotism_desire by (supported_units - city_total_shields)*5
 
Last edited:
I thought about strange behaviour of big AI empires later in the game: they love to transform to despotism. It looks so stylishly when they do that and start, like, a total war. But the reason for this is simple and along these rules: if AI created many, many units, then support (used shields) for these units is growing, so AI decides that despotism is better (because free unit support if < city population!). And usually after that AI starts the war(s), so its units are demolished in these wars -> so AI returns to its normal govt... All according the rules, but how elegant all of this looks!
 
I thought about strange behaviour of big AI empires later in the game: they love to transform to despotism. It looks so stylishly when they do that and start, like, a total war. But the reason for this is simple and along these rules: if AI created many, many units, then support (used shields) for these units is growing, so AI decides that despotism is better (because free unit support if < city population!). And usually after that AI starts the war(s), so its units are demolished in these wars -> so AI returns to its normal govt... All according the rules, but how elegant all of this looks!

Yes, it's like art imitating life.
In real life, a government spending more money on military sector is more likely to engage in wars than a government prioritizing health or education.
However, no country can be at war at all times - so, after the war naturally comes a time of peace and then it will all start again...
 
I mean, the US is the bigget warmonger IRL and has been at war nearly constantly for the last century or so.

Though Russia has currently the most recent annexation.
 
I like how this simple system works on different levels, some of which might not even have been intended. On one hand, it's obviously a way for the AI to avoid shield shortages and waste of trade, to maximize its territorial assets, and to manage potential unhappiness after a revolution, very much like a human player would prioritize these things -- you don't want disappearing units and rioting cities when you switch to republic, and you don't want to go hungry and broke when you switch to despotism. On the other hand, the more shields or trade you have, they more pull they have on your politics. Just like in the real world a large military doesn't want to be disbanded and has sway over the government, and large corporations don't want to be regulated and influence politics.
 
Top Bottom