Check for # of Diplomats?

OnlyHeStandsHere

Chieftain
Joined
May 4, 2018
Messages
46
Is there a lua method or function that reads the number of diplomats in other Civ's capitals? I've scoured the wiki reference and found one or two things that deal with spies or number of unused spies, but I'm not sure if it distinguishes how they are used.

Any and all help would be appreciated. I am fully-prepared for the answer to simply be that there is none. xD
 
As far as I am aware of, I don't believe there is a direct method that returns the amount of diplomats in a capital nor the amount of spies in a city. It is possible, however to use Player:GetEspionageSpies() to get information on each players' spies, including whether or not they are diplomats as well as the city they're in and tally that up.
 
That could be exactly what I need. Looking at the Player:GetEspionageSpies() method on the reference, it says it uses the SpyInfo table, but each thing in that table says the usage is unknown... Hm...

Would you happen to know which variable would tell me if a spy is a diplomat? My guess would be 'AgentActivity' and I can also guess that the string to check for would be 'diplomat', but I have no actual knowledge to base that on. I'm a little unsure of how to use the table, but I assume I could use Player:GetNumSpies() to check the total number and then loop through each AgentID to count the number of diplomats.
 
The lua api doesn't list it (probably because it's out of date), but one of the table fields of SpyInfo is 'IsDiplomat', a boolean.

Using Player:GetNumSpies() could work, just know that the AgentIDs start at 1 rather than 0. You could also just use the special lua method 'pairs' to loop through the table as well by doing something along the lines of:
Code:
for k, v in pairs(pPlayer:GetEspionageSpies()) do
      --Insert code
end
Where k is the table key, and v is the table value (in this case, each SpyInfo table). This is most useful when you're unsure of what a table's keys are (I don't know if the AgentIDs will always be from 1 to the amount of spies you have).
 
Last edited:
Interesting. So if I'm understanding correctly, I could check for a diplomat with the following:

Code:
for k, v in pairs(pPlayer:GetEspionageSpies()) do
     if v:IsDiplomat then
                      --do a thing (such as add to a counter variable)
     end
end

I'm not sure if the syntax is correct or not. My lua knowledge is essentially nonexistent, so I'm kinda learning as I go along.
 
Thanks for all the help. I was able to get the trait working now! Took a bunch of trial-and-error, it's a little wonky (i.e. it doesn't wait for the diplomats to finish introductions), but it's functional. And I have a couple ideas to refine it later. So for each diplomat that the civ has in another capital, they get an extra delegate in the world congress.

Code:
local iDiplomatCount = 0
local iCurrentCount = 0

function CheckForDiplomats(iPlayer)
    local pPlayer = Players[iPlayer]
    iDiplomatCount = 0
    iCurrentCount = 0
        for k, v in pairs(pPlayer:GetEspionageSpies()) do
            iCurrentCount = iDiplomatCount
            if v.IsDiplomat then
                iDiplomatCount = iCurrentCount + 1
           end
        end
   
    AddDelegates(pPlayer)
end

