[Lua] I keep failing to properly track friends.

Neirai

the Forgiven
Joined
Aug 5, 2013
Messages
1,049
Location
Canada
For the Colonial Canada mod, I'm trying to place a building in each of Canada's friends' capitals, and a building for each in Canada's capital. So far, it's not working.

Does anyone have any insight into why?

Code:
CapitalCity = nil

function IsCanadaMyAlly(tPlayer)
	local pPlayer = Players[tPlayer]
	local isCanadaMyAlly = false
	for i, eachPlayer in pairs(Players) do
		if eachPlayer:GetCivilizationType() == GameInfoTypes.CIVILIZATION_COLONIALCANADA then
			if pPlayer:IsFriends(eachPlayer:GetID()) then
				print(eachPlayer:GetName() .. " is friends with " .. pPlayer:GetName())
				isCanadaMyAlly = true
			break end
		end
	end
	if isCanadaMyAlly == false then
		return false
	elseif isCanadaMyAlly == true then
		return true
	end
end

function CanadianBrotherhoodOfNations(cPlayer)
	local pPlayer = Players[cPlayer]
	if pPlayer:IsEverAlive() == true and pPlayer:IsMinorCiv() == false then
		if (pPlayer:GetCivilizationType() ~= GameInfoTypes.CIVILIZATION_COLONIALCANADA) and (pPlayer:IsBarbarian() == false) then
			if pPlayer:GetNumCities() > 0 then
				CapitalCity = pPlayer:GetCapitalCity()
				print(IsCanadaMyAlly(cPlayer))
				if IsCanadaMyAlly(cPlayer) then
					if CapitalCity:GetNumBuilding(GameInfoTypes.BUILDING_CANADABROTHERHOODOFNATIONS) > 0 then
						print(pPlayer:GetName() .. " is already an ally of Canada!")
					elseif (CapitalCity:GetNumBuilding(GameInfoTypes.BUILDING_CANADABROTHERHOODOFNATIONS) == 0 or CapitalCity:GetNumBuilding(BUILDING_CANADABROTHERHOODOFNATIONS) == nil) then
						CapitalCity:SetNumRealBuilding(GameInfoTypes.BUILDING_CANADABROTHERHOODOFNATIONS, 1)
						print(pPlayer:GetName() .. " became an ally of Canada!")
					end
				else
					if CapitalCity:GetNumBuilding(GameInfoTypes.BUILDING_CANADABROTHERHOODOFNATIONS) > 0 then
						print(pPlayer:GetName() .. " is no longer an ally of Canada")
						CapitalCity:SetNumRealBuilding(GameInfoTypes.BUILDING_CANADABROTHERHOODOFNATIONS, 0)
					end
				end
			end
		elseif Players[cPlayer]:GetCivilizationType() == GameInfoTypes.CIVILIZATION_COLONIALCANADA then
			print("Canada detected")
			if Players[cPlayer]:GetCapitalCity() ~= nil then
				local cCity = Players[cPlayer]:GetCapitalCity()
				local canadianAllies = 0
				for i, gPlayer in pairs(Players) do
					if gPlayer:IsFriends(Players[cPlayer]:GetID()) then
						canadianAllies = canadianAllies + 1
					end
				end
				print("The number of Allies Canada has is " .. canadianAllies)
				if canadianAllies > 0 then
					cCity:SetNumRealBuilding(GameInfoTypes.BUILDING_CANADABROTHERHOODOFNATIONS, canadianAllies)
				end
			else
				print("Canada has no Capital!")
			end
		end
	end
end
GameEvents.PlayerDoTurn.Add(CanadianBrotherhoodOfNations)

There aren't any issues in the Lua logs.
 
Code:
CapitalCity = nil

function IsCanadaMyAlly(tPlayer)
	for i, eachPlayer in pairs(Players) do
		if eachPlayer:GetCivilizationType() == GameInfoTypes.CIVILIZATION_COLONIALCANADA then
			if eachPlayer:IsDoF(tPlayer) then
				print(eachPlayer:GetName() .. " is friends with " .. Players[tPlayer]:GetName())
				return true
			end
		end
	end
	return false
end
IsDof is used for declaration of friendship.
IsFriends is used for City-States.
Simplified first part.

In second part, you have to do the same.
Hint: Players[cPlayer]:GetID() == cPlayer.
 
Thanks a ton :D

I was using IsDoF before, but then I ran into trouble *cough passing Type instead of ID cough* so I switched to IsFriends.

That is awesome to know.
 
Let me try to understand this so I learn more:

If I have a return true followed by a return false, I only return the first one?
 
Yes.
Return shut down the whole function. Every "active" loop in function will be ended as well (no need for break in this case).

cough passing Type instead of ID cough
?
 
Back
Top Bottom