Lua - Making Calculations based on Function Return Value

Genghis.Khan

Person
Joined
Jun 9, 2012
Messages
934
Location
Somewhere
Another boring Lua questions by another annoying person triying to code in Lua, without any experience or knowdlege...

The purpose of this thread is to sucessfully implement a Ciy Tax System, for the development of the Civilization V Modding Project: Rise and Fall of Empires.

You will unterstand what I am attemping to do (or not) by the following pseudocode:

Code:
-- Messy code based on the code provided by Hambil in the "Lua - Adding X Culture and Y Faith from Trade Routes" thread at Civilization Fanatics Forum. Thank you again Hambil

local g_bDebug = true
local LowTaxedCities = {} 
local GoldToAddIfLowTaxes = city:GetPopulation() / 4

function DoTurn(iPlayer)
    pPlayer = Players[iPlayer]
        for pCity in pPlayer:Cities() do
		    pCity.GetPopulation() 
            if (pPlayer:SomehowCreatedFucntionToCheckIfCityHasLowTaxesViaSliderControl(pCity) and not m_LowTaxedCities[pCity]) then
	            m_LowTaxedCities[pCity] = true
                pCity.ChangeBaseYieldRateFromMisc(GameInfoTypes.YIELD_GOLD, GoldToAddIfLowTaxes)
                --pCity.AddFreeBuilding (to add Happiness somehow)
            elseif --repeat conditions for No, Medium, High and Very High Taxes 
then
            m_LowTaxedCities[pCity] = nil
            pCity.ChangeBaseYieldRateFromMisc(GameInfoTypes.YIELD_GOLD, -GoldToAddIfLowTaxes)
            --pCity.RemoveBuilding (to remove Happiness somehow)
        end
    end
end

function P_DoTurn(iPlayer)
	local bSuccess
	local sResult
	bSuccess, sResult = pcall(DoTurn, iPlayer)
	if (not bSuccess) then
		print(sResult)
	end
end

GameEvents.PlayerDoTurn.Add(g_bDebug and DoTurn or P_DoTurn)

Utility for Helping:

Function Modiki Links:
City.GetPopulation - http://modiki.civfanatics.com/index.php/City.GetPopulation_(Civ5_API)
 
Depending on what exactly you want high or low taxes to do you may be able to achieve your design by creating a building. For example, the "low tax" building could have a Building_YieldChangesPerPop of 25 (one gold per 4 citizens) and +2 Happiness while the "high tax" building could have one gold per 2 citizens and no happiness.

With that approach you're lua code would only need to add/remove the proper tax building rather than messing around with changing the city's base yield.

That said, I'm not exactly sure what you are seeking help with. Is there a specific part of the algorithm that you are unsure how to implement?
 
thanks. Yes, a Building should be the best option. My code is just too messy to be understandable. My only problem would be adding happines based on Population, but I think that can be somehow solved. I should learn UI Lua first to implement the Tax slider...
 
Happiness per population would be pretty easy too. There are only two times you would need to adjust the happiness, 1) when the city population changes and 2) when the tax level changes.

A single function (say "UpdateTaxHappiness") can be added to the cityPopulation event (I don't remember the exact name) and whatever user created lua event you throw with your tax UI. The "UpdateTaxHappiness" could than get the city population and create X copies of a building with noLimit that gives +1 local Happiness.
 
Back
Top Bottom