-- From: IGE (modified)
function ConvertState(state, sourceID, targetID, toConvert)
-- Enough people from that other religion?
local converted = toConvert
if state[sourceID] < toConvert then
converted = state[sourceID]
end
state[sourceID] = state[sourceID] - converted;
state[targetID] = state[targetID] + converted;
return toConvert - converted;
end
-- From: IGE (modified)
function SetFollowers(pCity, religionID, num)
local current = pCity:GetNumFollowers(religionID);
if (num == current) then return end
-- Get followers state
local count = 0;
local state = {}
for row in GameInfo.Religions() do
state[row.ID] = pCity:GetNumFollowers(row.ID);
count = count + state[row.ID];
end
state[-1] = pCity:GetPopulation() - count;
if num < current then
-- Convert to atheists
ConvertState(state, religionID, -1, current - num);
else
local toConvert = num - current;
-- Convert from atheists
toConvert = ConvertState(state, -1, religionID, toConvert)
-- Convert from pantheon
toConvert = ConvertState(state, 0, religionID, toConvert)
-- Convert from group with max followers, one at a time
while toConvert > 0 do
local maxFollowers = 0;
local sourceID = -1;
for row in GameInfo.Religions() do
if row.ID ~= religionID and row.ID > 0 then
if state[row.ID] > maxFollowers then
sourceID = row.ID;
maxFollowers = state[row.ID];
end
end
end
if sourceID == -1 then break end
if ConvertState(state, sourceID, religionID, 1) == 0 then
toConvert = toConvert - 1;
end
end
end
SetReligionState(pCity, state);
end
-- From: IGE (modified)
function SetReligionState(pCity, state)
-- What is the majority?
local maxFollowers = -1;
local majority = -1;
for i in pairs(state) do
state[i] = math.floor(state[i] + 0.5)
if state[i] > maxFollowers then
majority = i;
maxFollowers = state[i]
end
end
-- EDIT: Those crashes may actually have only been caused by tests with a religion not founded yet. Doesn't matter, leave the code like that
-- ConvertPercentFollowers is full of nasty bugs (can only convert from majority to minority) hence this twisted method
pCity:AdoptReligionFully(majority);
print("Done fully adopting")
for i, v in pairs(state) do
if i ~= majority and i >= 0 then
-- Convert 1% at a time because followers are internally stored as real numbers.
while (pCity:GetNumFollowers(i) + 0.5) < v do
pCity:ConvertPercentFollowers(i, majority, 1)
end
end
end
print("Done increasing minorities.")
-- We do atheists in the end because of a rounding error in civ5 (sum of followers can be population + 1)
-- Since they're never displayed, we actually use the majority as a loop condition.
if majority >= 0 then
while (pCity:GetNumFollowers(majority) - 0.5) > state[majority] do
pCity:ConvertPercentFollowers(-1, majority, 1)
end
end
print("Done lowering majority.")
end