AI colony runs religionless Theo with acess to religion

I had this happen in my last game (running the most recent solver patch), though I didn't save it. The Ethiopian civ was running theocracy with no religion. I think my religion (Islam) was very slowly (naturally) spreading through his cities, with a few others. Towards the very end of the game, he either chose a religion or declared free religion (I don't recall which), but he had the useless Theocracy/no state religion combo for quite a long time.

I should probably note he was isolated on a continent by himself for a long time, and didn't get a religion in any of his cities until I made contact with him. If I remember correctly, I traded him the tech for theocracy that turn, so he switched over to Theocracy just his first city or 2 got a religion.
 
Hooray! Finally an example with a posted save.... from a game with locked modified assets set :cry:

Thanks for staying on top of this r_rolo1. I'll see if there's anything I can find out from this one and if I get real desperate I'll hit end turn 50 times on the Hammurabi ALC and hope that it happens there for me like it did for Sisiutil...
 
I had this happen in my last game (running the most recent solver patch), though I didn't save it. The Ethiopian civ was running theocracy with no religion. I think my religion (Islam) was very slowly (naturally) spreading through his cities, with a few others. Towards the very end of the game, he either chose a religion or declared free religion (I don't recall which), but he had the useless Theocracy/no state religion combo for quite a long time.


Why do you say theo/no state is useless? The whole point of theo is to get the +2 XP, so theo is always useful. While it's strange he didn't pick a religion, maybe it wasn't worth the turn of anarchy or a hit to relations? In any case, not picking a religion has nothing to do with theo.

I think I'd like to see the code, before declaring this a bug. It may be a bug, but it could also be intentional.
 
No.... read the theo civic description. You only get the 2 XP in units built in cities with the State religion. If you don't have a State religion, Theo is worthless.

And that is the whole point.

To add, given the fact that only happens sometimes and that Firaxis fixed a similar "feature" long ago ( the theo blocking of auto spread when running No state religion ), and that it doesn't happen with non-colonies, I surely doubt that it is intentional
 
Ah, okay. I never noticed that the XP only gets added to cities with the state religion.
 
A small amount of further information from Sisiutil's ALC 24. When a colony is created, it has no religion and all the starter civics. A turn or so after creation, it will massively change its civics and (if applicable) its religion. The bug is that sometimes the colony decides to not choose a religion even though it chose a civic that is useless without one (Theocracy).

If anyone experiences this, please get us a save from the turn before the colony makes all its civics changes (or even the turn before creation) so that we can hopefully see why a religion is not chosen. Unfortunately on ALC 24, the closest save Sisiutil had was from 50 turns before the colony was created and when I "forced" colony creation (Worlbuildered some cities on the same landmass for the parent civ) the colony did spawn, but it also properly chose a religion...
 
While digging through CvPlayerAI.cpp and testing with the 1706 AD ALC save I found this:
Izzy has 4 cities, 3 have Taoism and one has Islam, she is in her favorite civic Theocracy but without a state religion--why doesn't she convert to Taoism???

CvPlayerAI::AI_bestReligion() asks for iValue = AI_religionValue((ReligionTypes)iI).
iValue will be 0 for all religions that Izzy doesn't have, otherwise it is equal to the sum of the number of cities in the whole world that have the religion plus the number of Izzy's citizens who believe/know it.
Islam is the dominant faith in the world, therefore Taoism cannot become the best religion for Izzy although the majority of her people prefer it. But it won't be Islam either because it is not "pure enough":

Code:
[SIZE="3"]int iBestCount = getHasReligionCount(eBestReligion);
int iSpreadPercent = (iBestCount * 100) / std::max(1, getNumCities());
int iPurityPercent = (iBestCount * 100) / std::max(1, countTotalHasReligion());
if (iPurityPercent < 49)
{
	if (iSpreadPercent > ((eBestReligion == eFavorite) ? 65 : 75))
	{
		if (iPurityPercent > ((eBestReligion == eFavorite) ? 25 : 32))
		{
			return eBestReligion;
		}
	}
	[B][COLOR="Red"]return NO_RELIGION[/COLOR][/B];
}
[/SIZE]
For Izzy and Islam iPurityPercent == iSpreadPercent == (1*100)/4 == 25, thus AI_bestReligion() returns NO_RELIGION.

Nevertheless she adopts Theocracy, because CvPlayerAI::AI_civicValue will dictate her to go for her favorite civic:

