[NFP] Unknown issue about works of functions

Lenin1870

Chieftain
Joined
Mar 16, 2013
Messages
50
Code:
-- YukinoGermany_GamePlay
-- Author: Yukino-Chan
-- DateCreated: 3/30/2020 9:14:40 PM
--==========================================================================================================================
-- INCLUDES
--==========================================================================================================================
include("JFD_YukinoGermany_Utils.lua");   
----------------------------------------------------------------------------------------------------------------------------
print("The script starts!")

function ChangeMilitarizationBalance(iPlayerID, iNumChange)
    return ExposedMembers.YukinoGermany.ChangeMilitarizationBalance(iPlayerID, iNumChange)
end

function GetMilitarizationBalance(iPlayerID)
    return ExposedMembers.YukinoGermany.GetMilitarizationBalance(iPlayerID)
end
--==========================================================================================================================
-- CORE FUNCTIONS
--==========================================================================================================================
-- Player_CalculationFunctions
----------------------------------------------------------------------------------------------------------------------------
function pPlayerUnitsGetOrLoseMilitarizationYield(iKilledPlayerID, iKilledUnitID, iPlayerID, iUnitID)
    local pKilledPlayer = Players[iKilledPlayerID];
    local pKilledPlayerConfig = PlayerConfigurations[iKilledPlayerID];
    local pKilledUnit = pKilledPlayer:GetUnits():FindID(iKilledUnitID);
    local pPlayer = Players[iPlayerID]; 
    local pPlayerConfig = PlayerConfigurations[iPlayerID];
    local pUnit = pPlayer:GetUnits():FindID(iUnitID);
    if pPlayer:IsAlive() and pKilledPlayer:IsAlive() and pPlayerConfig:GetCivilizationTypeName() == "CIVILIZATION_GERMANY" then
        if pUnit ~= pKilledUnit and UnitManager.Kill(pKilledUnit) then
            ChangeMilitarizationBalance(iPlayerID, 1)
            print("+1 Militarization for killed enemy unit")
        end
    elseif pPlayer:IsAlive() and pKilledPlayer:IsAlive() and pKilledPlayerConfig:GetCivilizationTypeName() == "CIVILIZATION_GERMANY" then
        if pUnit ~= pKilledUnit and UnitManager.Kill(pKilledUnit) then
            ChangeMilitarizationBalance(iPlayerID, -1)
            print("-1 Militarization for killed Prussian unit")
        end
    end
end
Events.UnitKilledInCombat.Add(pPlayerUnitsGetOrLoseMilitarizationYield)
----------------------------------------------------------------------------------------------------------------------------
function pPlayerCityGetOrLoseMilitarizationYield(iPlayerID, iCityID)
    local pPlayer = Players[iPlayerID]; 
    local pPlayerConfig = PlayerConfigurations[iPlayerID];
    local pPlayerCities = pPlayer:GetCities();
    local pGermanCities;
    if pPlayer:IsAlive() and pPlayerConfig:GetCivilizationTypeName() == "CIVILIZATION_GERMANY" then
        pGermanCities = pPlayer:GetCities();
    end
    for i, pPlayerCity in pPlayerCities:Members() do
        for j, pGermanCity in pGermanCities:Members() do
            if pPlayer:IsAlive() and pPlayerConfig:GetCivilizationTypeName() == "CIVILIZATION_GERMANY" and pGermanCity == pPlayerCity then
                ChangeMilitarizationBalance(iPlayerID, 1)
                print("+1 Militarization for gaining city")
            elseif pPlayer:IsAlive() and pPlayerConfig:GetCivilizationTypeName() ~= "CIVILIZATION_GERMANY" and pGermanCity == pPlayerCity then
                ChangeMilitarizationBalance(iPlayerID, -1)
                print("-1 Militarization for losing city")
            end
        end
    end
