Some difficult, for me, code and mysterious functions

Craig_Sutter

Deity
Joined
Aug 13, 2002
Messages
2,773
Location
Calgary, Canada
The following code is supposed to determine if a city converts to a religion, and if a majority of a city states cities have that religion, convert all of them...

I am using pPlayer:HasOthersReligionInMostCities(eReligion)... I have also tried pPlayer:HasReligionInMostCities(eReligion).

I think they may not do what I think they do... if not, I need to figure out a way to determine when the majority of a civilization's cities have converted to a certain religion.

The following code is what I am using... I'm not great at lua, but I want the conversion to happen only once per civilization. I hope I have gotten that right.

Anyhow, no errors show up in the following code in my logs... however, the function does not kick in when 2 or 3 of a minor civ have converted to Catholicisim (which is what I wish to occur).

Anyone know if the above functions are the ones I need to do the job, or is there an error in my coding that is giving me a null value?

Thanks.

Code:
-- converts minor civilization to Catholicism is enough cities are Catholic

local modData = Modding.OpenSaveData()
local modSussexCatholicKey = "SussexCatholic"
local haveSussexCatholic = (modData.GetValue(modSussexCatholicKey) == 1)

function MinorConvert(iPlayer, eReligion, iX, iY)

	if eReligion == GameInfoTypes["RELIGION_CHRISTIANITY"] then
	local pPlayer = Players[iPlayer];
	local civType = pPlayer:GetMinorCivType();
		
		if civType == GameInfo.MinorCivilizations["CIVILIZATION_SUSSEX"].ID then

			if (pPlayer:HasOthersReligionInMostCities(eReligion)) then
			
				if (civType == GameInfo.MinorCivilizations["CIVILIZATION_SUSSEX"].ID) then

					if (haveSussexCatholic == false) then

						for pCity in pPlayer:Cities() do

						-- City exists?

    						if pCity ~= nil then

							pCity:ConvertPercentFollowers(1, -1, 75);
							print(pCity:GetName(), "...is getting Catholic followers...");
				
							return pCity							
							end
						end

					haveSussexCatholic = true
					modData.SetValue(modSussexCatholicKey, 1)
				
					end		
			
				end
			end
		end
	end
end	
			    
	

GameEvents.CityConvertsReligion.Add (MinorConvert);
 
pPlayer:HasOthersReligionInMostCities() takes a player id as a parameter, not a religion id

so
Code:
Players[iPlayerFrance]:HasOthersReligionInMostCities(iPlayerEngland)
and not
Code:
Players[iPlayerFrance]:HasOthersReligionInMostCities(iReligionCricket)
 
Just curious: what religion is being referenced in the post above? Is it the religion founded by iPlayerEngland? Or is it the religion that is found in most of iPlayerEngland's cities?
 
Code:
/// Is this player happily following this other player's religion?
bool CvPlayerReligions::HasOthersReligionInMostCities(PlayerTypes eOtherPlayer) const {
  // Not happy about it if have their own religion
  if HasCreatedReligion() return false;

  // Count all our cities following the other players religion
  ReligionTypes eOtherReligion = GC.getGame().GetGameReligions()->GetReligionCreatedByPlayer(eOtherPlayer);
  int iNumFollowingCities = ...;

  // Over half?
  return (iNumFollowingCities * 2 > m_pPlayer->getNumCities());
}
 
Thanks. That clarifies a few things for me.

So a "player's religion" always refers to the religion founded by that player, regardless of what their cities actually follow. And HasOthersReligionInMostCities always returns false if the calling player has founded a religion.

In a mod, it's possible for one player to found >1 religion. It seems to work just fine after reworking the religion overview panel. But I have to avoid Lua methods that assume only 1 founded religion per civ (which will either crash or give me the wrong answer ... I haven't tested which of those two possibilities).
 
GC.getGame().GetGameReligions()->GetReligionCreatedByPlayer() will never crash, just return the FIRST religion created by a player
 
What about pPlayer:HasReligionInMostCities. That also does not work in my code. Does it likewise act differently than expected?

Depends how you're expecting it to act ;)

The only documentation for that one is in the G&K scenario and it does what I would expect from looking at that lua file - returns true if more than half of the player's cities are following the specified religion
 
Thanks for all the information. Got my function working, at least for one civ, using pPlayer:HasReligionInMostCities.

Now I have to figure out how to do it for all city states. However, I'm wondering if I really need to do the whole "Modding.OpenSaveData()" thing as presumably, if the function works, all the civs cities will be converted to Catholicism anyhow and the function won't kick in after that in any case (I'm okay with the odd case where a Catholic city is unconverted (sic) and the function kicks in again when it is reconverted).
 
Back
Top Bottom