Is this code correct for detecting if a civ has a major religion?

Rob (R8XFT)

Ancient Briton
Retired Moderator
Joined
Aug 11, 2002
Messages
10,866
Location
Leeds (UK)
I'm adding some events to Sukritact's events mod, to fit with Anno Domini. One event involves gaining a free missionary as one of the possible outcomes, and it struck me that I'd better check if the civ had a major religion first. Is this code correct?

Code:
local iReligion = GetPlayerMajorityReligion(pPlayer)
		if iReligion == nil then return false, false end
		end


Just in case you need to see it in context with the rest of the code:

Code:
--------------------------------------------------------------------------------------------------------------------------
-- Religious man
--------------------------------------------------------------------------------------------------------------------------
local Event_ReligiousMan = {}
	Event_ReligiousMan.Name = "TXT_KEY_EVENT_RELIGIOUSMAN"
	Event_ReligiousMan.Desc = "TXT_KEY_EVENT_RELIGIOUSMAN_DESC"
	Event_ReligiousMan.Weight = 5
	Event_ReligiousMan.CanFunc = (
	local iReligion = GetPlayerMajorityReligion(pPlayer)
		if iReligion == nil then return false, false end
		end
		)
	Event_ReligiousMan.Outcomes = {}
	--=========================================================
	-- Outcome 1
	--=========================================================
	Event_ReligiousMan.Outcomes[1] = {}
	Event_ReligiousMan.Outcomes[1].Name = "TXT_KEY_EVENT_RELIGIOUSMAN_OUTCOME_1"
	Event_ReligiousMan.Outcomes[1].Desc = "TXT_KEY_EVENT_RELIGIOUSMAN_OUTCOME_RESULT_1"
	Event_ReligiousMan.Outcomes[1].CanFunc = (
		function(pPlayer)
			local iReward = math.ceil(20 * iMod * (pPlayer:GetCurrentEra() + 1))
			Event_ReligiousMan.Outcomes[1].Desc = Locale.ConvertTextKey("TXT_KEY_EVENT_RELIGIOUSMAN_OUTCOME_RESULT_1", iReward)
			return true
		end
		)
	Event_ReligiousMan.Outcomes[1].DoFunc = (
		function(pPlayer)
			local iReward = math.ceil(20 * iMod * (pPlayer:GetCurrentEra() + 1))
			pPlayer:ChangeFaith(iReward)
			JFD_SendNotification(pPlayer:GetID(), "NOTIFICATION_GENERIC", Locale.ConvertTextKey("TXT_KEY_EVENT_RELIGIOUSMAN_OUTCOME_1_NOTIFICATION"), Locale.ConvertTextKey	(Event_ReligiousMan.Name))
		end
		)
	--=========================================================
	-- Outcome 2
	--=========================================================
	Event_ReligiousMan.Outcomes[2] = {}
	Event_ReligiousMan.Outcomes[2].Name = "TXT_KEY_EVENT_RELIGIOUSMAN_OUTCOME_2"
	Event_ReligiousMan.Outcomes[2].Desc = "TXT_KEY_EVENT_RELIGIOUSMAN_OUTCOME_RESULT_2"
	Event_ReligiousMan.Outcomes[2].CanFunc = (
		function(pPlayer)			
			local iGoldCost = math.ceil(98 * iMod)
			if pPlayer:GetGold() < iGoldCost then return false end
			
			Event_ReligiousMan.Outcomes[2].Desc = Locale.ConvertTextKey("TXT_KEY_EVENT_RELIGIOUSMAN_OUTCOME_RESULT_2", iGoldCost)
			return true
		end
		)
	[2].DoFunc = (
		function(pPlayer) 
			local iGoldCost = math.ceil(98 * iMod)
			pPlayer:ChangeGold(-iGoldCost)
			pPlayer:AddFreeUnit(GameInfoTypes[JFD_GetUniqueUnit(pPlayer, "UNITCLASS_MISSIONARY")], GameInfoTypes["UNIT_MISSIONARY"]) 
			
			JFD_SendNotification(pPlayer:GetID(), "NOTIFICATION_GENERIC", Locale.ConvertTextKey("TXT_KEY_EVENT_RELIGIOUSMAN_OUTCOME_2_NOTIFICATION"), Locale.ConvertTextKey("TXT_KEY_EVENT_RELIGIOUSMAN"))
		end)
	--=========================================================
	-- Outcome 3
	--=========================================================
	Event_ReligiousMan.Outcomes[3] = {}
	Event_ReligiousMan.Outcomes[3].Name = "TXT_KEY_EVENT_RELIGIOUSMAN_OUTCOME_3"
	Event_ReligiousMan.Outcomes[3].Desc = "TXT_KEY_EVENT_RELIGIOUSMAN_OUTCOME_RESULT_3"
	Event_ReligiousMan.Outcomes[3].CanFunc = (
		function(pPlayer)			
			local iGoldenAgeCost = math.ceil(33 * iMod)
			local iGoldReward = math.ceil(52 * iMod)
			if pPlayer:GetGoldenAgeProgressMeter() < iGoldenAgeCost then return false end
			
			Event_ReligiousMan.Outcomes[3].Desc = Locale.ConvertTextKey("TXT_KEY_EVENT_RELIGIOUSMAN_OUTCOME_RESULT_3", iGoldenAgeCost, iGoldReward)
			return true
		end
		)
	Event_ReligiousMan.Outcomes[3].DoFunc = (
		function(pPlayer)
			local iGoldenAgeCost = math.ceil(33 * iMod)
			local iGoldReward = math.ceil(52 * iMod)
			pPlayer:ChangeGoldenAgeProgressMeter(-iGoldenAgeCost)
			pPlayer:ChangeGold(iGoldReward) 
			
			JFD_SendNotification(pPlayer:GetID(), "NOTIFICATION_GENERIC", Locale.ConvertTextKey("TXT_KEY_EVENT_RELIGIOUSMAN_OUTCOME_3_NOTIFICATION"), Locale.ConvertTextKey("TXT_KEY_EVENT_RELIGIOUSMAN"))
		end)
	
