Rewriting "CulturePerGarrisonedUnit" into "FaithPerGarrisonedUnit"

FieryCharizard7

Chieftain
Joined
Jul 8, 2016
Messages
61
Location
Chicaaago
Hello all,

I'm working on an Italian Wonders modpack, and for one wonder, I want to implement something similar to the "Military Caste" social policy where the empire would gain +1 Faith for every unit garrisoned in a city. I have been trying to find the original LUA code from the game and simply repurpose that code for my needs, but I haven't seen it around so I am guessing it is proprietary.

I figured I would have to use LUA to do this, but I'm still a bit of a beginner with LUA (though I understand other languages like Java, C, and XML).
 
Code:
local iSuperWew = GameInfoTypes.BUILDING_WONDER_OF_THE_WEW
local iNotEnoughLads = GameInfoTypes.BUILDING_DUMMY_LADS
function Wowlads(iPlayer)
    local pPlayer = Players[iPlayer]
    for pCity in pPlayer:Cities() do
        pCity:SetNumRealBuilding(iNotEnoughLads, 0)
        if pPlayer:GetBuildingClassCount(iSuperWew) > 0 then
            if pCity:GetGarrisonedUnit() ~= nil then
                pCity:SetNumRealBuilding(iNotEnoughLads, 1)
            end
        end
    end
end
GameEvents.PlayerDoTurn.Add(Wowlads)
Make dummy building that produce +1 faith and this code should work.
 
Last edited:
Vanilla effects aren't handled in Lua - they're handled in the DLL game engine, which is impractical to edit.

Something similar to Enginseer's function would do as you want - although rather than using a dummy social policy it'd be better to run a function that makes sure the player still owns the wonder, because otherwise the wonder changing hands would make no difference.
 
Code:
local iSuperWew = GameInfoTypes.BUILDING_WONDER_OF_THE_WEW
local iNotEnoughLads = GameInfoTypes.BUILDING_DUMMY_LADS
function Wowlads(iPlayer)
    local pPlayer = Players[iPlayer]
    if pPlayer:GetBuildingClassCount(iSuperWew) > 0 then
        for pCity in pPlayer:Cities() do
            if pCity:GetGarrisonedUnit() ~= nil then
                pCity:SetNumRealBuilding(iNotEnoughLads, 1)
            else
                pCity:SetNumRealBuilding(iNotEnoughLads, 0)
            end
        end
    end
end
GameEvents.PlayerDoTurn.Add(Wowlads)
Make dummy building that produce +1 faith and this code should work.

In the XML, the wonder would have <FreeBulding> BUILDING_WONDER_OF_THE_WEW</FreeBulding> ? So if it were implemented like the CN Tower, how would I remove all of the buildings if the wonder was captured as Chrisy15 said?

Vanilla effects aren't handled in Lua - they're handled in the DLL game engine, which is impractical to edit.

Something similar to Enginseer's function would do as you want - although rather than using a dummy social policy it'd be better to run a function that makes sure the player still owns the wonder, because otherwise the wonder changing hands would make no difference.

So the building would run a function that checks to still see if the wonder is owned by the player as well as if a unit is garrisoned in the city? How would I do this?


Thank you for all of the help
 
Code:
local iSheet = GameInfoTypes.BUILDING_FREE_BUILDING_SOMETHING
function iWOWLADS(oldOwnerID, capital, plotX, plotY, newOwnerID)
    local Oldplayer = Players[newOwnerID]
    local city = Map.GetPlot(plotX, plotY):GetPlotCity()
    if city:IsHasBuilding(iSuperWew) then
       for pCity in Oldplayer:Cities() do
            pCity:SetNumRealBuilding(iSheet, 0)
        end
    end
end
GameEvents.CityCaptureComplete.Add(iWOWLADS)

I'm bored with the variables, you can modify it appropriately.
 
