Craig_Sutter
Deity
Here is a bit of code I have in my mod... The first portion converts missionary units to a UU Irish missionary. It is supposed to convert units if the missionary is run by a human, and otherwise, to do so 40% or the time (2/5).
The second portion calls a building to generate missionaries and forcibly deducts faith to pay for them. The intent is that the first code will operate upon the second to convert the building spawned missionaries to Irish missionaries.
The only problem is that the conversion seems to be happening 100% of the time as opposed to 40%.
I cannot figure out why.
Here is the code:
Can fresh eyes figure out why this is so?
Thank-you.
The second portion calls a building to generate missionaries and forcibly deducts faith to pay for them. The intent is that the first code will operate upon the second to convert the building spawned missionaries to Irish missionaries.
The only problem is that the conversion seems to be happening 100% of the time as opposed to 40%.
I cannot figure out why.
Here is the code:
Code:
-- Exchanges missionary purchased with Unique unit
-- MissionaryReplacer
-- Author: LastSword
-- DateCreated: 8/24/2013 2:56:18 PM
--------------------------------------------------------------
local sUnitType = "UNIT_MISSIONARY"
local iMissionaryID = GameInfo.Units.UNIT_MISSIONARY.ID
local iMissionaryOverride = GameInfo.Units.UNIT_IRISH_MISSIONARY.ID
local iCivType = GameInfo.Civilizations["CIVILIZATION_OTTOMAN"].ID
function MissionaryOverride(iPlayer, iUnit)
local pPlayer = Players[iPlayer];
if (pPlayer:IsEverAlive()) then
if (pPlayer:GetCivilizationType() == iCivType) then
if pPlayer:GetUnitByID(iUnit) ~= nil then
pUnit = pPlayer:GetUnitByID(iUnit);
if (pUnit:GetUnitType() == iMissionaryID) then
if pPlayer:IsHuman() or (math.random (5) > 2) then
local newUnit = pPlayer:InitUnit(iMissionaryOverride, pUnit:GetX(), pUnit:GetY())
newUnit:Convert(pUnit);
end
end
end
end
end
end
LuaEvents.SerialEventUnitCreatedGood.Add( MissionaryOverride )
-- To spawn a Missionary in AI player city and deduct faith purchase price.
function ForceMissionaryPurchase (iPlayer)
local player = Players[iPlayer]
if player and player:IsAlive() and (GameInfo.Civilizations.CIVILIZATION_OTTOMAN.ID == player:GetCivilizationType()) and not player:IsHuman() then
for city in player:Cities() do
if (city:GetReligiousMajority() == GameInfoTypes["RELIGION_CHRISTIANITY"]) or (city:GetReligiousMajority() == GameInfoTypes["RELIGION_HINDUISM"])
or (city:GetReligiousMajority() == GameInfoTypes["RELIGION_CONFUCIANISM"]) or (city:GetReligiousMajority() == GameInfoTypes["RELIGION_BUDDHISM"]) then
local iMissionary = GameInfoTypes.UNIT_MISSIONARY
local iUnitCost = city:GetUnitFaithPurchaseCost(iMissionary)
if player:GetFaith() >= iUnitCost then
city:SetNumRealBuilding(GameInfoTypes["BUILDING_CLOISTER"], 1);
player:ChangeFaith ( -iUnitCost )
else
city:SetNumRealBuilding(GameInfoTypes["BUILDING_CLOISTER"], 0);
end
end
end
end
end
Can fresh eyes figure out why this is so?
Thank-you.