tEvents.Event_ReligiousMan = Event_ReligiousMan
 
Code:
local iReligion = GetPlayerMajorityReligion(pPlayer)
That will depend entirely on what the (non-core) GetPlayerMajorityReligion() function has been defined to do, assuming it's been defined at all.

However, you can easily find out if a player has created a religion via

Code:
if (pPlayer:HasCreatedReligion()) then
  -- The player has created a religion
end

Note that religions do not include pantheons, if you want to check for the player having created a pantheon, use pPlayer:HasCreatedPantheon(), but then note that pantheons are included in religions, so if you need to cover all bases

Code:
if (pPlayer:HasCreatedReligion()) then
  -- The player has created a religion
elseif (pPlayer:HasCreatedPantheon()) then
  -- The player has created a pantheon, but not a religion
else
  -- The player has created neither a religion nor a pantheon
end
 
Thanks for the response. I've checked and GetPlayerMajorityReligion is defined as a function in one of the associated lua files. In one of the decisions files associated with Sukritact's mod (which is the mod I'm adding to), it's used to determine if the civ has a religion rather than a pantheon.
 
I believe GetPlayerMajorityReligion is defined in the Renaissance scenario but I'm not familiar with how it calculates its return value.

Presumably if you are giving a player a free Missionary it should be of the religion the player has founded.

It is a bit of a misnomer to ask what "religion a civ has" because "cities have religions", "cities are owned by civs" and "civs found religions" but a religion does not directly belong to a civ.
 
The code itself was throwing an error, stating that there was something invalid following "local", so I've deleted it for now. Thanks for the responses; I do understand what both of you are saying, but lack the know-how to do anything about it unfortunately.
 
Back
Top Bottom