Help with Top Panel displaying Happiness and Unhappiness Luxuries

Harkodos

Warlord
Joined
Feb 9, 2016
Messages
192
Well, after stressing about this code for about a week (read: an estimated 50~60 work hours of repeatedly trying to get this to work), I've had enough and am desperate for help with this problem.

Basically I'm reworking the Happiness and Unhappiness sections to display any varieties of Happiness/Unhappiness that the game can calculate and display (ex. Now Happiness can display for Population, number of Cities, Specialists, etc., and Unhappiness can display for Luxuries, Religions, Local Happiness, Natural Wonders, etc.) But now I've hit a damn infuriating road-block; for what I felt should have taken me a single afternoon has now taken over a week to work out, and is still not finished.

Through my efforts, I've made it so that both Happiness and Unhappiness can display in any combination of Happiness/Unhappiness:
Luxuries
Having a Variety of Luxuries
Extra Happiness/Unhappiness from Luxuries

But the last effort is to allow the Top Panel to display extra Happiness/Unhappiness granted from City-State Luxuries. This is where the infuration lies; I've been working so long on this, trying countless times to get the above ones as well as this current problem to work that my brain is fried (and even coming back to it in full order, as I did a few days ago has proven fruitless).

So before losing my train of thought completely, here's the code I have as of right now:
Code:
        local iResourcesHappiness = pPlayer:GetHappinessFromResources(); -- Happiness
        local iHappinessFromResources = 0; -- Happiness
        local iUnhappinessFromResources = 0; -- Unhappiness
        local iHappinessFromExtraResources = pPlayer:GetHappinessFromResourceVariety(); -- Happiness
        local iExtraLuxuryHappiness = pPlayer:GetExtraHappinessPerLuxury(); -- Happiness
        local iBaseHappinessFromResources = 0; -- Happiness
        local iNumHappinessResources = 0;
        local iBaseUnhappinessFromResources = 0; -- Unhappiness
        local iNumUnhappinessResources = 0;
        local bMinorResourceBonus = false;

        local iMiscHappiness = iResourcesHappiness; -- Happiness
        if (iMiscHappiness * -1 > 0) then
            iMiscHappiness = 0; -- Oops! This value was Negative for some reason! Correcting...
        end
        local iMiscUnhappiness = iResourcesHappiness; -- Unhappiness
        if (iMiscUnhappiness * -1 < 0) then
            iMiscUnhappiness = 0; -- Oops! This value was Positive for some reason! Correcting...
        end

        for resource in GameInfo.Resources() do
            local resourceID = resource.ID;
            local iHappiness = pPlayer:GetHappinessFromLuxury(resourceID);
            if (iHappiness > 0) then
                iNumHappinessResources = iNumHappinessResources + 1;
                iBaseHappinessFromResources = iBaseHappinessFromResources + iHappiness; -- Happiness
            end
            if (iHappiness < 0) then
                iNumUnhappinessResources = iNumUnhappinessResources + 1;
                iBaseUnhappinessFromResources = iBaseUnhappinessFromResources + (iHappiness * -1); -- Unhappiness
            end
        end
        if (iBaseHappinessFromResources > 0) then
            iHappinessFromResources = iHappinessFromResources + iBaseHappinessFromResources; -- Happiness
        end
        if (iBaseUnhappinessFromResources > 0) then
            iUnhappinessFromResources = iUnhappinessFromResources + iBaseUnhappinessFromResources; -- Unhappiness
        end
        if (iHappinessFromExtraResources > 0) then
            iHappinessFromResources = iHappinessFromResources + iHappinessFromExtraResources; -- Happiness
        end
        if (iHappinessFromExtraResources < 0) then
            iUnhappinessFromResources = iUnhappinessFromResources + (iHappinessFromExtraResources * -1); -- Unhappiness
        end
        if (iExtraLuxuryHappiness > 0) then
            iUnhappinessFromResources = iUnhappinessFromResources - (iExtraLuxuryHappiness * iNumUnhappinessResources); -- Happiness
        end
        if (iExtraLuxuryHappiness < 0) then
            iUnhappinessFromResources = iUnhappinessFromResources + ((iExtraLuxuryHappiness * -1) * iNumUnhappinessResources); -- Unhappiness
        end

