Need help with complex lua

emeralis00

Chieftain
Joined
Jul 19, 2014
Messages
33
Location
WI, United States
Hello, I've been working on a new civ (referred to as Yuma from here), and while I am almost complete, the last piece is particularity difficult.

The idea is that when Yuma enters a war, up to two Minor Civ allies of the other party in the war switch sides and become Yuma's ally. I have some of it already implemented, but am not sure how to replicate the effects of a coup, which is exactly what I need. If anyone has some advice on how to perform a coup via lua, or replicate the effect I would greatly appreciate it.

The code I have so far is below. Other parts of the mod use Whoward's DLL so that is an available. It requires BNW.

Code:
local YUMA = GameInfo.Civilizations.CIVILIZATION_CHITOSAN.ID

function yumaWarCoup(iTeam1, iTeam2, bWar)
	--print("OnWarStateChanged", iTeam1, iTeam2, bWar)

	for _, pPlayer in pairs(Players) do --start finding Yuma
		if pPlayer:IsEverAlive() then
			if pPlayer:GetCivilizationType() == YUMA then
				local fTeamID = pPlayer:GetTeam()
				if fTeamID == iTeam1 then
					print("Yuma is in a war. playerID is " ..pPlayer:GetID() ..". teamID is " ..fTeamID)
					-- the code after this point has not been tested in game
					local pEnemyPlayer = Players[iTeam2:GetLeaderID()]
					if not pEnemyPlayer:IsMinorCiv() and not pEnemyPlayer:IsBarbarian() then --IsMajorCiv()
							local iCoupsRemaining = 2 -- max stealable allies
						for i = GameDefines.MAX_MAJOR_CIVS, GameDefines.MAX_CIV_PLAYERS - 2 do --loop through all Minor Civs
							local mPlayer = Players[i]
							
							if mPlayer:GetAlly() == pEnemyPlayer then -- if Major Civ is ally of Minor Civ
								-- found allied MinorCiv
								if iCoupsRemaining > 0 then -- we can still steal a minor civ

									yumaCoupFunction(pPlayer, pEnemyPlayer, mPlayer) -- call for the coup, passing the target Minor Civ, the current Ally, and the Yuma Civ

									iCoupsRemaining = iCoupsRemaining - 1 -- one less civ left
								end
							end
						end
					end
				end
			end
		end
	end
end
Events.WarStateChanged.Add(YumaWarCoup)

function yumaCoupFunction(pYumaPlayer, pEnemyPlayer, pMinorPlayer)
	-- make pYumaPlayer the ally of pMinorPlayer, remove pEnemyPlayer status
	-- this is essentially a 100% likely coup
end

I can provide other parts of the code if needed, as well as more elaboration on what I want done.
 
Code:
local YUMA = GameInfo.Civilizations.CIVILIZATION_CHITOSAN.ID;
local YUMpl = {}
local HelpMMC = GameDefines.MAX_MAJOR_CIVS;
local HelpMAP = GameDefines.MAX_CIV_PLAYERS;

for i = 0, HelpMMC -1 do
	if Players[i]:GetCivilizationType() == YUMA then
		YUMpl[i] = Players[i]:GetTeam();
	end
end

function yumaWarCoup(iTeam1, iTeam2, bWar)
	if bWar then
		for iPlayer, iTeam in pairs(YUMpl) do
			if iTeam == iTeam1 then
				local CoupAva = 2;
				for i = 0, HelpMMC -1 do
					if Players[i]:GetTeam() == iTeam2 then
						for minCiv = HelpMMC, HelpMAP - 2 do
							if Players[minCiv]:GetAlly() == i then
								if Players[minCiv]:IsAlive() then
									DoYumaCoupt(iPlayer, i, minCiv)
									if CoupAva < 2 then
										return;
									else
										CoupAva = CoupAva - 1;
									end
								end	
							end
						end
					end
				end
			end
		end
	end
end

function DoYumaCoupt(iPlayer, i, minCiv)
	local InfluenceP = Players[minCiv]:GetMinorCivFriendshipWithMajor(i);
	Players[minCiv]:ChangeMinorCivFriendshipWithMajor(i, -60);
	Teams[Players[minCiv]:GetTeam()]:DeclareWar(Players[i]:GetTeam());
	Teams[Players[minCiv]:GetTeam()]:MakePeace(Players[iPlayer]:GetTeam());
	Players[minCiv]:ChangeMinorCivFriendshipWithMajor(iPlayer, InfluenceP - Players[minCiv]:GetMinorCivFriendshipWithMajor(iPlayer));
end

Events.WarStateChanged.Add(YumaWarCoup)
Nice ability, good luck.
 
Nice ability, good luck.
Thanks. I just tested it and it worked after fixing a couple mistakes. The yuma in the hook was not supposed to be capitalized and iPlayer, a number, in DoYumaCoupt was being used as an object (or whatever you get from the Players table).

You're getting an entry in my credits list when I'm done.
 
Top Bottom