Bazaar developer reference

CvTacticalAI::ChooseRemainingAssignments(int, int)

Defined in
CvTacticalAI.cpp, line 11021
Class
CvTacticalAI
Hook name
CivHooks::CvTacticalAI_ChooseRemainingAssignments

Hooks

static bool BeforeCvTacticalAI_ChooseRemainingAssignments(CvTacticalAI* self, int iNumUnitsDesired, int iNumUnitsAcceptable, bool& result);static void AfterCvTacticalAI_ChooseRemainingAssignments(CvTacticalAI* self, int iNumUnitsDesired, int iNumUnitsAcceptable, bool& result);static bool ReplaceCvTacticalAI_ChooseRemainingAssignments(bool (*original)(CvTacticalAI*, int, int), CvTacticalAI* self, int iNumUnitsDesired, int iNumUnitsAcceptable);CivRegisterBefore<CivHooks::CvTacticalAI_ChooseRemainingAssignments>(host, &BeforeCvTacticalAI_ChooseRemainingAssignments);CivRegisterAfter<CivHooks::CvTacticalAI_ChooseRemainingAssignments>(host, &AfterCvTacticalAI_ChooseRemainingAssignments);CivRegisterReplace<CivHooks::CvTacticalAI_ChooseRemainingAssignments>(host, &ReplaceCvTacticalAI_ChooseRemainingAssignments);

Code

11020/// No clear cut blocking assignments left, have to make search possibilities and score most preferred11021bool CvTacticalAI::ChooseRemainingAssignments(int iNumUnitsDesired, int iNumUnitsAcceptable)11022{11023	int iBestScore = 0;11024	int iScore;11025	int iCurrent[NUM_DIRECTION_TYPES];11026	int iFirst[NUM_DIRECTION_TYPES];11027	int iLast[NUM_DIRECTION_TYPES];1102811029	int iBlocksToCreate = iNumUnitsDesired - (int)m_ChosenBlocks.size();1103011031	if(iBlocksToCreate == 0)11032	{11033		return true;11034	}1103511036	FAssertMsg(iBlocksToCreate <= NUM_DIRECTION_TYPES, "More than NUM_DIRECTION_TYPES hexes to block. Will cause array overflows and performance issues!");1103711038	if(iBlocksToCreate > 0 && iBlocksToCreate < NUM_DIRECTION_TYPES)11039	{11040		m_NewlyChosen.clear();1104111042		// Set up indexes pointing to the possible choices11043		int iLevel = 0;11044		int iCurIndex = 0;11045		while(iLevel < iBlocksToCreate)11046		{11047			iFirst[iLevel] = iCurIndex;11048			int iNumChoices = m_PotentialBlocks[iCurIndex].GetNumChoices();1104911050			FAssertMsg(iNumChoices > 0, "Invalid number of tactical AI move choices. Will cause array overflows and performance issues!");11051			FAssertMsg(iNumChoices + iCurIndex <= (int)m_PotentialBlocks.size(), "Invalid number of tactical AI move choices. Will cause array overflows and performance issues!");1105211053			iLast[iLevel] = iCurIndex + iNumChoices - 1;11054			iCurIndex = iLast[iLevel] + 1;11055			iLevel++;11056		}1105711058		for(int iI = 0; iI < iBlocksToCreate; iI++)11059		{11060			iCurrent[iI] = iFirst[iI];11061		}1106211063		// Loop through each possibility11064		bool bDone = false;11065		while(!bDone)11066		{11067			m_TemporaryBlocks.clear();1106811069			// Create this choice11070			for(int iI = 0; iI < iBlocksToCreate; iI++)11071			{11072				if(iI >= (int)m_PotentialBlocks.size())11073				{11074					FAssertMsg(false, "Invalid fast vector index - show Ed");11075				}11076				m_TemporaryBlocks.push_back(m_PotentialBlocks[iCurrent[iI]]);11077			}1107811079			iScore = ScoreAssignments(iNumUnitsDesired != iNumUnitsAcceptable /*bCanLeaveOpenings*/);1108011081			// If best so far, save it off11082			if(iScore > iBestScore)11083			{11084				m_NewlyChosen.clear();11085				for(unsigned int iI = 0; iI < m_TemporaryBlocks.size(); iI++)11086				{11087					if(m_TemporaryBlocks[iI].GetDistanceToTarget() != MAX_INT)11088					{11089						m_NewlyChosen.push_back(m_TemporaryBlocks[iI]);11090					}11091				}11092				iBestScore = iScore;11093			}1109411095			// Increment proper index11096			bool bIncrementDone = false;11097			for(int iLevelIndex = iBlocksToCreate - 1; !bIncrementDone && iLevelIndex >= 0 && iLevelIndex < NUM_DIRECTION_TYPES; iLevelIndex--)11098			{11099				// See if at end of line for this index11100				if(iCurrent[iLevelIndex] + 1 > iLast[iLevelIndex])11101				{11102					// Reset to first one and keep iterating11103					iCurrent[iLevelIndex] = iFirst[iLevelIndex];11104				}1110511106				else11107				{11108					iCurrent[iLevelIndex]++;11109					bIncrementDone = true;11110				}11111			}1111211113			if(!bIncrementDone)11114			{11115				bDone = true;11116			}11117		}1111811119		// Copy final choices into output11120		for(unsigned int iI = 0; iI < m_NewlyChosen.size(); iI++)11121		{11122			m_ChosenBlocks.push_back(m_NewlyChosen[iI]);11123		}1112411125		return (int)m_ChosenBlocks.size() >= iNumUnitsAcceptable;11126	}1112711128	return false;11129}

Calls

Called by