Lua snippets

This is a very usefull thread.
Will try some of your code in the near future.
Thanks for sharing it.
 
This code transfers cities to another civ pregame... works for minors as well with some adjustments.

Code:
function CityTransfer()

local America
local pAmerica
local Denmark
local pDenmark

	if Game.GetElapsedGameTurns() == 0 then
	
		-- Set up Denmark Player

		for iPlayer=0, GameDefines.MAX_MAJOR_CIVS-1 do 

		local pDenmark = Players[iPlayer]

			if (GameInfo.Civilizations.CIVILIZATION_DENMARK.ID == pDenmark:GetCivilizationType()) then
	
			Denmark = pDenmark

			end	
		end

	-- Set up America Player

		for iPlayer=0, GameDefines.MAX_MAJOR_CIVS-1 do 

		local pAmerica = Players[iPlayer]

			if (GameInfo.Civilizations.CIVILIZATION_AMERICA.ID == pAmerica:GetCivilizationType()) then
	
			America = pAmerica

			end	
		end

	--city transfer

		for iCity in Denmark:Cities() do

			if iCity ~= nil and iCity:GetName() == "Lund" then
			print("Transferring...", iCity:GetName(), "to" , America:GetName(), "from", Denmark:GetName() )
			America:AcquireCity(iCity, false, true);

				if iCity ~= nil and iCity:IsRazing() then
				iCity:DoTask(TaskTypes.TASK_UNRAZE);
				
				end

			end
		end
	end
	
end

Events.SequenceGameInitComplete.Add(CityTransfer)
 
Hey Craig. Used your code for founding religions in a scenario I'm working on. I also made a LUA code to enable capturing a religion, as some of the playable civs didn't have one.

The code checks if a city being conquered is a holy city, and if the conquerer does not have a founded religion. If both cases are true, the conquerer becomes the new founder of the religion.

Code:
function CaptureReligion (iOldOwner, bIsCapital, iCityX, iCityY, iNewOwner, iPop, bConquest)

print ("Attempting to change religion founder")

local newOwner = Players[iNewOwner]
local oldOwner = Players[iOldOwner]

local targetReligionID = oldOwner:GetReligionCreatedByPlayer()
local newOwnerHasFoundedReligion = newOwner:GetReligionCreatedByPlayer()

local targetCityID = (Map.GetPlot(iCityX, iCityY):GetPlotCity()):GetID()
local holyCityID = (Game.GetHolyCityForReligion(targetReligionID, -1)):GetID()

print("Conquered City ID:" .. targetCityID)
print("Religion's Holy City ID:" .. holyCityID)

	if (targetCityID == holyCityID) then
		if newOwnerHasFoundedReligion < 1 then
			print("Religion Founder Changed")
			Game.SetFounder(targetReligionID , iNewOwner)
		end
	end

end

GameEvents.CityCaptureComplete.Add(CaptureReligion);
 
I doubt it would work very well if you tried having a civ be the founder for multiple religions. It certainly wouldn't be able to display it properly ingame, unless you wrote your own UI for it.
 
There's nothing in the DLL that stops a civ from founding more than one religion with different Holy Cities and it should be possibly from Lua - you'll need to adjust some aspects of the UI though.

Edit: There's also nothing that stops a pantheon from having multiple beliefs, although without a DLL mod, only the first will be copied to a newly founded religion
 
A question unrelated to the religions. How would you use LUA to force a specific starting era? I've had the scenario set it to medieval, but because I'd forgotten "Information Era" in the advanced setup from earlier games, this seems to overwrite it. I think I have the right command to do it, but I suspect it runs it too late.
 
If you're making a scenario Kimber it is probably easier to just mod the tech tree with XML.
 
Mod the tech tree? I'm not entirely sure what you mean. I want the tech tree to stay the same for now (Although I'm consider making a custom one in the future for a different version), but I want it to always start in the medieval era, even if the advanced setup says something else.
 
There's nothing in the DLL that stops a civ from founding more than one religion with different Holy Cities and it should be possibly from Lua - you'll need to adjust some aspects of the UI though.

Edit: There's also nothing that stops a pantheon from having multiple beliefs, although without a DLL mod, only the first will be copied to a newly founded religion