function AddDelegates(thePlayer)
   
    if (thePlayer:IsAlive()) then
        if (thePlayer:GetCivilizationType() == GameInfoTypes.CIVILIZATION_SWISS) then
            if thePlayer:GetCapitalCity() then
                pCapital = thePlayer:GetCapitalCity()
                if iDiplomatCount == 0 then
                    pCapital:SetNumRealBuilding(GameInfoTypes["BUILDING_SWISS_DELEGATE_01"], 0)
                    pCapital:SetNumRealBuilding(GameInfoTypes["BUILDING_SWISS_DELEGATE_02"], 0)
                    pCapital:SetNumRealBuilding(GameInfoTypes["BUILDING_SWISS_DELEGATE_03"], 0)
                    pCapital:SetNumRealBuilding(GameInfoTypes["BUILDING_SWISS_DELEGATE_04"], 0)
                    pCapital:SetNumRealBuilding(GameInfoTypes["BUILDING_SWISS_DELEGATE_05"], 0)
                    pCapital:SetNumRealBuilding(GameInfoTypes["BUILDING_SWISS_DELEGATE_06"], 0)
                elseif iDiplomatCount == 1 then
                    pCapital:SetNumRealBuilding(GameInfoTypes["BUILDING_SWISS_DELEGATE_02"], 0)
                    pCapital:SetNumRealBuilding(GameInfoTypes["BUILDING_SWISS_DELEGATE_03"], 0)
                    pCapital:SetNumRealBuilding(GameInfoTypes["BUILDING_SWISS_DELEGATE_04"], 0)
                    pCapital:SetNumRealBuilding(GameInfoTypes["BUILDING_SWISS_DELEGATE_05"], 0)
                    pCapital:SetNumRealBuilding(GameInfoTypes["BUILDING_SWISS_DELEGATE_06"], 0)
                    pCapital:SetNumRealBuilding(GameInfoTypes["BUILDING_SWISS_DELEGATE_01"], 1)
                elseif iDiplomatCount == 2 then
                    pCapital:SetNumRealBuilding(GameInfoTypes["BUILDING_SWISS_DELEGATE_01"], 0)
                    pCapital:SetNumRealBuilding(GameInfoTypes["BUILDING_SWISS_DELEGATE_03"], 0)
                    pCapital:SetNumRealBuilding(GameInfoTypes["BUILDING_SWISS_DELEGATE_04"], 0)
                    pCapital:SetNumRealBuilding(GameInfoTypes["BUILDING_SWISS_DELEGATE_05"], 0)
                    pCapital:SetNumRealBuilding(GameInfoTypes["BUILDING_SWISS_DELEGATE_06"], 0)
                    pCapital:SetNumRealBuilding(GameInfoTypes["BUILDING_SWISS_DELEGATE_02"], 1)
                elseif iDiplomatCount == 3 then
                    pCapital:SetNumRealBuilding(GameInfoTypes["BUILDING_SWISS_DELEGATE_01"], 0)
                    pCapital:SetNumRealBuilding(GameInfoTypes["BUILDING_SWISS_DELEGATE_02"], 0)
                    pCapital:SetNumRealBuilding(GameInfoTypes["BUILDING_SWISS_DELEGATE_04"], 0)
                    pCapital:SetNumRealBuilding(GameInfoTypes["BUILDING_SWISS_DELEGATE_05"], 0)
                    pCapital:SetNumRealBuilding(GameInfoTypes["BUILDING_SWISS_DELEGATE_06"], 0)
                    pCapital:SetNumRealBuilding(GameInfoTypes["BUILDING_SWISS_DELEGATE_03"], 1)
                elseif iDiplomatCount == 4 then
                    pCapital:SetNumRealBuilding(GameInfoTypes["BUILDING_SWISS_DELEGATE_01"], 0)
                    pCapital:SetNumRealBuilding(GameInfoTypes["BUILDING_SWISS_DELEGATE_02"], 0)
                    pCapital:SetNumRealBuilding(GameInfoTypes["BUILDING_SWISS_DELEGATE_03"], 0)
                    pCapital:SetNumRealBuilding(GameInfoTypes["BUILDING_SWISS_DELEGATE_05"], 0)
                    pCapital:SetNumRealBuilding(GameInfoTypes["BUILDING_SWISS_DELEGATE_06"], 0)
                    pCapital:SetNumRealBuilding(GameInfoTypes["BUILDING_SWISS_DELEGATE_04"], 1)
                elseif iDiplomatCount == 5 then
                    pCapital:SetNumRealBuilding(GameInfoTypes["BUILDING_SWISS_DELEGATE_01"], 0)
                    pCapital:SetNumRealBuilding(GameInfoTypes["BUILDING_SWISS_DELEGATE_02"], 0)
                    pCapital:SetNumRealBuilding(GameInfoTypes["BUILDING_SWISS_DELEGATE_03"], 0)
                    pCapital:SetNumRealBuilding(GameInfoTypes["BUILDING_SWISS_DELEGATE_04"], 0)
                    pCapital:SetNumRealBuilding(GameInfoTypes["BUILDING_SWISS_DELEGATE_06"], 0)
                    pCapital:SetNumRealBuilding(GameInfoTypes["BUILDING_SWISS_DELEGATE_05"], 1)
                elseif iDiplomatCount == 6 then
                    pCapital:SetNumRealBuilding(GameInfoTypes["BUILDING_SWISS_DELEGATE_01"], 0)
                    pCapital:SetNumRealBuilding(GameInfoTypes["BUILDING_SWISS_DELEGATE_02"], 0)
                    pCapital:SetNumRealBuilding(GameInfoTypes["BUILDING_SWISS_DELEGATE_03"], 0)
                    pCapital:SetNumRealBuilding(GameInfoTypes["BUILDING_SWISS_DELEGATE_04"], 0)
                    pCapital:SetNumRealBuilding(GameInfoTypes["BUILDING_SWISS_DELEGATE_05"], 0)
                    pCapital:SetNumRealBuilding(GameInfoTypes["BUILDING_SWISS_DELEGATE_06"], 1)
                end
            end
        end
    end
end

GameEvents.PlayerDoTurn.Add(CheckForDiplomats)

Not at all pretty, but I'm happy it works at all. Apparently Player.RemoveBuildingClass() wasn't doing what I expected so I manually removed all the buildings each time. xD
 
This conversation was super helpful in figuring out how to give bonuses for a civ's num. diplomats

here is the code I used:
Code:
function CanadianBrotherhoodOfNations(iPlayer)
    local pPlayer = Players[iPlayer]

    if not (pPlayer and (pPlayer:GetCivilizationType() == GameInfoTypes.CIVILIZATION_COLONIALCANADA)) then return end
    
    local iDiplomatCount = 0
    for k, spy in pairs(pPlayer:GetEspionageSpies()) do

        if spy.IsDiplomat and spy.EstablishedSurveillance then
            iDiplomatCount = iDiplomatCount + 1
            print("Canada has a diplomat")
        end
    end      

    local pCapital = pPlayer:GetCapitalCity()
    pCapital:SetNumRealBuilding(GameInfoTypes.BUILDING_CANADABROTHERHOODOFNATIONS, iDiplomatCount)
end  
    
GameEvents.PlayerDoTurn.Add(CanadianBrotherhoodOfNations)
Then you can set that dummy building in the capital to do whatever.

A little slimmer. I also added that the diplomat has to establish surveillance, so a player can't swap all their spies to diplomats for the WC session, then swap them back 1 turn later. That seemed like a human exploit.
 
Back
Top Bottom