Code:
local iSheet = GameInfoTypes.BUILDING_FREE_BUILDING_SOMETHING
function iWOWLADS(oldOwnerID, capital, plotX, plotY, newOwnerID)
    local Oldplayer = Players[newOwnerID]
    local city = Map.GetPlot(plotX, plotY):GetPlotCity()
    if city:IsHasBuilding(iSuperWew) then
       for pCity in Oldplayer:Cities() do
            pCity:SetNumRealBuilding(iSheet, 0)
        end
    end
end
GameEvents.CityCaptureComplete.Add(iWOWLADS)

I'm bored with the variables, you can modify it appropriately.

By adding to GameEvents.CityCaptureComplete, does this happen every time a city is captured? And this only removes the buildings? The other function would add a GARRISONED_FAITH_BUILDING if a garrisoned unit is present? And that other function only runs the building adding if the GARRISONED_FAITH_WONDER is activated?
 
By adding to GameEvents.CityCaptureComplete, does this happen every time a city is captured? And this only removes the buildings? The other function would add a GARRISONED_FAITH_BUILDING if a garrisoned unit is present? And that other function only runs the building adding if the GARRISONED_FAITH_WONDER is activated?
First function says check every city, see if player has a garrison and the wonder, then add in a +1 faith dummy building. Second faith checks if a city has a wonder, if it does, check the old owner of this city and remove all the <FreeBuilding>.
 
First function says check every city, see if player has a garrison and the wonder, then add in a +1 faith dummy building. Second faith checks if a city has a wonder, if it does, check the old owner of this city and remove all the <FreeBuilding>.

Is it too much stress on the (system/computer/game - I'm not really sure which of thes) to constantly add and remove buildings every turn or will the function not really affect performance? This should be good so thank you!

Also, is there a way to modify the present code that checks to see if a certain unit like a great general or an artillery unit? So it would only give a bonus to that city if the specific unit was present?

edit:
Would it if
pCity:GetGarrisonedUnit() = 7

Since 7 is the great general number?
 
Last edited:
Is it too much stress on the (system/computer/game - I'm not really sure which of thes) to constantly add and remove buildings every turn or will the function not really affect performance? This should be good so thank you!
If you use one of JFD's mod, this code is literally nothing compared to his in terms of performance.
Also, is there a way to modify the present code that checks to see if a certain unit like a great general or an artillery unit? So it would only give a bonus to that city if the specific unit was present?

edit:
Would it if
pCity:GetGarrisonedUnit() = 7

Since 7 is the great general number?
I guess.
 
Is it too much stress on the (system/computer/game - I'm not really sure which of thes) to constantly add and remove buildings every turn or will the function not really affect performance

As much as WH will tell you to be efficient, I very much doubt you'd notice a difference :p

Also, is there a way to modify the present code that checks to see if a certain unit like a great general or an artillery unit? So it would only give a bonus to that city if the specific unit was present?

GetGarrisonedUnit() gives the unit pointer, doesn't it? What you should do from there is:

Code:
local pUnit = pCity:GetGarrisonedUnit()
if pUnit:GetUnitClassType() == GameInfoTypes ["UNITCLASS_XXX"] then

Where GetUnitClassType will return the ID of the correct row in UnitClasses, and GameInfoTypes[] will return the ID of the UnitClasses row with the queried Type. You should never use hardcoded IDs.
 
<FreeBuilding> Buildings go *poof* when a wonder goes obsolete or is captured.

I prefer to look at the city plot and determine what units are on the plot. Naval units, as I recall, are not returned as the 'Garrison' unit in G&K and BNW. And a civilian unit would not be either, as I recall. But as I mentioned I stopped using the GetrGarrisonUnit method long ago and used the Plot:GetNumUnits() method intstead. I also don't use the Plot:GetUnit() method because this only returns data for the unit on the plot that is seen as being the '0' unit. And if there is a workboat but not a naval combat unit on the plot, you get the workboat, even if there is also a land combat unit on the plot, as there can be in a city.
 
Top Bottom