How to count how many of a certain building class in the entire world?

Uighur_Caesar

Comandante en Jefe
Joined
Mar 14, 2015
Messages
1,227
Location
Florida
I'm trying to make a civ that gets bonuses for every city it's liberated. My idea was to put a dummy building in the city upon liberation and then check for how many of those buildings exist in the world to apply the bonus. How exactly would I do this?
 
Code:
function CountBuildingsInWorld(iBuilding, iPlayer, bSkipDesignatediPlayer, bCountMinors)
	local iTotalCount = 0
	local iCivsNumber = (bCountMinors and (GameDefines.MAX_PLAYERS - 2) or (GameDefines.MAX_MAJOR_CIVS - 1))
	for iOtherPlayer = 0, iCivsNumber do
		local pOtherPlayer = Players[iOtherPlayer]
		if bSkipDesignatediPlayer then
			if iOtherPlayer ~= iPlayer then
				iTotalCount = iTotalCount + pOtherPlayer:CountNumBuildings(iBuilding)
			end
		else
			iTotalCount = iTotalCount + pOtherPlayer:CountNumBuildings(iBuilding)
		end
	end
	return iTotalCount
end
So in a PlayerDoTurn event you would do something like:
Code:
local iRequiredCiv = GameInfoTypes.CIVILIZATION_YOURCIV
local iBuildingLiberation = GameInfoTypes.BUILDING_LIBERATION

function PlayerTurn(iPlayer)
	local pPlayer = Players[iPlayer]
	if pPlayer:GetCivilizationType() == iRequiredCiv then
		[color="blue"]local iSomething = CountBuildingsInWorld(iBuildingLiberation, iPlayer, true[sup]1[/sup], true[sup]2[/sup])[/color]
		--more stuff here based on what 'iSomething' is calculated as
	end
end



1true if you want to exclude the player# for your civ, or for whichever civ ought to be excluded from the count for some other building, but it only skips the count for whatever # is fed as parameter iPlayer
2true if you want liberated City-States to be counted


-------------------------------------------------------------------------------------------------------------

[edit] JFD's method would be simpler, though does not allow exclusions directly
[edit#2] I had a slight whoopsie in that the Barbs would be accounted for in the "MAX_PLAYERS - 1". It really needs to be "-2" as I edited it to. Though unless playing with one of those crazy-barbs mods, it shouldn't make much difference in this specific case.
 
Thanks guys!

I have another question that I might as well just include here as not to make another thread. Would you happen to be familiar with IsHasPaidTributeTo() and whether it works or not? I was trying to make something so that if you ask a city state for tribute you get influence the turn after to cancel out the influence lost and I currently have this:

Code:
local MinorT = {}
for i, player in pairs(Players) do
	if player:IsEverAlive() then
	if player:IsMinorCiv() then 
			table.insert(MinorT, i)
		end
	end
end

GameEvents.PlayerDoTurn.Add(function(iPlayer)
	if Players[iPlayer]:GetCivilizationType() == polkID then
		TeddyTribute(iPlayer)
	end
end)

function TeddyTribute(iPlayer)
for i, minorID in pairs(MinorT) do
			if Players[minorID]:IsAlive() then
				if Players[minorID]:IsHasPaidTributeTo(iPlayer) then
					if not Teams[Players[iPlayer]:GetTeam()]:IsAtWar(Players[minorID]:GetTeam()) then
						Players[minorID]:ChangeMinorCivFriendshipWithMajor(iPlayer, 15)
					end
				end
			end
		end
	end

I've borrowed from Lastsword's Hittite UA which does something similar except that it's if you can bully the city state, rather than if you've already bullied them. The influence doesn't change when using IsHasPaidTributeTo which leads me to suspect that I'm either doing something wrong or IsHasPaidTributeTo doesn't work at all. I think it might be the latter because if I replace IsHasPaidTributeTo with CanMajorBullyGold or CanMajorBullyUnit it works fine. Any ideas?
 
It is used in UI/InGame/InfoTooltipInclude.lua

Based on where and how it is being used I would not conclude it has anything to do with minor civs but rather with Major Players. It is being used in the section of that file where all the diplo modifiers are being gathered for display as text (such text as is actually displayed, that is, and isn't sneaky-hidden from the human because we want confused and muddled diplomacy in Civ5)
 
Well in that case, how can I remove a diplomatic penalty for bullying a city state? I'm guessing I could use IsHasPlayerBulliedProtectedMinor IsAngryAboutProtectedMinorBullied to find civs that you have a penalty with for bullying, but is it possible to remove this penalty so bullying city states doesn't have negative diplomatic repercussions?
 
Well, there are these 'events'
Code:
  <event name="GetScenarioDiploModifier1" type="Acc">
    <arg pos="1" type="PlayerTypes" name="eFromPlayer"/>
    <arg pos="2" type="PlayerTypes" name="eToPlayer"/>
  </event>
  <event name="GetScenarioDiploModifier2" type="Acc">
    <arg pos="1" type="PlayerTypes" name="eFromPlayer"/>
    <arg pos="2" type="PlayerTypes" name="eToPlayer"/>
  </event>
  <event name="GetScenarioDiploModifier3" type="Acc">
    <arg pos="1" type="PlayerTypes" name="eFromPlayer"/>
    <arg pos="2" type="PlayerTypes" name="eToPlayer"/>
  </event>
Copied from W.Howard's API-as-XML references. But I am not sure you can use them just whenever you want, or only "once", as it were. And the only time I tried to play around with them all I ever seemed to get out of it was an increased or decreased "We want your lands" I think because I was experimenting with #1. Other than a DLL I am not sure there is any way to get the amount of diplo-modifier from CS-bullying, and to offset it.

[edit] I am pretty sure Vice Virtuoso used one of these to adjust starting diplomacy attitudes in one of his Anime-based civs, so the events allow you to return an adjustment integer value to diplomacy 'values' but it isn't as dynamic as one would wish from what I recall of the little experimentation I did.

V.V. used #1 like this:
Code:
GameEvents.GetScenarioDiploModifier1.Add(function(iPlayer1, iPlayer2)
	local pPlayer1 = Players[iPlayer1]
	local pPlayer2 = Players[iPlayer2]
	if (pPlayer1:GetCivilizationType() == iIreland) then
		if (Locale.ConvertTextKey("{@1: gender feminine?yes; other?no}", pPlayer2:GetNameKey()) == "yes") then
			return -30
		end
	elseif (pPlayer2:GetCivilizationType() == iIreland) then
		if (Locale.ConvertTextKey("{@1: gender feminine?yes; other?no}", pPlayer1:GetNameKey()) == "yes") then
			return -30
		end
	else
		return 0
	end
end)
He's looking to see 1st if either of the two players is 'iIreland', and if so is the other of the two leaders shown as having 'Gender:Feminine' in the TXT_KEY tag for the leader-name. If so, then a -30 to diplo-modifier is returned, which means "30" better diplomatic-relations-value with the two players. Negative values mean better relations.
 
Back
Top Bottom