Lua war status check issue

Karatekid5

Warlord
Joined
Nov 11, 2012
Messages
198
Location
Pennsylvania
I am currently working on finishing the Lua portion of my newest custom civilization's unique ability, and I've run into a bit of an issue. The code is intended to grant an amount of copies of a dummy building based on the player's Declaration of Friendship count, and then swap out those buildings for the same amount of a different dummy building while at war. I was able to use some code from Calcul8or's Connecticut mod as a base, and modify it to fit the parameters of the Unique Ability I chose. It works for the most part, showing no errors in the LUA log and granting the player the correct dummy building count for peacetime. However, the check I added for the player's war status isn't working, even though I set it all up in a similar fashion to the check in the Pleco unit's ability. Here is the current code:

Code:
print("PLUS ULTRA!!!!!");

local civID   = GameInfoTypes["CIVILIZATION_UA_HIGH"]
local bldgID  = GameInfoTypes["BUILDING_NEZU_CULTURE"];
local bldgID2 = GameInfoTypes["BUILDING_NEZU_GREAT_PEOPLE"];

function NezuCheck(player)
    for playerID, player in pairs(Players) do
        local player = Players[playerID];
        if player:GetCivilizationType() == civID then
            GameEvents.PlayerDoTurn.Add(NezuFriendshipRoutine)
        end
    end
end
Events.SequenceGameInitComplete.Add(NezuCheck)

function NezuFriendshipRoutine(playerID)
    local pPlayer = Players[playerID]
    local pPlayerTeam = Teams[pPlayer:GetTeam()]
    if (pPlayer:IsAlive() and pPlayer:GetCivilizationType() == civID) then
        local DOF = DOFcount(playerID)
        print("Final DOF count = " .. DOF);   

        if (pPlayerTeam:IsAtWar()) then
            for city in pPlayer:Cities() do
                city:SetNumRealBuilding(bldgID2, DOF)
                city:SetNumRealBuilding(bldgID, 0)
            end
        else

            for city in pPlayer:Cities() do
                city:SetNumRealBuilding(bldgID, DOF)
                city:SetNumRealBuilding(bldgID2, 0)
            end
        end
    end
end

function DOFcount(iPlayer)
    local player = Players[iPlayer]
    local iCounter = 0
    for i = 0, GameDefines.MAX_MAJOR_CIVS - 1 do
        local pOtherPlayer = Players[i]
        --print("Checking for DoF with " .. pOtherPlayer:GetName());
        if (pOtherPlayer:IsAlive()) then
            if (i ~= iPlayer and player:IsDoF(i)) then
                iCounter = iCounter + 1
            end
        end
    end
    return iCounter
end

And for a side question, is it possible to use Lua to assign an icon to a specific UniqueName of a unit? For example, this civ's unique unit is the Great Hero (Great General replacement), and for flavor it'd be cool to have Lua that checks the UniqueName of the unit and sets an icon from an atlas that matches the character name chosen instead of the default. I know it likely isn't possible but I just wanted to know if it's worth looking into.

Thanks in advance!
 
Your problem lies in the following line:
if (pPlayerTeam:IsAtWar()) then
The cheatsheets mention that Team:IsAtWar(...) requires a teamID as a parameter (which is the teamID of the team for which you want to check if you're at war).
I suspect that it defaults to 0 when omitted (which is the team of the human player in SP). Since you'll never be at war with Team 0 (it's YOUR team), the condition never holds and you will always be "at peace".
There exists 2 options to fix it:
1) Loop over all teams, and check :IsAtWar(...) for each of these teams.
2) Use Team:GetAtWarCount(bIgnoreMinors) instead
(Alternatively, you could also hook into GameEvents.DeclareWar(...) and GameEvents.MakePeace(...) for faster changes in the bonus)

---

Also, the following piece contains an error that will be hard to find:
Code:
for playerID, player in pairs(Players) do
        local player = Players[playerID];
        if player:GetCivilizationType() == civID then
            GameEvents.PlayerDoTurn.Add(NezuFriendshipRoutine)
        end
    end
If multiple copies of your civ are in play (I.e. two or more people are playing your civ), then you hook in your function multiple times! This means that you function will execute multiple times!
I suggest you'd use Whoward's IsCivInPlay(...) method instead to check if your civ is in play.

---

And for a side question, is it possible to use Lua to assign an icon to a specific UniqueName of a unit? For example, this civ's unique unit is the Great Hero (Great General replacement), and for flavor it'd be cool to have Lua that checks the UniqueName of the unit and sets an icon from an atlas that matches the character name chosen instead of the default. I know it likely isn't possible but I just wanted to know if it's worth looking into.
I have been thinking of something like this as well, but haven't really taken (or had) the time to see if something like this already exists!
If it doesn't, you could do it yourself by replacing existing .lua files. E.g. UnitPanel.lua(?) for changing the icon that appears in the UnitPanel, NotificationPanel.lua(?) for the unit icons that appear in the NotificationPanel, etc. Of course you could skip some files (such as NotificationPanel.lua(?)) since you don't see units them often anyways.
Alternatively, you could add a UI that overlays the existing UI (E.g. UnitPanel). This means that if another mod were to change the UnitPanel, your mods would not clash. Of course, if the other mod changes the UnitPanel in a significant way (such as EUI), then it would not look good. But for minor changes it would not cause incompatibility issues!
 
I decided to go with the Team:GetAtWarCount(bIgnoreMinors) option and it is now working perfectly! Thank you! and I looked back at the code and remembered that the Pleco code I pulled the war check from had a plot owner check that sets the Team ID parameter needed. I'll work on the multiple copies issue for this code later, but for right now I'm just glad that it is fully functional.

I think the add UI option would be the better of the two to look into, as it would cause the least compatibility issues. For mods that edit the unit UI there could always be a check for said mods and forcing a different version (designed to suit the UI mod) if it is detected. In fact, this would make for a good globally implementable element like Unique Diplomacy Utilities that a lot of modders could make use of. Though I personally wouldn't know where to start since I've never made UI edits.
 
I'd recommend Whoward's UI Tutorials if you're interested in UI modding. It helped me a TON when making 'Additional Achievements' (a UI-intensive mod). And of course, practise makes... better (not perfect ;))
 
Top Bottom