Code:
[SIZE="3"]if (GC.getLeaderHeadInfo(getPersonalityType()).getFavoriteCivic() == eCivic)
{
	if (!kCivic.isStateReligion() || iHighestReligionCount > 0)
	{
		iValue *= 5; 
		iValue /= 4; 
		iValue += 6 * getNumCities();
		iValue += 20; 
	}
}[/SIZE]

The problem here is that iHighestReligionCount = findHighestHasReligionCount() is 3 for Taoism so that Theocracy gets pushed, although she doesn't have Taoism as her state religion.
A better test might be
if (!kCivic.isStateReligion() || getStateReligion() != NO_RELIGION)
...

There are more checks for iHighestReligionCount in AI_civicValue (note, kCivic.isStateReligion() is false for Free Religion):
Code:
[SIZE="3"]iValue += (kCivic.getNonStateReligionHappiness() * (iTotalReligonCount - iHighestReligionCount) * 5);

if (kCivic.isStateReligion())
{
	if (iHighestReligionCount > 0)
	{
		iValue += iHighestReligionCount;

		iValue += ((kCivic.isNoNonStateReligionSpread()) ? ((getNumCities() - iHighestReligionCount) * 2) : 0);
		iValue += (kCivic.getStateReligionHappiness() * iHighestReligionCount * 4);
		iValue += ((kCivic.getStateReligionGreatPeopleRateModifier() * iHighestReligionCount) / 20);
		iValue += (kCivic.getStateReligionGreatPeopleRateModifier() / 4);
		iValue += ((kCivic.getStateReligionUnitProductionModifier() * iHighestReligionCount) / 4);
		iValue += ((kCivic.getStateReligionBuildingProductionModifier() * iHighestReligionCount) / 3);
		iValue += (kCivic.getStateReligionFreeExperience() * iHighestReligionCount * ((bWarPlan) ? 6 : 2));
	}
}[/SIZE]
The algorithm appears to search the best civic for the most widespread religion (highest number of cities with this faith, independent of size of those cities), no matter whether this religion is the actual state religion or not. IMHO iHighestReligionCount should be replaced by a iStateReligionCount, so that the civic follows the state religion, because the SR has a higher impact on the AI's success in the game due to diplomacy.

The whole problem is not restricted or directly connected to colonies, but the usual situation in the game when colonies are created makes its occurrence more likely (globally widespread religions, new player with a few small cities).

For tests I have attached a save where Hannibal will create a colony (Izzy) after you press <End Turn>. The dominant religion is Buddhism, Hannibal is currently without a SR but will convert to Islam after splitting his empire, because after that he will have Islam in 100% of his 3 cities (which are big enough). Izzy has 2 cities with Judaism and one with Buddhism and Islam each (all size 1) and will adopt Theo but stay without a SR as her best religion is Buddhism but it is not pure enough. After increasing the size of one of her Jewish cities via WB to 5 she will convert to Judaism (Judaism is the best and iPurityPercent is 2/4 = 50%)
With the change (if (!kCivic.isStateReligion() || getStateReligion() != NO_RELIGION)) she will adopt pacifism instead (when without a SR), probably because it is the best civic due to no upkeep. BTW the AI always handles civics before religions.
 

Attachments

  • DanF_ReliTest AD-1300.CivBeyondSwordSave
    39.9 KB · Views: 115
Nice work once again Dan. :goodjob:

While changing the civic value check for the favorite Theocracy to
Code:
if (!kCivic.isStateReligion() || getStateReligion() != NO_RELIGION)
does nicely handle the problem case where she's not going to pick a religion, it then causes a new issue when she does choose a religion.

For example, taking your save and upping the pop of a jewish city to 5 so that she'll pick Judaism, if we use that new check, she'll still adopt Pacifism even though she subsequently chooses a state religion (Judaism.) While this situation is arguably still better than the current situation, it essentially nullifies any "favorite civic" preference for religious civics and so it doesn't completely fix things from my point of view.

It's obvious that the designers intended to give favorite civics a higher weight and so a solution that still honors that would be best. Since civics are always picked first, perhaps we should be looking at changing AI_bestReligion() so that it takes into account the current civic choice since right now it completely ignores civics.

The tricky part is evaluating which civics are still useful without a state religion and which aren't. For example, Pacifism is still potentially useful because of no upkeep, OR is still potentially useful because of the ability to train missionaries without a monastery, but Theocracy is not useful because it has meidum upkeep and all its benefits require state religion.
 
