Skip to content

Commit

Permalink
Change function to be non-recursive (#11192)
Browse files Browse the repository at this point in the history
Should fix possible infinite recursion issue/any other possible issue with the function.
  • Loading branch information
KungCheops authored Aug 1, 2024
1 parent 5a3eaa8 commit cb8e377
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 35 deletions.
108 changes: 74 additions & 34 deletions CvGameCoreDLL_Expansion2/CvBuilderTaskingAI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3630,57 +3630,97 @@ void CvBuilderTaskingAI::SetupExtraXAdjacentPlots()
if (pkImprovementInfo->GetXSameAdjacentMakesValid() == 0)
continue;

for (int iI = 0; iI < GC.getMap().numPlots(); iI++)
SetupExtraXAdjacentPlotsForBuild(eBuild, eImprovement, pkImprovementInfo->GetXSameAdjacentMakesValid());
}
}

void CvBuilderTaskingAI::SetupExtraXAdjacentPlotsForBuild(BuildTypes eBuild, ImprovementTypes eImprovement, int iAdjacencyRequirement)
{
set<CvPlot*> sPlotsToCheck;

for (int iI = 0; iI < GC.getMap().numPlots(); iI++)
{
CvPlot* pPlot = GC.getMap().plotByIndexUnchecked(iI);
if (pPlot->isPlayerCityRadius(m_pPlayer->GetID()) && pPlot->canBuild(eBuild, m_pPlayer->GetID()))
sPlotsToCheck.insert(pPlot);
}

while (!sPlotsToCheck.empty())
{
set<CvPlot*> sNewPlotsToCheck;
for (set<CvPlot*>::const_iterator it = sPlotsToCheck.begin(); it != sPlotsToCheck.end(); ++it)
{
CvPlot* pPlot = GC.getMap().plotByIndexUnchecked(iI);
CvPlot* pPlot = *it;

if (!ShouldAnyBuilderConsiderPlot(pPlot))
if (!pPlot->isPlayerCityRadius(m_pPlayer->GetID()))
continue;

if (pPlot->getOwner() != m_pPlayer->GetID())
if (pPlot->getImprovementType() == eImprovement)
continue;

SetupExtraXAdjacentBuildPlot(pPlot, eBuild, eImprovement, pkImprovementInfo->GetXSameAdjacentMakesValid());
}
}
}
if (!pPlot->canBuild(eBuild, m_pPlayer->GetID(), false, true, true))
continue;

bool CvBuilderTaskingAI::SetupExtraXAdjacentBuildPlot(const CvPlot* pPlot, BuildTypes eBuild, ImprovementTypes eImprovement, int iAdjacencyRequirement, std::tr1::unordered_set<const CvPlot*> sIgnoredPlots)
{
if (m_extraPlotsForXAdjacentImprovements[eImprovement].find(pPlot) != m_extraPlotsForXAdjacentImprovements[eImprovement].end())
return true;
if (pPlot->canBuild(eBuild, m_pPlayer->GetID()))
{
m_extraPlotsForXAdjacentImprovements[eImprovement].insert(pPlot);

if (sIgnoredPlots.find(pPlot) != sIgnoredPlots.end())
return false;
for (int iI = 0; iI < NUM_DIRECTION_TYPES; iI++)
{
DirectionTypes eDirection = (DirectionTypes)iI;
CvPlot* pAdjacentPlot = plotDirection(pPlot->getX(), pPlot->getY(), eDirection);

if (!pPlot->canBuild(eBuild, m_pPlayer->GetID(), false, true, true))
return false;
if (!pAdjacentPlot)
continue;

if (pPlot->canBuild(eBuild, m_pPlayer->GetID()))
return true;
if (m_extraPlotsForXAdjacentImprovements[eImprovement].find(pAdjacentPlot) != m_extraPlotsForXAdjacentImprovements[eImprovement].end())
continue;

std::tr1::unordered_set<const CvPlot*> newIgnoredPlots = sIgnoredPlots;
newIgnoredPlots.insert(pPlot);
sNewPlotsToCheck.insert(pAdjacentPlot);
}

int iAdjacentCanBuild = 0;
continue;
}

for (int iI = 0; iI < NUM_DIRECTION_TYPES; iI++)
{
DirectionTypes eDirection = (DirectionTypes)iI;
CvPlot* pAdjacentPlot = plotDirection(pPlot->getX(), pPlot->getY(), eDirection);
int iPossibleAdjacentImprovements = 0;

if (!pAdjacentPlot)
continue;
for (int iI = 0; iI < NUM_DIRECTION_TYPES; iI++)
{
DirectionTypes eDirection = (DirectionTypes)iI;
CvPlot* pAdjacentPlot = plotDirection(pPlot->getX(), pPlot->getY(), eDirection);

if (SetupExtraXAdjacentBuildPlot(pAdjacentPlot, eBuild, eImprovement, iAdjacencyRequirement, newIgnoredPlots))
iAdjacentCanBuild++;
}
if (!pAdjacentPlot)
continue;

bool bRet = iAdjacentCanBuild >= iAdjacencyRequirement;
if (bRet)
m_extraPlotsForXAdjacentImprovements[eImprovement].insert(pPlot);
if (pAdjacentPlot->getImprovementType() == eImprovement)
iPossibleAdjacentImprovements++;
else if (m_extraPlotsForXAdjacentImprovements[eImprovement].find(pAdjacentPlot) != m_extraPlotsForXAdjacentImprovements[eImprovement].end())
iPossibleAdjacentImprovements++;
}

return bRet;
if (iPossibleAdjacentImprovements >= iAdjacencyRequirement)
{
m_extraPlotsForXAdjacentImprovements[eImprovement].insert(pPlot);

for (int iI = 0; iI < NUM_DIRECTION_TYPES; iI++)
{
DirectionTypes eDirection = (DirectionTypes)iI;
CvPlot* pAdjacentPlot = plotDirection(pPlot->getX(), pPlot->getY(), eDirection);

if (!pAdjacentPlot)
continue;

if (m_extraPlotsForXAdjacentImprovements[eImprovement].find(pAdjacentPlot) != m_extraPlotsForXAdjacentImprovements[eImprovement].end())
continue;

sNewPlotsToCheck.insert(pAdjacentPlot);
}

continue;
}
}
sPlotsToCheck.swap(sNewPlotsToCheck);
}
}

/// Central logging repository!
Expand Down
2 changes: 1 addition & 1 deletion CvGameCoreDLL_Expansion2/CvBuilderTaskingAI.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ class CvBuilderTaskingAI
int GetRouteMissingTiles(PlannedRoute plannedRoute) const;

void SetupExtraXAdjacentPlots();
bool SetupExtraXAdjacentBuildPlot(const CvPlot* pPlot, BuildTypes eBuild, ImprovementTypes eImprovement, int iAdjacencyRequirement, std::tr1::unordered_set<const CvPlot*> sIgnoredPlots = std::tr1::unordered_set<const CvPlot*>());
void SetupExtraXAdjacentPlotsForBuild(BuildTypes eBuild, ImprovementTypes eImprovement, int iAdjacencyRequirement);

void UpdateCanalPlots();

Expand Down

0 comments on commit cb8e377

Please sign in to comment.