Everything in the above code box works as intended, but now for the problem code. Pretty much everything in this box is what's giving me trouble; the rest of the code included after this one is only for clarity.
Code:
        for pPolicy in GameInfo.Policies() do
            local iPolicy = pPolicy.ID;

            -- Extra Happiness from gifted Luxury Resources (Global)
            if (pPolicy.MinorResourceBonus == true) then
                if (pPlayer:HasPolicy(iPolicy)) then
                    bMinorResourceBonus = true;
                else
                    bMinorResourceBonus = false;
                end
            end
        end
        if (bMinorResourceBonus == true) then
            if (iExtraLuxuryHappiness > 0) then
                iMiscUnhappiness = iMiscUnhappiness + (iExtraLuxuryHappiness * iNumUnhappinessResources); -- Happiness
            end
            if (iExtraLuxuryHappiness < 0) then
                iMiscHappiness = iMiscHappiness - ((iExtraLuxuryHappiness * -1) * iNumHappinessResources); -- Happiness
            end
            if (iHappinessFromExtraResources > 0) then
                iMiscUnhappiness = iMiscUnhappiness - iHappinessFromExtraResources; -- Unhappiness
            end
            if (iHappinessFromExtraResources < 0) then
                iMiscHappiness = iMiscHappiness + (iHappinessFromExtraResources * -1); -- Happiness
            end
            if (iNumHappinessResources >= 1) then
                iMiscHappiness = iMiscHappiness - iHappinessFromResources + iUnhappinessFromResources; -- Happiness
            end
            if (iNumUnhappinessResources >= 1) then
                iMiscUnhappiness = (iMiscUnhappiness * -1) - iUnhappinessFromResources + iHappinessFromResources; -- Unhappiness
            end
            if (iNumHappinessResources >= 1) then
                if (iMiscHappiness * -1 < 0) then
                    iHappinessFromResources = iHappinessFromResources - (iMiscHappiness * -1); -- Happiness
                end
                if (iMiscHappiness * -1 > 0) then
                    iHappinessFromResources = iHappinessFromResources + iMiscHappiness; -- Happiness
                end
            end
            if (iNumUnhappinessResources >= 1) then
                if (iMiscUnhappiness * -1 < 0) then
                    iUnhappinessFromResources = iUnhappinessFromResources + iMiscUnhappiness; -- Unhappiness
                end
                if (iMiscUnhappiness * -1 > 0) then
                    iUnhappinessFromResources = iUnhappinessFromResources - (iMiscUnhappiness * -1); -- Unhappiness
                    iMiscUnhappiness = iMiscUnhappiness * -1; -- Unhappiness
                end
            end
        end

I believe I had this code working at some point, but as other areas demanded fidling with, I lost that progress days ago, and now can't fathom how to fix it. The following code snippets are just for clarification:
Code:
        if (iHappinessFromResources < 0) then
            iHappinessFromResources = 0; -- Happiness
        end
        if (iUnhappinessFromResources < 0) then
            iUnhappinessFromResources = 0; -- Unhappiness
        end
        if (iHappinessFromResources > 0) then
            iTotalHappiness = iTotalHappiness + iHappinessFromResources; -- Happiness
        end
        if (iUnhappinessFromResources > 0) then
            iTotalUnhappiness = iTotalUnhappiness + iUnhappinessFromResources; -- Unhappiness
        end

And the code to display everything:
Code:
            strText = strText .. "[NEWLINE]  [ICON_BULLET]" .. Locale.ConvertTextKey("TXT_KEY_TP_HAPPINESS_FROM_RESOURCES", iHappinessFromResources);

            -- Individual Resource Info
            for resource in GameInfo.Resources() do
                local resourceID = resource.ID;
                local iHappiness = pPlayer:GetHappinessFromLuxury(resourceID);
                if (iHappiness > 0) then
                    strText = strText .. "[NEWLINE]        [ICON_BULLET]+" .. Locale.ConvertTextKey("TXT_KEY_TP_HAPPINESS_EACH_RESOURCE", iHappiness, resource.IconString, resource.Description);
                end
            end
  
            -- Happiness from Luxury Variety (Global)
            if (iHappinessFromExtraResources > 0) then
                strText = strText .. "[NEWLINE]              [ICON_BULLET]+" .. Locale.ConvertTextKey("TXT_KEY_TP_HAPPINESS_RESOURCE_VARIETY", iHappinessFromExtraResources);
            end
  
            -- Extra Happiness from each Luxury (Global)
            if (iExtraLuxuryHappiness ~= 0) then
                strText = strText .. "[NEWLINE]              [ICON_BULLET]" .. Locale.ConvertTextKey("TXT_KEY_TP_HAPPINESS_EXTRA_PER_RESOURCE", iExtraLuxuryHappiness, iNumHappinessResources);
            end
  
            -- Happiness from City-State Resources (Global)
            if (iCityStateResourceBonusMod ~= 0) then
                if (iMiscHappiness ~= 0) then
                    strText = strText .. "[NEWLINE]              [ICON_BULLET]" .. Locale.ConvertTextKey("TXT_KEY_TP_HAPPINESS_OTHER_SOURCES", iMiscHappiness);

                    if (not OptionsManager.IsNoBasicHelp()) then
                        -- Extra City-State Luxury Happiness from Policies
                        if (iCityStateResourceBonusMod > 0) then
                            strText = strText .. "[NEWLINE]                    [ICON_BULLET]" .. Locale.ConvertTextKey("TXT_KEY_TP_HAPPINESS_CITY_STATE_LUXURY_BONUS", iCityStateResourceBonusMod);
                        end
                    end
                end
            end