Religion in BtS is really a can of worms .......

i don't see how you can solve this besides making a evaluation of how good the civic will be for the civ. It surely needs to take in account civic maintenance (easy to get ), but how to evaluate OR missionary or the FR :) bonus?
 
Religion in BtS is really a can of worms .......
Yeah, alot of the stuff we try to fix seems to open up a can of worms...

i don't see how you can solve this besides making a evaluation of how good the civic will be for the civ. It surely needs to take in account civic maintenance (easy to get ), but how to evaluate OR missionary or the FR :) bonus?
There's also a bit of chicken-and-egg problem here. The civic valuation should include religion in its determination (it does, sorta, as shown by Dan above), and the religion valuation should include civics (right now it does not). Frankly, both of them should also consider diplomatic implications too but we'd probably be drowning in worms if we go that route...



Here's another way to look at it. AI_civicValue uses the following as its base when determining the value of all religious civics:
Code:
	iHighestReligionCount = findHighestHasReligionCount();
The primary problem, therefore, is that it is basing its decision on the most prevalent religion, but the religion chooser instead looks at the "best" religion. What if, instead, the civic valuator used the following:
Code:
	iHighestReligionCount = getHasReligionCount(AI_bestReligion());
That would mean that the religion chooser and the civic valuator would be on the same page in terms of their preferred religion and so we should avoid the current problem situation. (The favorite civic if check is then restored to the original since it's now considering the preferred religion.) Here are some results with Dan's test save:

1) Original Situation (no religious preference):
Izzy chooses Free Religion and obviously No State Religion

2) Pumping pop of Jewish city to make Judaism the preference:
Izzy chooses Theocracy and Judaism

What does everyone think of that idea?

EDIT: Note that while the above does actually seem to work, it's a little sloppy since we shouldn't call getHasReligionCount() with an argument of NO_RELIGION. The following seems a much better idea; test results are the same as above:
Code:
	ReligionTypes eBestReligion = AI_bestReligion();
	iHighestReligionCount = ((eBestReligion == NO_RELIGION) ? 0 : getHasReligionCount(eBestReligion));
 
When I went to bed last night, the last thing I thought was: iHighestReligionCount = getHasReligionCount(AI_bestReligion()) is how it should be done.
Nice to see you suggest it Dresden.
But this actually represents two changes:
1. Switch from most prevalent religion to state religion for the determination of civic values.
2. Assume that the AI can and will convert to the best religion if it does not have it as its SR already.

I already agreed to 1. but I'm not 100% sure about 2 because of uncertainties like the conversion timer and other reasons which might prevent a conversion.
For instance in CvPlayerAI::AI_doReligion() there is this part:
Code:
if (eBestReligion == NO_RELIGION)
{
	eBestReligion = getStateReligion();
}
which means that Izzy would never switch out of Judaism again if say Confucianism spread to her 5th city so that Judaism is not pure enough anymore and NO_RELIGION would be her best religion again. If she doesn't know Liberalism for Free Religion she switches to OR (wins vs Pacifism because of the Missionaries-without-Monastery-bonus [btw the check does not consider the knowledge of ScM]) while staying in Judaism = she does something that "goes against everything she stands for!" ;).
So I think we need more checks to allow a civic switch that would benefit from an anticipated (future) conversion:
Code:
ReligionTypes eBestReligion = AI_bestReligion();
if (getStateReligion() != eBestReligion)      // we would like to convert
{
	if (!canConvert(eBestReligion) || eBestReligion == NO_RELIGION)  // we are not allowed to convert
	{
		eBestReligion = getStateReligion();   
	}
}
iHighestReligionCount = ((eBestReligion == NO_RELIGION) ? 0 : getHasReligionCount(eBestReligion));
was my first version, but canConvert always returns false if the AI is in Free Religion (because then isStateReligion is false). I guess this is meant to prohibit "hidden" conversions to a different faith while in FR.
Since this would lock Izzy in FR even for circumstances when she would like to convert to Judaism + Theo again, I dropped that part:
Code:
if (getStateReligion() != eBestReligion && eBestReligion == NO_RELIGION)       // we would like to convert
{
	eBestReligion = getStateReligion();   
}
Now there might be periods of Theo + no SR again when the conversion timer doesn't allow an instant conversion. BTW why are there 2 different timers for CvPlayer and CvPlayerAI (both for civics and religions)?
I'm afraid this needs some more testing...
 
