How do I check that all elements in a table are a part of a condition?

JFD

Kathigitarkh
Joined
Oct 19, 2010
Messages
9,132
Location
The Kingdom of New Zealand
Basically, I want to check that the player possesses all of the holy cities of the religions listed in a given table. Of course, the way I have it, it will only check for if the player has at least one of the holy cities for one of the religions in the table. I'm not sure how to check for all values. I'm sure the method is simply to someone, so any suggestions would be very helpful. Thanks.

Here's the code, for better clarity:

Code:
function JFD_HasReligiousHolyCities(player, religionType)
   local religions = {}
   if religionType == "RELIGION_CHRISTIANITY" then
      for loopPlayerID = 0, GameDefines.MAX_MAJOR_CIVS-1 do
         local loopPlayer = Players[loopPlayerID]
            if loopPlayer:IsAlive() then
               if loopPlayer:GetReligionCreatedByPlayer() == GameInfoTypes["RELIGION_CHRISTIANITY"] then
		  table.insert(religions, "RELIGION_CHRISTIANITY")
		end
				
		if loopPlayer:GetReligionCreatedByPlayer() == GameInfoTypes["RELIGION_PROTESTANTISM"] then
		   table.insert(religions, "RELIGION_PROTESTANTISM")
		end
				
		if loopPlayer:GetReligionCreatedByPlayer() == GameInfoTypes["RELIGION_ORTHODOXY"] then
		   table.insert(religions, "RELIGION_ORTHODOXY")
		end
				
		if loopPlayer:GetReligionCreatedByPlayer() == GameInfoTypes["RELIGION_CHRISTIAN_ORIENTAL_ORTHODOX"] then
		   table.insert(religions, "RELIGION_CHRISTIAN_ORIENTAL_ORTHODOX")
		end
		
		if (#religions > 0) then
		   for i, v in ipairs(religions) do
			print(v)
			for city in player:Cities() do
				if city:IsHolyCityForReligion(GameInfoTypes[v]) then
				   print("we have all holy cities")
				   return true
end
end						
end		
end
end
end
end
   return
end
 
How about:

Code:
function JFD_HasReligiousHolyCities(player, religionType)
   local religions = {}
   if religionType == "RELIGION_CHRISTIANITY" then
      for loopPlayerID = 0, GameDefines.MAX_MAJOR_CIVS-1 do
         local loopPlayer = Players[loopPlayerID]
            if loopPlayer:IsAlive() then
               if loopPlayer:GetReligionCreatedByPlayer() == GameInfoTypes["RELIGION_CHRISTIANITY"] then
		  table.insert(religions, "RELIGION_CHRISTIANITY")
		end
				
		if loopPlayer:GetReligionCreatedByPlayer() == GameInfoTypes["RELIGION_PROTESTANTISM"] then
		   table.insert(religions, "RELIGION_PROTESTANTISM")
		end
				
		if loopPlayer:GetReligionCreatedByPlayer() == GameInfoTypes["RELIGION_ORTHODOXY"] then
		   table.insert(religions, "RELIGION_ORTHODOXY")
		end
				
		if loopPlayer:GetReligionCreatedByPlayer() == GameInfoTypes["RELIGION_CHRISTIAN_ORIENTAL_ORTHODOX"] then
		   table.insert(religions, "RELIGION_CHRISTIAN_ORIENTAL_ORTHODOX")
		end
              end
             end
                local iPlayerHolyCities = 0;
		if (#religions > 0) then
		   for i, v in ipairs(religions) do
			print(v)
			for city in player:Cities() do
				if city:IsHolyCityForReligion(GameInfoTypes[v]) then
                                   iPlayerHolyCities = iPlayerHolyCities + 1
                                   break
                                end
                        end
                        if iPlayerHolyCities >= #religions then
			   print("we have all holy cities")
			   return true
                        end
                   end
               end
end

Sorry for bad indentation; it's hard to indent properly when typing on a web page.
 
Untested, but something like

Code:
local allRequiredReligions = {
  RELIGION_CHRISTIANITY = {
    GameInfoTypes.RELIGION_CHRISTIANITY, 
    GameInfoTypes.RELIGION_PROTESTANTISM, 
    GameInfoTypes.RELIGION_ORTHODOXY, 
    GameInfoTypes.RELIGION_CHRISTIAN_ORIENTAL_ORTHODOX
  }
}

function JFD_HasReligiousHolyCities(player, religionType)
  local requiredReligions = allRequiredReligions[religionType]
  
  if (requiredReligions) then
    for _, religion in ipairs(requiredReligions) do
      local pHolyCity = Game.GetHolyCityForReligion(religion)

      if (pHolyCity) then
       if (pHolyCity:GetOwner() ~= player) then
         return false
       end
     end
    end
  
    return true
  end
  
  return false
end
 
'fraid not:

Code:
bad argument #2 to 'GetHolyCityForReligion' (number expected, got no value)

Ah, GetHolyCityForReligion wants another parameter. I'll try filling that and see if it solves the issue.

That seems to have done the trick.

Thanks for your sage help! Much appreciated :)
 
local pHolyCity = Game.GetHolyCityForReligion(religion, NO_PLAYER)
 
Back
Top Bottom