...
            strText = strText .. "[NEWLINE]  [ICON_BULLET]" .. Locale.ConvertTextKey("TXT_KEY_TP_UNHAPPINESS_FROM_RESOURCES", iUnhappinessFromResources);

            -- Individual Resource Info
            for resource in GameInfo.Resources() do
                local resourceID = resource.ID;
                local iHappiness = pPlayer:GetHappinessFromLuxury(resourceID);
                if (iHappiness < 0) then
                    strText = strText .. "[NEWLINE]        [ICON_BULLET]+" .. Locale.ConvertTextKey("TXT_KEY_TP_HAPPINESS_EACH_RESOURCE", iHappiness + iExtraLuxuryHappiness, resource.IconString, resource.Description);
                end
            end

            -- Unhappiness from Luxury Variety (Global)
            if (iHappinessFromExtraResources < 0) then
                strText = strText .. "[NEWLINE]              [ICON_BULLET]+" .. Locale.ConvertTextKey("TXT_KEY_TP_HAPPINESS_RESOURCE_VARIETY", iHappinessFromExtraResources);
            end

            -- Extra Unhappiness from each Luxury (Global)
            if (iExtraLuxuryHappiness ~= 0) then
                strText = strText .. "[NEWLINE]              [ICON_BULLET]" .. Locale.ConvertTextKey("TXT_KEY_TP_UNHAPPINESS_EXTRA_PER_RESOURCE", iExtraLuxuryHappiness * -1, iNumUnhappinessResources);
            end

            -- Unhappiness from City-State Resources (Global)
            if (iCityStateResourceBonusMod ~= 0) then
                if (iMiscUnhappiness ~= 0) then
                    strText = strText .. "[NEWLINE]              [ICON_BULLET]" .. Locale.ConvertTextKey("TXT_KEY_TP_UNHAPPINESS_OTHER_SOURCES", iMiscUnhappiness);

                    if (not OptionsManager.IsNoBasicHelp()) then
                        -- Extra City-State Luxury Unhappiness from Policies
                        if (iCityStateResourceBonusMod < 0) then
                            strText = strText .. "[NEWLINE]                    [ICON_BULLET]" .. Locale.ConvertTextKey("TXT_KEY_TP_UNHAPPINESS_CITY_STATE_LUXURY_BONUS", iCityStateResourceBonusMod);
                        end
                    end
                end
            end

