1. We have added a Gift Upgrades feature that allows you to gift an account upgrade to another member, just in time for the holiday season. You can see the gift option when going to the Account Upgrades screen, or on any user profile screen.
    Dismiss Notice

LUA and Building Yields

Discussion in 'Civ5 - SDK / LUA' started by philhellenismos, Nov 14, 2016.

  1. philhellenismos

    philhellenismos Chieftain

    Joined:
    Nov 17, 2014
    Messages:
    55
    The Wonder in Question

    This is a debug request, regarding a wonder of mine (Asklepieion en tei Koi).

    The problem lies within one of its effects, which is supposed to increase the food and science yields of Holy Sites by 2 each. The effect itself works fine, but the problem lies with it affecting buildings that affect food yields of tiles (e.g. Petra, Lighthouse, so they don't yield food bonuses, only production bonuses and so on). I could simply use a dummy policy, but I chose not to do it since I wanted the effect to stack should a certain civilization of mine acquired two copies of the said wonder.

    Here's the code, which is largely borrowed from DSJHenninger's code if the Lanna LUA comment has anything to say about it.
    Code:
    -- Asklepieion
    -- Author: Mathetesalexandrou
    -- DateCreated: 10/21/2016 9:16:57 PM
    --------------------------------------------------------------
    local iAsklepieion = GameInfoTypes["BUILDING_MATHALX_ASKLEPIEION_EN_TEI_KOI"]
    
    function checkAsklepieion(playerID, klerosX, klerosY, improvementID)
        local kleros = Map.GetPlot(klerosX, klerosY)
        local improvementID = kleros:GetImprovementType()
        local Owner = kleros:GetOwner()
        local agonistes = Players[Owner]
        local asklepieiones = 0
        local pTeam = Teams[agonistes:GetTeam()]
        if agonistes:IsAlive() then
            for asty in agonistes:Cities() do
                asklepieiones = asklepieiones + asty:GetNumBuilding(iAsklepieion)
            end
            if improvementID == GameInfoTypes["IMPROVEMENT_HOLY_SITE"] then
                if kleros:GetYield(YieldTypes.YIELD_FOOD) ~= (kleros:CalculateNatureYield(YieldTypes.YIELD_FOOD, pTeam, false) + kleros:CalculateImprovementYieldChange(kleros:GetImprovementType(), YieldTypes.YIELD_FOOD, agonistes) + 2*asklepieiones) then
                    --First time Holy site enters, so no calculations
                    Game.SetPlotExtraYield(klerosX, klerosY, GameInfoTypes.YIELD_FOOD, 2*asklepieiones)
                end
                if kleros:GetYield(YieldTypes.YIELD_SCIENCE) ~= (kleros:CalculateNatureYield(YieldTypes.YIELD_SCIENCE, pTeam, false) + kleros:CalculateImprovementYieldChange(kleros:GetImprovementType(), YieldTypes.YIELD_SCIENCE, agonistes) + 2*asklepieiones) then
                    Game.SetPlotExtraYield(klerosX, klerosY, GameInfoTypes.YIELD_SCIENCE, 2*asklepieiones)
                end
            end
        end
    end
    GameEvents.BuildFinished.Add(checkAsklepieion)
    
    function checkAsklepieionTurn(playerID)
        local agonistes = Players[playerID]
        local pTeam = Teams[agonistes:GetTeam()]
        if agonistes:IsAlive() then
            local asklepieiones = 0
            for asty in agonistes:Cities() do
                asklepieiones = asklepieiones + asty:GetNumBuilding(iAsklepieion)
            end 
            for asty in agonistes:Cities() do
                for cityPlot = 0, asty:GetNumCityPlots() - 1, 1 do
                    local kleros = asty:GetCityIndexPlot(cityPlot)
                    if kleros:GetOwner() == asty:GetOwner() and kleros:GetPlotCity() == nil then
                        if ((kleros:GetImprovementType() ~= GameInfoTypes["IMPROVEMENT_HOLY_SITE"]) or (kleros:GetImprovementType() == GameInfoTypes["IMPROVEMENT_HOLY_SITE"] and kleros:IsImprovementPillaged())) then
                            --Simple reset: what needs calculating is current yield (includes the wonder yield) - base yield from tile and improvement
                            local diffFood = kleros:GetYield(YieldTypes.YIELD_FOOD) - kleros:CalculateNatureYield(YieldTypes.YIELD_FOOD, pTeam, false) - kleros:CalculateImprovementYieldChange(kleros:GetImprovementType(), YieldTypes.YIELD_FOOD, agonistes)
                            --print("No Holy site. Current Yield: " ..kleros:GetYield(YieldTypes.YIELD_FOOD) ..", Natural Yield: " .. kleros:CalculateNatureYield(YieldTypes.YIELD_FOOD, pTeam, false) .. ", Improvement Yield: " ..kleros:CalculateImprovementYieldChange(kleros:GetImprovementType(), YieldTypes.YIELD_FOOD, agonistes) .. ", Set Variable: " ..diffFood)
                            Game.SetPlotExtraYield(kleros:GetX(), kleros:GetY(), GameInfoTypes.YIELD_FOOD, -diffFood)
                            --Game.SetPlotExtraYield(kleros:GetX(), kleros:GetY(), GameInfoTypes.YIELD_FOOD, 0)
                            local diffScience = kleros:GetYield(YieldTypes.YIELD_SCIENCE) - kleros:CalculateNatureYield(YieldTypes.YIELD_SCIENCE, pTeam, false) - kleros:CalculateImprovementYieldChange(kleros:GetImprovementType(), YieldTypes.YIELD_SCIENCE, agonistes)
                            Game.SetPlotExtraYield(kleros:GetX(), kleros:GetY(), GameInfoTypes.YIELD_SCIENCE, -diffScience)
                            --Game.SetPlotExtraYield(kleros:GetX(), kleros:GetY(), GameInfoTypes.YIELD_SCIENCE, 0)
                        else
                            --Ought value (natural yields + improvement yield (policy+base+tech) + 2 * number of asklepieion wonder (because Kaguya and IGE)) - current yield
                            local diffFood = (2 * asklepieiones) + kleros:CalculateNatureYield(YieldTypes.YIELD_FOOD, pTeam, false) + kleros:CalculateImprovementYieldChange(kleros:GetImprovementType(), YieldTypes.YIELD_FOOD, agonistes) - kleros:GetYield(YieldTypes.YIELD_FOOD)
                            --print("Has found Holy Site. Current Yield: " ..kleros:GetYield(YieldTypes.YIELD_FOOD) ..", Natural Yield: " .. kleros:CalculateNatureYield(YieldTypes.YIELD_FOOD, pTeam, false) .. ", Improvement Yield: " ..kleros:CalculateImprovementYieldChange(kleros:GetImprovementType(), YieldTypes.YIELD_FOOD, agonistes) .. ", Set Variable: " ..diffFood)
                            Game.SetPlotExtraYield(kleros:GetX(), kleros:GetY(), GameInfoTypes.YIELD_FOOD, diffFood)
                            --Game.SetPlotExtraYield(kleros:GetX(), kleros:GetY(), GameInfoTypes.YIELD_FOOD, (2 * asklepieiones))
                            local diffScience = (2 * asklepieiones) + kleros:CalculateNatureYield(YieldTypes.YIELD_SCIENCE, pTeam, false) + kleros:CalculateImprovementYieldChange(kleros:GetImprovementType(), YieldTypes.YIELD_SCIENCE, agonistes) - kleros:GetYield(YieldTypes.YIELD_SCIENCE)
                            Game.SetPlotExtraYield(kleros:GetX(), kleros:GetY(), GameInfoTypes.YIELD_SCIENCE, diffScience)
                            --Game.SetPlotExtraYield(kleros:GetX(), kleros:GetY(), GameInfoTypes.YIELD_SCIENCE, (2 * asklepieiones))
                        end
                    end
                end
            end
        end
    end
    GameEvents.PlayerDoTurn.Add(checkAsklepieionTurn)
    
    Is there a LUA API Method that grabs the yield modifiers to tiles and resources provided by buildings, or must I bruteforce search building class and its effects?
     

Share This Page