For instance in CvPlayerAI::AI_doReligion() there is this part:
Code:
if (eBestReligion == NO_RELIGION)
{
	eBestReligion = getStateReligion();
}
which means that Izzy would never switch out of Judaism again if say Confucianism spread to her 5th city so that Judaism is not pure enough anymore and NO_RELIGION would be her best religion again.
Is this actually a bad thing? If Izzy decides that Free Religion is her best civic, she will be forced out of her state religion regardless. If, however, she is running something other than FR, there is no benefit to dropping the State Religion even if NO_RELIGION is suddenly determined to be the best. Switching from Judaism to NSR while running OR/Theo/Pac would only ever be beneficial for diplomacy reasons; if you ignore diplomacy, staying in the state religion will always be better than running one of those civics without a state religion. And since the AI doesn't consider diplomacy in its choice of religion (and changing that would be beyond the scope of this patch) the rule makes perfect sense to me.

She's also not completely trapped in Judaism forever with the current AI_doReligion() rules. Here is an example based on the test save.

Izzy starts out with the following cities:
  • Braga (1) No religions
  • Faro (1) Bu
  • Leiria (1) Is
  • Lagos (1) Jw
  • Evora (5) Jw
She chooses Theocracy/Judaism as discussed previously.

Now, Hannibal and Joao start giving her loads of missionaries which she uses (a whole other issue we're ignoring) and she also founds another city resulting in the following situation:
  • Braga (2) Bu, Cn, Tao
  • Faro (2) Bu, Cn, Tao
  • Leiria (4) Is, Bu, Cn, Tao
  • Lagos (2) Jw, Cn, Tao
  • Evora (6) Jw, Cn, Tao
  • Madrid (1) Is
At this point, Izzy determines Free Religion is in her best interests (value is 148 and crushes everything else which has a value in the 30s and 40s) and so she revolts. Free Religion, by design, forces her into No State Religion as well.

But then there is war from Joao. Joao captures the 3 Southern cities and Buddhism spreads (with Hannibal's help) to Madrid putting Izzy in this situation:
  • Braga (3) Bu, Cn, Tao
  • Faro (3) Bu, Cn, Tao
  • Madrid (2) Is, Bu
At this point, Buddhism is the preferred religion and Theocracy the highest valued civic (111 vs FR's 57 thanks, in part, to being at war), resulting in a switch to Theo/Budd.

==========================================

While interesting, the above is really just a diversion. The key is to recognize that AI_bestReligion() isn't always the preferred state religion because of the above mentioned check so let's get back on task.

So I think we need more checks to allow a civic switch that would benefit from an anticipated (future) conversion:
...
Code:
if (getStateReligion() != eBestReligion && eBestReligion == NO_RELIGION)       // we would like to convert
{
	eBestReligion = getStateReligion();   
}
Since our goal is to make the civic valuator base its calculations on the (most likely) state religion, and do_Religion has this special case, this seems a reasonable improvement. :thumbsup: I will switch the order of the if check so that we don't bother calling getStateReligion() unless it's necessary, but that's just an optimization issue and doesn't alter the logic.

When testing using the same procedure as my above example, the primary difference when making this change seems to be that an AI is less likely to switch to Free Religion than they would be using the previous suggestion. This is probably better since it would be closer to the existing game behavior in that regard and avoiding unintended consequences is a good thing.

During the previous test, at checkpoint 2, the value of Free Religion was nearly 4 times the value of any other civic. This was due to the fact that the civic valuator was assuming a switch to NO_RELIGION. However, as Dan showed, the AI wouldn't voluntarily choose NO_RELIGION and would instead stay in its current religion and so now Free Religion's value has gone down slightly to 138 (the happiness increase is smaller) and Theocracy's value is up to 128 since it'd actually have useful benefits. OR and Pac have also doubled in value though they're still non-factors. As a result, Izzy still revolts to FR, but it's a much closer call now.

At checkpoint 3, since the best religion is Buddhism, it's exactly the same as before. Theo (111) is vastly superior to FR (57), so she goes to Theo/Budd. Obviously this needs testing (everything always does) but I think this will do the job. For clarity the full current proposal follows:
Code:
	ReligionTypes eBestReligion = AI_bestReligion();
	if ( (eBestReligion == NO_RELIGION) && (getStateReligion() != eBestReligion) )
	{
		eBestReligion = getStateReligion();   
	}
	iHighestReligionCount = ((eBestReligion == NO_RELIGION) ? 0 : getHasReligionCount(eBestReligion));

===========================================

Now there might be periods of Theo + no SR again when the conversion timer doesn't allow an instant conversion. BTW why are there 2 different timers for CvPlayer and CvPlayerAI (both for civics and religions)?
Small periods of questionable civic/religion choices due to having to wait for the timer are unavoidable and have never been a problem; as long as the long-term issue is addressed then we've done our job. Regarding the timer difference, it looks like the AI has a little extra delay built-in to prevent a non-spiritual civ from changing its civics and religions so often that it spends half its time in Anarchy.
 
Is this actually a bad thing? If Izzy decides that Free Religion is her best civic, she will be forced out of her state religion regardless. If, however, she is running something other than FR, there is no benefit to dropping the State Religion even if NO_RELIGION is suddenly determined to be the best. Switching from Judaism to NSR while running OR/Theo/Pac would only ever be beneficial for diplomacy reasons; if you ignore diplomacy, staying in the state religion will always be better than running one of those civics without a state religion. And since the AI doesn't consider diplomacy in its choice of religion (and changing that would be beyond the scope of this patch) the rule makes perfect sense to me.
I don't object the ebestReli==NO_RELI-check in AI_doReligion at all. I just saw that this would lead to a discrepancy between the anticipated religion of the civic valuator and the actual future religion, so that the civic valuator assumes NO_RELIGION and therefore neglects the favorite civic bonus for Theo and returns OR as the best NO_RELIGION civic (when she doesn't know Liberalism for FR). But then AI_doReligion doesn't allow Izzy to follow the civic valuator's plan and she must stay in Judaism, thus creating the unwanted OR+Judaism combination ("which goes against everything...").

Optimization is good and something I haven't really thought about yet. So according to "A designer knows he has achieved ..." I believe
Code:
ReligionTypes eBestReligion = AI_bestReligion();
if (eBestReligion == NO_RELIGION)
{
	eBestReligion = getStateReligion();
}
iHighestReligionCount = ((eBestReligion == NO_RELIGION) ? 0 : getHasReligionCount(eBestReligion));
should do the trick.

My previous inclusion of canConvert was supposed to be the conservative no-risk solution, but unfortunately doesn't work due to the no-conversion-in-FR-rule. Looking at what canConvert actually does check now, I can only think of one risk apart from the conversion timer issue:
Suppose a city with a religion is conquered by the Barbarians, then the barbs' civic valuator will anticipate a conversion and weight the civics accordingly (OR or Pacifism will win). But barbs are not allowed to convert, so that provided they know Liberalism FR should always come out as the best religious civic. Hmmm.... I suppose most CIV players could live with that "problem" :).
 
Hmmm...I posted to this thread and then lost power for a week...oh well...

If anyone experiences this, please get us a save from the turn before the colony makes all its civics changes (or even the turn before creation) so that we can hopefully see why a religion is not chosen. Unfortunately on ALC 24, the closest save Sisiutil had was from 50 turns before the colony was created and when I "forced" colony creation (Worlbuildered some cities on the same landmass for the parent civ) the colony did spawn, but it also properly chose a religion...

Sorry I didn't get a save from my earlier game...it didn't occur to me that it would be that much of a problem. Nevertheless, you seem to have a handle on why it arises out of the code.

The central problem in games where I've seen it happen is that the 'natural' progression for a civ that doesn't get a religion to naturally spread to most of its cities is to 1)go to OR & spam missionaries then, 2)switch to theocracy (if you prefer that civic). This is the only sensible option if monastaries are obsolete. If an AI first gets religion when it already has the option for theo (because it was previously isolated, or because its a new colony), it bypasses the first 2 steps.

It may be beyond the scope of what you're doing, but if you could code a '2-step' conversion that could kick in sometimes, that would create an AI that played smarter, while still being 'in character'. This would be something that would activate in the no religion/theo situations, but also when an AI wanted to switch to a religion without many cities of that faith and few of the necessary monastaries. It would also get activated more often for spiritual civs.

A character could still run theo 97% of the time, but going to OR that other 3% of the time makes that 97% of the time in theo much more effective.
 
Top Bottom