Just to clarify how I've been testing this, I've been using the following modified variables (in both Positive and Negative values, I'm just using the Negative here for convenience):
-EDIT-

I've added more definitions to make testing easier for anyone willing to help.
Code:
In GameDefines:
    <Defines>
        <Delete Name="HAPPINESS_PER_EXTRA_LUXURY"/>
        <Row Name="HAPPINESS_PER_EXTRA_LUXURY">
            <Value>-1</Value> -- This will trigger whenever the player has 2 or more Happiness Luxuries (even though it provides Unhappiness)
        </Row>
        <Delete Name="MINOR_POLICY_RESOURCE_HAPPINESS_MULTIPLIER"/>
        <Row Name="MINOR_POLICY_RESOURCE_HAPPINESS_MULTIPLIER">
            <Value>50</Value> -- This value is normally 150. This reduced the amount of Happiness/Unhappiness that a Luxury will provide.
        </Row>
    </Defines>

In Policies:
    <Policies>
        <Update>
            <Where Type="POLICY_PROTECTIONISM"/>
            <Set ExtraHappinessPerLuxury="-2"/> -- This reduces the amount of Happiness and increases the amount of Unhappiness a Luxury can give.
        </Update>
    </Policies>

In Resources:
    <Resources>
        <Update>
            <Where Type="RESOURCE_GOLD"/>
            <Set Happiness="-4"/> -- This makes the Luxuries provide Unhappiness; 2 minimum are required for testing.
        </Update>
        <Update>
            <Where Type="RESOURCE_SILVER"/>
            <Set Happiness="-4"/> -- The two chosen here were arbitrary; any Luxury can be used to test this.
        </Update>
    </Resources>

In Language_en_US:
    <Language_en_US>
        <Delete Tag="TXT_KEY_TP_HAPPINESS_FROM_RESOURCES"/>
        <Row Tag="TXT_KEY_TP_HAPPINESS_FROM_RESOURCES">
            <Text>{1_Num} [ICON_HAPPINESS_1] from having access to the following Luxury Resources:</Text>
        </Row>
        <Delete Tag="TXT_KEY_TP_HAPPINESS_EACH_RESOURCE"/>
        <Row Tag="TXT_KEY_TP_HAPPINESS_EACH_RESOURCE">
            <Text>{1_Num: number "#.##' [ICON_HAPPINESS_1]';#.##' [ICON_HAPPINESS_4]'"} from {2_ResourceIcon} {3_ResourceName:textkey}.</Text>
        </Row>
        <Delete Tag="TXT_KEY_TP_HAPPINESS_RESOURCE_VARIETY"/>
        <Row Tag="TXT_KEY_TP_HAPPINESS_RESOURCE_VARIETY">
            <Text>{1_Num: number "#.##' [ICON_HAPPINESS_1]';#.##' [ICON_HAPPINESS_4]'"} from having a variety of Luxury Resources.</Text>
        </Row>
        <Delete Tag="TXT_KEY_TP_HAPPINESS_EXTRA_PER_RESOURCE"/>
        <Row Tag="TXT_KEY_TP_HAPPINESS_EXTRA_PER_RESOURCE">
            <Text>{1_Num: number "'+'#.##' extra [ICON_HAPPINESS_1]';'-'#.##' less [ICON_HAPPINESS_1]'"} per Luxury Resource ({2_NumResources} total).</Text>
        </Row>
        <Delete Tag="TXT_KEY_TP_HAPPINESS_OTHER_SOURCES"/>
        <Row Tag="TXT_KEY_TP_HAPPINESS_OTHER_SOURCES">
            <Text>{1_Num: number "'+'#.##' extra [ICON_HAPPINESS_1]';'-'#.##' less [ICON_HAPPINESS_1]'"} from Luxury Resources gifted by [ICON_CITY_STATE] City-States.</Text>
        </Row>
        <Delete Tag="TXT_KEY_TP_HAPPINESS_CITY_STATE_LUXURY_BONUS"/>
        <Row Tag="TXT_KEY_TP_HAPPINESS_CITY_STATE_LUXURY_BONUS">
            <Text>{1_Num: number "'+'#.##'% more';'-'#.##'% less'"} [ICON_HAPPINESS_1] from [ICON_CITY_STATE] City-State Resources.</Text>
        </Row>
        <Delete Tag="TXT_KEY_TP_UNHAPPINESS_FROM_RESOURCES"/>
        <Row Tag="TXT_KEY_TP_UNHAPPINESS_FROM_RESOURCES">
            <Text>{1_Num} [ICON_HAPPINESS_4] from having access to the following Luxury Resources:</Text>
        </Row>
        <Delete Tag="TXT_KEY_TP_UNHAPPINESS_EXTRA_PER_RESOURCE"/>
        <Row Tag="TXT_KEY_TP_UNHAPPINESS_EXTRA_PER_RESOURCE">
            <Text>{1_Num: number "'+'#.##' extra [ICON_HAPPINESS_4]';'-'#.##' less [ICON_HAPPINESS_4]'"} per Luxury Resource ({2_NumResources} total).</Text>
        </Row>
        <Delete Tag="TXT_KEY_TP_UNHAPPINESS_OTHER_SOURCES"/>
        <Row Tag="TXT_KEY_TP_UNHAPPINESS_OTHER_SOURCES">
            <Text>{1_Num: number "'-'#.##' less [ICON_HAPPINESS_4]';'+'#.##' extra [ICON_HAPPINESS_4]'"} from Luxury Resources gifted by [ICON_CITY_STATE] City-States.</Text>
        </Row>
        <Delete Tag="TXT_KEY_TP_UNHAPPINESS_CITY_STATE_LUXURY_BONUS"/>
        <Row Tag="TXT_KEY_TP_UNHAPPINESS_CITY_STATE_LUXURY_BONUS">
            <Text>{1_Num: number "'-'#.##'% less';'+'#.##'% more'"} [ICON_HAPPINESS_4] from [ICON_CITY_STATE] City-State Resources.</Text>
        </Row>
    </Language_en_US>

And hopefully that's all the information you'd need to help me with this massive problem I'm having. Any help is much appreciated, as you'd be saving me another week of stress, I'm sure.
 
Last edited:

Harkodos

Warlord
Joined
Feb 9, 2016
Messages
192
-SOLVED!-

After a short break and a perusal of the Civ 5 Modiki, I have discovered a method which allows my code to work perfectly (meaning: no more relying on iResourceHappiness, which was tempermental under these circumstances).

Yes, I was able to get my code to work with the lovely GetResourceFromMinors and GetResourceUsageType methods (in LuaPlayer and LuaGame, respectively).

I know that this was quite involved, but even so, it was disheartening to receive no aid on the matter. Still, a big thanks to all who at least took the time to look it over; even if replies or suggestions were not forthcoming.

But enough of that, I'm just glad to be finished with this malady. Here's hoping great things come of this in the future!
 
Top Bottom