Thanks for the information.
 
I have altered my original found religion code. The first version did not properly enhance religions ( I derived it from code used for a city state, in which case enhancement was meaningless). The new code has been posted in the original thread, but I am also posting it here in the corrected version...

Code:
-- Find Berlin player and found Christianity in their city, returning the city

function EstablishChristian()
if Game.GetElapsedGameTurns() == 0 then

function CreateChristianHolyCity(pCity, religion, pantheonBelief, founderBelief, followerBelief, followerBelief2, enhancerBelief)
  local iPlayer = pCity:GetOwner()
  local iReligion = GameInfoTypes[religion]

  -- Optional extra beliefs
  local iBelief4 = followerBelief2 and GameInfoTypes[followerBelief2] or -1
  local iBelief5 = enhancerBelief and GameInfoTypes[enhancerBelief] or -1

  Game.FoundPantheon(iPlayer, GameInfoTypes[pantheonBelief])
  Game.FoundReligion(iPlayer, iReligion, nil, GameInfoTypes[founderBelief], GameInfoTypes[followerBelief], -1, -1, pCity)
  Game.EnhanceReligion(iPlayer, iReligion, GameInfoTypes[followerBelief2], GameInfoTypes[enhancerBelief]);
end

function FoundChristianity()
  for iPlayer=0, GameDefines.MAX_CIV_PLAYERS - 1 do
    local pPlayer = Players[iPlayer]
    if (pPlayer:IsEverAlive()) then

	for pCity in pPlayer:Cities() do

				-- City exists and has the proper name?
    			if pCity ~= nil and pCity:GetName() == "Berlin" then

		             CreateChristianHolyCity(pCity, "RELIGION_CHRISTIANITY", "BELIEF_GOD_CRAFTSMEN",
					"BELIEF_INITIATION_RITES", "BELIEF_CATHEDRALS1",  "BELIEF_STATE_CHURCH", "BELIEF_RELIGIOUS_TEXTS");
					print ("okay, Berlin is the Catholic holy city")
				return pCity
				end
			end
		end
	end
end

local pChristianPlot = FoundChristianity():Plot()

end
end

Events.SequenceGameInitComplete.Add(EstablishChristian)

Hope no inconvenience was caused for those using the original...
 
hey,

So I've been trying to add religions to various cities for a mod scenario I'm making, got the religions to found okay but I can't get non-holy cities to follow any of the religions from the start of the scenario.

Code:
-- relassign
-- Author: dagriggstar
-- DateCreated: 3/17/2014 9:56:16 PM
--------------------------------------------------------------
-- Enumerate Playable Civ Cities with Confucianism

function FollowChristianity()
if Game.GetGameTurn() == 0 then

for iPlayer=0, GameDefines.MAX_CIV_PLAYERS - 1 do
	local pPlayer = Players[iPlayer];
		if pPlayer:IsEverAlive() then 

		print (pPlayer:GetName())
			
			-- Enumerate cities
			for cityIndex = 0, pPlayer:GetNumCities() - 1, 1 do
    			local pCity = pPlayer:GetCityByID(cityIndex)
				 
				print (pCity:GetName());

				-- City exists and has the proper name?
    			if pCity ~= nil and city:GetName() == "Tong Binh" then 
					pCity:AdoptReligionFully(GameInfoTypes["RELIGION_TAOISM"]);
				elseif pCity ~= nil and pCity:GetName() == "Panyu" then
					pCity:AdoptReligionFully(GameInfoTypes["RELIGION_TAOISM"]);
				elseif pCity ~= nil and pCity:GetName() == "Chang'an" then
					pCity:AdoptReligionFully(GameInfoTypes["RELIGION_TAOISM"]);
				elseif pCity ~= nil and pCity:GetName() == "Ganzhou" then
					pCity:AdoptReligionFully(GameInfoTypes["RELIGION_TAOISM"]);
				elseif pCity ~= nil and pCity:GetName() == "Chengdu" then
					pCity:AdoptReligionFully(GameInfoTypes["RELIGION_TAOISM"]);
					return pCity							
    			end
			end
		end
end

end
end

Events.ActivePlayerTurnEnd.Add(FollowChristianity)

So help anyone ?
 
Back
Top Bottom