[R&F] [LUA] Religion founded and GPP question

TheLunarArmy

Chieftain
Joined
Jul 9, 2014
Messages
59
Location
South-Africa
Hi folks,

Been looking around for a decent LUA api but doesnt seem to be a well documented collection yet (or at least I cant find it). The closest being the LUA spreadsheet. Anyway, wouldn't mind a hand with the Religion and Great Person Points, any code/hints/slap-in-the-right direction would be appreciated.

Basic idea: Leader Trait that will give Great Prophet Points when a Barb is killed if:
1. No Religion founded
2. A Great Prophet is available
3. Does not have Great Prophet already for the civ
Otherwise it will give Great Artist Points instead.
4. Give corresponding GPP

Here is a pseudo code of how I think it will work, but need some advice on which direction I can pass the 4 steps listed above
Code:
function Give_GreatPeoplePoints(iOwnerPlayerID, iDiedUnitID, iKillerPlayerID, iKillerUnitID)
    local pOwner = Players[iOwnerPlayerID]
    local pPlayer = Players[iKillerPlayerID]
    local pPlayerConfig = PlayerConfigurations[iKillerPlayerID]

    -- Determine if dead unit is Barbarian, and killer is correct Civ has Leader with Trait
    if pOwner.IsBarbarian and HasLeaderTrait(pPlayerConfig:GetLeaderTypeName(), sGeoffTrait) then
        -- (1) and (2) and (3)
       If any false:
              (4) Give Artist Points
       If all true:
              (4) Give Prophet Points
    end

end
Events.UnitKilledInCombat.Add(Give_GreatPeoplePoints)

I have l found some approaches to the problem, but hasnt yielded any reliable results.

1.
• Maybe do a lookup using a query looking into a RequirmentSet containing a RequirementSetRequirements containing REQUIRES_PLAYER_FOUNDED_RELIGION ?
• Using GetReligion():GetMajorityReligion() somehow? Problem is if another civ rushes your civ with missionaries then it will return false.
• Maybe Look if the civ has received the inspiration for Theology civic? (Found a Religion)
2.
• No clue yet (currently investigating)
3
• Grab unit list from player, then iterate and determine if unit type is prophet
4.
• Below seems like the logical solution here
Code:
 pPlayer:GetGreatPeoplePoints():CalculatePointsPerTurn(GreatPersonClass)
-- Math here
pPlayer:GetGreatPeoplePoints():ChangePointsTotal(...)

Edit:
JFD's Rule with Faith (RaF) Has some pretty good Utility lua scripts. function Player_CanRecruitGreatTheologian(player) has some code I will try out

Thanks in Advance! :)
 
Last edited:
After a lot of digging and debugging I was able to do what I intended:

Code:
-- Defines
iGreatProphetPointsMod = 2.0
iGreatArtistPointsMod = 2.0
-- Locals
local sGeoffTrait = "TRAIT_LEADER_TLA_GEOFF_UA"
local GreatProphetClass = GameInfo.GreatPersonClasses["GREAT_PERSON_CLASS_PROPHET"].Index
local GreatArtistClass = GameInfo.GreatPersonClasses["GREAT_PERSON_CLASS_ARTIST"].Index
local GreatProphetType = GameInfo.Units["UNIT_GREAT_PROPHET"].Index
local DistrictHolySiteId = GameInfo.Districts["DISTRICT_HOLY_SITE"].Index

print("hello world 3/27")

function HasLeaderTrait(leaderType, traitType)
    for row in GameInfo.LeaderTraits() do
        if (row.LeaderType == leaderType and row.TraitType == traitType) then return true end
    end
    return false
end

-- Adapted from Player_CanRecruitGreatTheologian, JFD_RwF_MasterUtils.lua, by Deo-e
local maxReligions = GameInfo.Map_GreatPersonClasses[Map.GetMapSize()].MaxWorldInstances

