[Lua] How to set a foreign city as a capital

Murphy613

Prince
Joined
Nov 18, 2012
Messages
483
Is there a lua to make a city get conquered by another civ before the game starts? Meaning, can I make a Worldbuilder map with Washington and New York as American cities, then make a lua to have them get conquered by the Mongols, so when the game starts they'll be under Mongol control but considered occupied American cities? I'm hoping this is possible bec. I know its possible to make a religion get founded in a city right before the game starts.

Edit: Perhaps if I explain exactly what I'm trying to do it'll be easier to help me. I'm making a Maccabean Revolt scenario. The map is of Most of modern Israel, part of modern Jordan, and a bit of modern Lebanon and Syria. There are two civs, the Seleucids and Israelites. There are 2 religions, Hellenism and Judaism. When the game starts, Hellenism will be founded with all beliefs in the Seleucid capital and Judaism will be founded with all beliefs in Jerusalem. (I'm using a lua code from the Into the Renaissance scenario for this, that has the religions get founded in those cities and get all beliefs right before the game loads.) If that were all it would be relatively simple. But it gets more complicated. I want every city on the map but 1 to start the game controlled by the Seleucids (you play as the Israelites and win by recapturing your territory.) Yet I want certain cities to be considered occupied Israelite cities and Jerusalem occupied Israelites capital. Any ideas?
 
First, find proper event. I would use:
Code:
Events.SequenceGameInitComplete

Though It also triggers on gameload so we will need some "cheesy" condition like:
Code:
Game.GetElapsedGameTurns()
(upon start it should have value 0 or 1, don't remember. Check it.)

Now, you only have to detect city you want to change owner in. I will use cityname, because you should easily adapt it for multiple cities. Full code:

Code:
Events.SequenceGameInitComplete.Add(function()
	if Game.GetElapsedGameTurns() == 0 then
		for iCity in Players[oldOwner]:Cities() do
			if iCity:GetName() == "New York" then
				Players[iNewOwner]:AcquireCity(iCity);
				iCity:ChangeResistanceTurns(-50)
			end
		end
	end
end)
oldOwner is integer. You are making scenario so you should know this number (first player = 0, second = 1, ...)
newOwner is also integer.
ChangeResistance - it should remove the resistance.

I bet it should work now or after little editing.
 
First, find proper event. I would use:
Code:
Events.SequenceGameInitComplete

Though It also triggers on gameload so we will need some "cheesy" condition like:
Code:
Game.GetElapsedGameTurns()
(upon start it should have value 0 or 1, don't remember. Check it.)

Now, you only have to detect city you want to change owner in. I will use cityname, because you should easily adapt it for multiple cities. Full code:

Code:
Events.SequenceGameInitComplete.Add(function()
	if Game.GetElapsedGameTurns() == 0 then
		for iCity in Players[oldOwner]:Cities() do
			if iCity:GetName() == "New York" then
				Players[iNewOwner]:AcquireCity(iCity);
				iCity:ChangeResistanceTurns(-50)
			end
		end
	end
end)
oldOwner is integer. You are making scenario so you should know this number (first player = 0, second = 1, ...)
newOwner is also integer.
ChangeResistance - it should remove the resistance.

I bet it should work now or after little editing.

This is perfect. Just one minor question though. If I wanted to do this for multiple cities, would I write this code over and over or is there a way to stick in a whole bunch of cities?
 
If you want to give several cities from civ A to civ B:
Code:
Events.SequenceGameInitComplete.Add(function()
	if Game.GetElapsedGameTurns() == 0 then
		for iCity in Players[oldOwner]:Cities() do
			if iCity:GetName() == "New York" [COLOR="Red"]or iCity:GetName() == "Washington" or [...][/COLOR] then
				Players[iNewOwner]:AcquireCity(iCity);
				iCity:ChangeResistanceTurns(-50)
			end
		end
	end
end)

If you want to give several cities from civ A to civs B and C:
Code:
Events.SequenceGameInitComplete.Add(function()
	if Game.GetElapsedGameTurns() == 0 then
		for iCity in Players[oldOwner]:Cities() do
			if iCity:GetName() == "New York" or iCity:GetName() == "Washington" or [...] then
				Players[iNewOwner]:AcquireCity(iCity);
				iCity:ChangeResistanceTurns(-50)
			end
			if iCity:GetName() == "Boston" or iCity:GetName() == "Chicago" or [...] then
				Players[[COLOR="Red"]iNewOwnerC[/COLOR]]:AcquireCity(iCity);
				iCity:ChangeResistanceTurns(-50)
			end
		end
	end
end)

If you want to give several cities from civs A and B to other civs:
Code:
Events.SequenceGameInitComplete.Add(function()
	if Game.GetElapsedGameTurns() == 0 then
		for iCity in Players[oldOwner]:Cities() do
                       [...]
		end
		for iCity in Players[[COLOR="Red"]oldOwnerB[/COLOR]]:Cities() do
                       [...]
		end
	end
end)
 
If you want to give several cities from civ A to civ B:
Code:
Events.SequenceGameInitComplete.Add(function()
	if Game.GetElapsedGameTurns() == 0 then
		for iCity in Players[oldOwner]:Cities() do
			if iCity:GetName() == "New York" [COLOR="Red"]or iCity:GetName() == "Washington" or [...][/COLOR] then
				Players[iNewOwner]:AcquireCity(iCity);
				iCity:ChangeResistanceTurns(-50)
			end
		end
	end
end)

If you want to give several cities from civ A to civs B and C:
Code:
Events.SequenceGameInitComplete.Add(function()
	if Game.GetElapsedGameTurns() == 0 then
		for iCity in Players[oldOwner]:Cities() do
			if iCity:GetName() == "New York" or iCity:GetName() == "Washington" or [...] then
				Players[iNewOwner]:AcquireCity(iCity);
				iCity:ChangeResistanceTurns(-50)
			end
			if iCity:GetName() == "Boston" or iCity:GetName() == "Chicago" or [...] then
				Players[[COLOR="Red"]iNewOwnerC[/COLOR]]:AcquireCity(iCity);
				iCity:ChangeResistanceTurns(-50)
			end
		end
	end
end)

If you want to give several cities from civs A and B to other civs:
Code:
Events.SequenceGameInitComplete.Add(function()
	if Game.GetElapsedGameTurns() == 0 then
		for iCity in Players[oldOwner]:Cities() do
                       [...]
		end
		for iCity in Players[[COLOR="Red"]oldOwnerB[/COLOR]]:Cities() do
                       [...]
		end
	end
end)

Thank you very much for your help. I have a problem though. I tried using the code and it did not work,a nd this showed up in my lua log:
Code:
[283567.703] Runtime Error: C:\Users\Murphy613\Documents\My Games\Sid Meier's Civilization 5\MODS\Maccabean Revolt (v 1)\Scenario/UI/CitySwaps.lua:5: attempt to index global 'Events' (a nil value)
[283567.703] Runtime Error: Error loading C:\Users\Murphy613\Documents\My Games\Sid Meier's Civilization 5\MODS\Maccabean Revolt (v 1)\Scenario/UI/CitySwaps.lua.

Also, should I be adding the code as an InGameUiAddin or a Mapscript (I tested it with Mapscript)?

Edit: Never mind, I realized my problem. I had forgotten to change oldOwner and iNewOwner. Though I' not sure what I'm supposed to put there. Would t be CIVILIZATION_GREECE, or something else?
 
Back
Top Bottom