end
Events.CityOccupationChanged.Add(pPlayerCityGetOrLoseMilitarizationYield)
----------------------------------------------------------------------------------------------------------------------------
function pPlayerEncampmentTrainingCompleteGetMilitarizationYield(iPlayerID, iProjectID, bCancelled)
    local iProjectEncampmentTrainingID = GameInfo.Projects["PROJECT_ENHANCE_DISTRICT_ENCAMPMENT"].Index;
    local pPlayer = Players[iPlayerID]; 
    local pPlayerConfig = PlayerConfigurations[iPlayerID];
    local pPlayerCities = pPlayer:GetCities();
    local pGermanCities;
    if pPlayer:IsAlive() and pPlayerConfig:GetCivilizationTypeName() == "CIVILIZATION_GERMANY" then
        pGermanCities = pPlayer:GetCities();
    end
    for i, pPlayerCity in pPlayerCities:Members() do
        for j, pGermanCity in pGermanCities:Members() do
            if pGermanCity == pPlayerCity and iProjectID == iProjectEncampmentTrainingID and bCancelled == false then
                ChangeMilitarizationBalance(iPlayerID, 5)
                print("+5 Militarization for completing encampment training")
            end
        end
    end
end
Events.CityProjectCompleted.Add(pPlayerEncampmentTrainingCompleteGetMilitarizationYield)
----------------------------------------------------------------------------------------------------------------------------
function pPlayerDeficitLoseMilitarizationYield(iPlayerID, iGoldPerTurn)
    local pPlayer = Players[iPlayerID]; 
    local pPlayerConfig = PlayerConfigurations[iPlayerID];
    if pPlayer:IsAlive() and pPlayerConfig:GetCivilizationTypeName() == "CIVILIZATION_GERMANY" and iGoldPerTurn < 0 then
        ChangeMilitarizationBalance(iPlayerID, -5)
        print("-5 Militarization for deficit")
    end
end
Events.TreasuryChanged.Add(pPlayerDeficitLoseMilitarizationYield)
----------------------------------------------------------------------------------------------------------------------------
function pPlayerOnCombat(m_CombatResults)
    local attacker = m_CombatResults[CombatResultParameters.ATTACKER]
    local defender = m_CombatResults[CombatResultParameters.DEFENDER]
    pPlayerMilitarizationBonus(attacker, defender)
    pPlayerKillBonus(attacker, defender)
end
Events.Combat.Add(pPlayerOnCombat)
----------------------------------------------------------------------------------------------------------------------------
-- Player_ApplicationFunctions
----------------------------------------------------------------------------------------------------------------------------
function pPlayerMilitarizationBonus(attacker, defender)
    local tAttackerID = attacker[CombatResultParameters.ID];
    local pAttackingPlayer = Players[tAttackerID.player];
    local pAttackingPlayerConfig = PlayerConfigurations[tAttackerID.player];
    local pAttackingPlayerUnits = pAttackingPlayer:GetUnits();
    local tDefenderID = defender[CombatResultParameters.ID];
    local pDefendingPlayer = Players[tDefenderID.player];
    local pDefendingPlayerConfig = PlayerConfigurations[tDefenderID.player];
    local pDefendingPlayerUnits = pDefendingPlayer:GetUnits();
    local iAttackerCombatStrength    = attacker[CombatResultParameters.COMBAT_STRENGTH];
    local iDefenderCombatStrength    = defender[CombatResultParameters.COMBAT_STRENGTH];
    local iAttackerBonus            = attacker[CombatResultParameters.STRENGTH_MODIFIER];
    local iDefenderBonus            = defender[CombatResultParameters.STRENGTH_MODIFIER];
    local iAttackerUnitBonus = GetMilitarizationBalance(tAttackerID.player);
    local iDefenderUnitBonus = GetMilitarizationBalance(tDefenderID.player);

    if pAttackingPlayer:IsAlive() and pAttackingPlayerConfig:GetCivilizationTypeName() == "CIVILIZATION_GERMANY" then
        for i, pAttackingPlayerUnit in pAttackingPlayerUnits:Members() do
            iAttackerBonus = iAttackerBonus + iAttackerCombatStrength * 0.01 * iAttackerUnitBonus
            pAttackingPlayerUnit:ChangeDamage(iAttackerBonus)
            pAttackingPlayerUnit:GetExperience():ChangeExperience(iAttackerUnitExperienceBonus)
            print("Prussian attacker bonus")
        end
    elseif pDefendingPlayer:IsAlive() and pDefendingPlayerConfig:GetCivilizationTypeName() == "CIVILIZATION_GERMANY" then
        for i, pDefendingPlayerUnit in pDefendingPlayerUnits:Members() do
            iDefenderBonus = iDefenderBonus + iDefenderCombatStrength * 0.01 * iDefenderUnitBonus
            pDefendingPlayerUnit:ChangeDamage(iDefenderBonus)
            pDefendingPlayerUnit:GetExperience():ChangeExperience(iDefenderUnitExperienceBonus)
            print("Prussian defender bonus")
        end
    end
