Bazaar developer reference

CvLeagueAI::AllocateVotes(CvLeague *)

Defined in
CvVotingClasses.cpp, line 8592
Class
CvLeagueAI
Hook name
CivHooks::CvLeagueAI_AllocateVotes

Hooks

static bool BeforeCvLeagueAI_AllocateVotes(CvLeagueAI* self, CvLeague* pLeague);static void AfterCvLeagueAI_AllocateVotes(CvLeagueAI* self, CvLeague* pLeague);static void ReplaceCvLeagueAI_AllocateVotes(void (*original)(CvLeagueAI*, CvLeague*), CvLeagueAI* self, CvLeague* pLeague);CivRegisterBefore<CivHooks::CvLeagueAI_AllocateVotes>(host, &BeforeCvLeagueAI_AllocateVotes);CivRegisterAfter<CivHooks::CvLeagueAI_AllocateVotes>(host, &AfterCvLeagueAI_AllocateVotes);CivRegisterReplace<CivHooks::CvLeagueAI_AllocateVotes>(host, &ReplaceCvLeagueAI_AllocateVotes);

Code

8591// Find the most preferred use of our votes in the session of pLeague, and allocate them with some randomness8592void CvLeagueAI::AllocateVotes(CvLeague* pLeague)8593{8594	CvAssert(pLeague != NULL);8595	if (!(pLeague != NULL)) 8596		return;85978598	int iVotes = pLeague->GetRemainingVotesForMember(GetPlayer()->GetID());8599	VoteConsiderationList vConsiderations;8600	int iFocusResolutionID = -1;86018602	EnactProposalList vEnactProposals = pLeague->GetEnactProposals();8603	for (EnactProposalList::iterator it = vEnactProposals.begin(); it != vEnactProposals.end(); ++it)8604	{8605		// Special case - If an embargo on us is proposed, use all our Delegates towards its outcome8606		if (it->GetEffects()->bEmbargoPlayer && it->GetProposerDecision()->GetDecision() == GetPlayer()->GetID())8607		{8608			iFocusResolutionID = it->GetID();8609		}86108611		FindBestVoteChoices(it, vConsiderations);8612	}8613	RepealProposalList vRepealProposals = pLeague->GetRepealProposals();8614	for (RepealProposalList::iterator it = vRepealProposals.begin(); it != vRepealProposals.end(); ++it)8615	{8616		// Special case - If an embargo on us is proposed, use all our Delegates towards its outcome8617		if (it->GetEffects()->bEmbargoPlayer && it->GetProposerDecision()->GetDecision() == GetPlayer()->GetID())8618		{8619			iFocusResolutionID = it->GetID();8620		}86218622		FindBestVoteChoices(it, vConsiderations);8623	}86248625	if (vConsiderations.size() > 0)8626	{8627		vConsiderations.SortItems();86288629		// If we want to focus on one resolution, zero out all other considerations8630		if (iFocusResolutionID != -1)8631		{8632			bool bFound = false;8633			for (int i = 0; i < vConsiderations.size(); ++i)8634			{8635				if (vConsiderations.GetElement(i).iID != iFocusResolutionID)8636				{8637					vConsiderations.SetWeight(i, 0);8638				}8639				else8640				{8641					bFound = true;8642				}8643			}8644			CvAssertMsg(bFound, "Could not find the intended proposal when focusing all Delegates on one proposal.");8645			CvAssertMsg(vConsiderations.GetTotalWeight() > 0, "Focusing all Delegates on one proposal, but it has no weight value.");8646		}86478648		// Even if we don't like anything, make sure we have something to choose from8649		if (vConsiderations.GetTotalWeight() <= 0)8650		{8651			for (int i = 0; i < vConsiderations.size(); i++)8652			{8653				vConsiderations.SetWeight(i, 1);8654			}8655		}86568657		CvWeightedVector<VoteConsideration, 4, false> vVotesAllocated;8658		for (int i = 0; i < iVotes; i++)8659		{8660			RandomNumberDelegate fcn = MakeDelegate(&GC.getGame(), &CvGame::getJonRandNum);8661			VoteConsideration chosen = vConsiderations.ChooseByWeight(&fcn, "Choosing a vote to allocate");86628663			if (chosen.bEnact)8664			{8665				pLeague->DoVoteEnact(chosen.iID, GetPlayer()->GetID(), 1, chosen.iChoice);8666			}8667			else8668			{8669				pLeague->DoVoteRepeal(chosen.iID, GetPlayer()->GetID(), 1, chosen.iChoice);8670			}86718672			// Zero out weight of any other choices that were considered for this proposal, since we can only allocate to one choice8673			for (int j = 0; j < vConsiderations.size(); j++)8674			{8675				if (vConsiderations.GetWeight(j) > 0)8676				{8677					if (vConsiderations.GetElement(j).bEnact == chosen.bEnact && vConsiderations.GetElement(j).iID == chosen.iID)8678					{8679						if (vConsiderations.GetElement(j).iChoice != chosen.iChoice)8680						{8681							vConsiderations.SetWeight(j, 0);8682						}8683					}8684				}8685			}86868687			// For logging - track how many delegates we allocate towards each considered option8688			bool bFirst = true;8689			for (int j = 0; j < vVotesAllocated.size(); j++)8690			{8691				if (vVotesAllocated.GetElement(j).bEnact == chosen.bEnact &&8692					vVotesAllocated.GetElement(j).iID == chosen.iID &&8693					vVotesAllocated.GetElement(j).iChoice == chosen.iChoice)8694				{8695					bFirst = false;8696					vVotesAllocated.SetWeight(j, vVotesAllocated.GetWeight(j) + 1);8697				}8698			}8699			if (bFirst)8700			{8701				vVotesAllocated.push_back(chosen, 1);8702			}8703		}87048705		// Logging8706		vVotesAllocated.SortItems();8707		for (int i = 0; i < vVotesAllocated.size(); i++)8708		{8709			if (vVotesAllocated.GetElement(i).bEnact)8710			{8711				CvEnactProposal* pProposal = pLeague->GetEnactProposal(vVotesAllocated.GetElement(i).iID);8712				CvAssert(pProposal != NULL);8713				if (pProposal != NULL)8714				{8715					LogVoteChoiceCommitted(pProposal, vVotesAllocated.GetElement(i).iChoice, vVotesAllocated.GetWeight(i));8716				}8717			}8718			else8719			{8720				CvRepealProposal* pProposal = pLeague->GetRepealProposal(vVotesAllocated.GetElement(i).iID);8721				CvAssert(pProposal != NULL);8722				if (pProposal != NULL)8723				{8724					LogVoteChoiceCommitted(pProposal, vVotesAllocated.GetElement(i).iChoice, vVotesAllocated.GetWeight(i));8725				}8726			}8727		}8728	}8729}

Calls

Called by