City States and LUA

Protroid

Chieftain
Joined
Apr 13, 2013
Messages
57
Location
United States
Hello

I want to write a method that grants a combat strength bonus to military units based on the number of city states you have brought into the war. The second half of the UA is granting units produced by Allied City States a special promotion. I know how to write the basic code, I just don't know how to go about the specific methods that Civ V's LUA contains, or where to even find them for that matter.

Basic code I have right now is as follows;

-- Nixon UA

local cityStateAllies

function countCityStateAllies(playerID)
local player = Players[playerID]
if(player:isAlive() and player:GetCivilizationType() == "CIVILIZATION_UNUM_AMERICA") then
for pCity in -- City States
if pCity -- is Allied then
cityStateAllies = cityStateAllies + 1
end
end
end
end

function strengthFromCityStates(playerID)
local player = Players[playerID]
if (player:isAlive() and player:GetCivilizationType() == "CIVILIZATION_UNUM_AMERICA" and -- war declared) then
if (cityStateAllies==1) then
for unit in player:Units() do
--Grant 2% Combat Bonus Promotion
end
end
if (cityStateAllies==2) then
for unit in player:Units() do
--Grant 4% Combat Bonus Promotion
end
end

-- so on, to 10%
end
end

function removeUAPromotions(playerID)
local player = Players[playerID]
if (player:isAlive() and player:GetCivilizationType() == "CIVILIZATION_UNUM_AMERICA") then
--remove UA Promotions
end
end

GameEvents.PlayerDoTurn.Add(countCityStateAllies)
GameEvents.PlayerDoTurn.Add(removeUAPromotions)
GameEvents.PlayerDoTurn.Add(strengthFromCityStates)

Once I know the methods, I should be able to figure out the second half, but I wanted to start by asking to see if my pseudo-ish code above would work, and the methods/variables I would need to make it work.

Cheers!
 
Basic Starting point: Lua and UI Reference (Civ5)

Player (Civ5 Type)

Team (Civ5 Type)

Unit (Civ5 Type)
  • Giving and taking away promotions from a unit will be under the Unit:SetHasPromotion() method

GameDefines (Civ5 Type)
  • You will have to loop through all Minor Civ players to find their "GetMinorCivFriendshipLevelWithMajor" (this is found in the player methods), and then tally all that are at Level # 2.
  • To make this loop a bit more efficient you can use the static defines as the start and end values for the loop, and start at GameDefines.MAX_MAJOR_CIVS and end the loop at (GameDefines.MAX_PLAYERS- 1)
  • The "-1" bit is because the game always assigns player #0 as the human player, so even though the game says it can have 64 players, the way the lua system works is you are always using player #0 to player #63 in the lua

Also, lua is not forgiving of Capitalization errors.
  • isAlive() != IsAlive()

This is an old old bit of code for tallying how many friend and how many city-state allies a player had. It is not the least bit efficient, I think. But it gives a practical example of the method required to figure how many city-state allies a given player has:
Code:
function CityStateAllies(iPlayer)
	local pPlayer = Players[iPlayer]
	local iNumberCSAllies = 0
	local iNumberCSFriends = 0
	local iCivPlayerNumber = iPlayer
	for i = GameDefines.MAX_MAJOR_CIVS, GameDefines.MAX_PLAYERS - 2 do
		if Players[i]:IsMinorCiv() then
			local iFriend = Players[i]:GetMinorCivFriendshipLevelWithMajor(iCivPlayerNumber)
			if iFriend == 1 then
				iNumberCSFriends = iNumberCSFriends + 1
			end
			if iFriend == 2 then
				iNumberCSAllies = iNumberCSAllies + 1
			end
		end
	end
	--stuff to do here based on number of allies
end
GameEvents.PlayerDoTurn.Add(CityStateAllies)
[edit]edited the code shown because as whoward mentioned, I forgot to take the barbs into account :sad:
 
... and end the loop at (GameDefines.MAX_PLAYERS - 2)

The "-1" bit is because the game always assigns player #0 as the human player, so even though the game says it can have 64 players, the way the lua system works is you are always using player #0 to player #63 in the lua

And the other -1 is because player #63 is always the barbies
 
Basic Starting point: Lua and UI Reference (Civ5)

Player (Civ5 Type)

Team (Civ5 Type)

Unit (Civ5 Type)
  • Giving and taking away promotions from a unit will be under the Unit:SetHasPromotion() method

GameDefines (Civ5 Type)
  • You will have to loop through all Minor Civ players to find their "GetMinorCivFriendshipLevelWithMajor" (this is found in the player methods), and then tally all that are at Level # 2.
  • To make this loop a bit more efficient you can use the static defines as the start and end values for the loop, and start at GameDefines.MAX_MAJOR_CIVS and end the loop at (GameDefines.MAX_PLAYERS- 1)
  • The "-1" bit is because the game always assigns player #0 as the human player, so even though the game says it can have 64 players, the way the lua system works is you are always using player #0 to player #63 in the lua

Also, lua is not forgiving of Capitalization errors.
  • isAlive() != IsAlive()

This is an old old bit of code for tallying how many friend and how many city-state allies a player had. It is not the least bit efficient, I think. But it gives a practical example of the method required to figure how many city-state allies a given player has:
Code:
function CityStateAllies(iPlayer)
	local pPlayer = Players[iPlayer]
	local iNumberCSAllies = 0
	local iNumberCSFriends = 0
	local iCivPlayerNumber = iPlayer
	for i = GameDefines.MAX_MAJOR_CIVS, GameDefines.MAX_PLAYERS - 1 do
		if Players[i]:IsMinorCiv() then
			local iFriend = Players[i]:GetMinorCivFriendshipLevelWithMajor(iCivPlayerNumber)
			if iFriend == 1 then
				iNumberCSFriends = iNumberCSFriends + 1
			end
			if iFriend == 2 then
				iNumberCSAllies = iNumberCSAllies + 1
			end
		end
	end
	--stuff to do here based on number of allies
end
GameEvents.PlayerDoTurn.Add(CityStateAllies)

Oh my, thank you so much! This is bound to help a ton!
 
Top Bottom