end
----------------------------------------------------------------------------------------------------------------------------
function pPlayerKillBonus(attacker, defender)
    local tAttackerID = attacker[CombatResultParameters.ID];
    local pAttackingPlayer = Players[tAttackerID.player];
    local pAttackingPlayerConfig = PlayerConfigurations[tAttackerID.player];
    local pAttackingPlayerUnits = pAttackingPlayer:GetUnits();
    local iAttackerUnitBonus = GetMilitarizationBalance(tAttackerID.player);
    local tDefenderID = defender[CombatResultParameters.ID];
    local pDefendingPlayer = Players[tDefenderID.player];
    local pDefendingPlayerConfig = PlayerConfigurations[tDefenderID.player];
    local pDefendingPlayerUnits = pDefendingPlayer:GetUnits();
    local iDefenderUnitBonus = GetMilitarizationBalance(tDefenderID.player);

    if pAttackingPlayer:IsAlive() and pAttackingPlayerConfig:GetCivilizationTypeName() == "CIVILIZATION_GERMANY" then
        local pkAttacker = UnitManager.GetUnit(tAttackerID.player, tAttackerID.id);
        local pPlayerEnemyUnitsKillRate = 0.1 * iAttackerUnitBonus
        if Automation.GetRandomNumber(100) <= pPlayerEnemyUnitsKillRate then
            for i, pDefendingPlayerUnit in pDefendingPlayerUnits:Members() do
                pkAttacker.Kill(pDefendingPlayerUnit)
                print("Prussian attacker headshot!")
            end
        end
    elseif pDefendingPlayer:IsAlive() and pDefendingPlayerConfig:GetCivilizationTypeName() == "CIVILIZATION_GERMANY" then
        local pkDefender = UnitManager.GetUnit(tDefenderID.player, tDefenderID.id);
        local pPlayerEnemyUnitsKillRate = 0.1 * iDefenderUnitBonus
        if Automation.GetRandomNumber(100) <= pPlayerEnemyUnitsKillRate then
            for i, pAttackingPlayerUnit in pAttackingPlayerUnits:Members() do
                 pkDefender.Kill(pAttackingPlayerUnit)
                print("Prussian defender headshot!")
            end
        end
    end
end
--==========================================================================================================================
--==========================================================================================================================
In order to check functions' action, I inserted print command in functions. As a result, I have confirmed to have issues of many functions except part under 'Player_ApplicationFunctions' through using 'print' commands.

There might have been not syntax errors in this script. If had existed, it was written in Lua.log file. But the log file didn't write everything about errors and only wrote "Prussian attacker bonus" when I attacked enemy unit. 'CityProjectCompleted' and 'UnitKilledInCombat' did not work. So I have infered other functions have not worked except the part of 'Player_ApplicationFunctions'.

I conclude it is caused problems of Events or local variables, however I also think other possibilities should be considered.

Could you know causes of the issues? Is there a solution?
 
Back
Top Bottom