function Player_IsEligibleForGreatProphet(player)
    -- Fetch majority religion in player's cities
    local religion = player:GetReligion()
    if religion:GetReligionInMajorityOfCities() > -1 then -- This returns an ID of the majority religion
        -- Player does have a majority, ensure there are still Religions (hence GPs) available
        local religions = Game.GetReligion():GetReligions()
        local numFoundedReligions = 0
        -- Count the number of religions
        for _, religion in ipairs(religions) do
            -- If at any point the current religion in table is founded by our player then we are not eligible
            if religion.Founder == player:GetID() then return false end
            -- Otherwise increment counter if it is a proper founded religion
            if (not (religion.Pantheon) and Game.GetReligion():HasBeenFounded(religion.Religion)) then
                numFoundedReligions = numFoundedReligions + 1
            end
        end
        -- Determine if the number of existing Religions is eql to/more than the max, hence not eligible
        if numFoundedReligions >= maxReligions then return false end

        -- At this point the player has a majority religion, but it is not theirs
        -- and there are still Religions available, we just need to ensure they are not hiding an existing GP
        local playerUnits = player:GetUnits()
        for _,unit in playerUnits:Members() do
            if unit:GetType() == GreatProphetType then return false end
        end
    end
    -- All contingencies met, we are eligible for a GreatProphet!
    return true
end


function Give_GreatPeoplePoints(iOwnerPlayerID, iDiedUnitID, iKillerPlayerID, iKillerUnitID)
    -- If the killerUnitID is -1 then the killer is a city, which breaks everything, so ignore city kills
    if iKillerUnitID ~= -1 then return end

    local pOwner = Players[iOwnerPlayerID]
    local pPlayer = Players[iKillerPlayerID]
    local pPlayerConfig = PlayerConfigurations[iKillerPlayerID]
    local pKiller = pPlayer:GetUnits():FindID(iKillerUnitID)

    -- Must be Barbarian dying unit and killer must be a Major civ
    if not (pOwner.IsBarbarian and pPlayer.IsMajor) then return end
    -- Must be Leader with Geoff trait controlling the killing unit
    if not (HasLeaderTrait(pPlayerConfig:GetLeaderTypeName(), sGeoffTrait)) then return end
    
    -- Count number of Holy Sites
    local numHolySite = 0
    for _,city in pPlayer:GetCities():Members() do
        local district = city:GetDistricts():GetDistrict(DistrictHolySiteId)
        if district ~= nil and district:IsComplete() then
            numHolySite = numHolySite + 1
        end   
    end

    -- If player has no Holy Sites then do nothing
    if numHolySite == 0 then return end

    -- Determine if Great Prophet Points should be awarded
    if Player_IsEligibleForGreatProphet(pPlayer) then
        -- Get the current number of Great Prophet Points
        local greatProphetPT = pPlayer:GetGreatPeoplePoints():GetPointsTotal(GreatProphetClass)
        -- Add the number of Holy Sites * modifier to that total
        local prophetPointsEarned = iGreatProphetPointsMod * numHolySite
        pPlayer:GetGreatPeoplePoints():SetPointsTotal(GreatProphetClass, greatProphetPT + prophetPointsEarned )

        -- If the player is human give a little UI message
        if pPlayer:IsHuman() then
            Game.AddWorldViewText(
                iKillerPlayerID,
                Locale.Lookup("[COLOR_FLOAT_FAITH]+{1_Num} [ICON_GreatProphet] Great Prophet Points[ENDCOLOR]", prophetPointsEarned),
                pKiller:GetX(),
                pKiller:GetY(),
                0)
        end
    else
        -- Get the current number of Great Artist Points
        local greatArtistPT = pPlayer:GetGreatPeoplePoints():GetPointsTotal(GreatArtistClass)
        -- Add the number of Holy Sites * modifier to that total
        local ArtistPointsEarned = iGreatArtistPointsMod * numHolySite
        pPlayer:GetGreatPeoplePoints():SetPointsTotal(GreatArtistClass, greatArtistPT + ArtistPointsEarned )

        -- If the player is human give a little UI message
        if pPlayer:IsHuman() then
            Game.AddWorldViewText(
                iKillerPlayerID,
                Locale.Lookup("[COLOR_FLOAT_CULTURE]+{1_Num} [ICON_GreatArtist] Great Artist Points[ENDCOLOR]", ArtistPointsEarned),
                pKiller:GetX(),
                pKiller:GetY(),
                0)
        end       
    end
end

Events.UnitKilledInCombat.Add(Give_GreatPeoplePoints)
 
Top Bottom