Hooks
static bool BeforeCvAIOperation_cpp_GetClosestUnit(CvOperationSearchUnitList& kSearchList, CvPlot* pkMusterPlot, CvPlot* pkTarget, bool bNeedToCheckTarget, CvUnit*& result);static void AfterCvAIOperation_cpp_GetClosestUnit(CvOperationSearchUnitList& kSearchList, CvPlot* pkMusterPlot, CvPlot* pkTarget, bool bNeedToCheckTarget, CvUnit*& result);static CvUnit* ReplaceCvAIOperation_cpp_GetClosestUnit(CvUnit* (*original)(CvOperationSearchUnitList&, CvPlot*, CvPlot*, bool), CvOperationSearchUnitList& kSearchList, CvPlot* pkMusterPlot, CvPlot* pkTarget, bool bNeedToCheckTarget);// CivHooks.h gives this hook point no types, so nothing checks these hooks' parameters.host->RegisterHook(host->pluginIndex, CivHooks::CvAIOperation_cpp_GetClosestUnit::ID, CIV_HOOK_BEFORE, (void*)&BeforeCvAIOperation_cpp_GetClosestUnit);host->RegisterHook(host->pluginIndex, CivHooks::CvAIOperation_cpp_GetClosestUnit::ID, CIV_HOOK_AFTER, (void*)&AfterCvAIOperation_cpp_GetClosestUnit);host->RegisterHook(host->pluginIndex, CivHooks::CvAIOperation_cpp_GetClosestUnit::ID, CIV_HOOK_REPLACE, (void*)&ReplaceCvAIOperation_cpp_GetClosestUnit);
Code
1593static CvUnit* GetClosestUnit(CvOperationSearchUnitList& kSearchList, CvPlot* pkMusterPlot, CvPlot* pkTarget, bool bNeedToCheckTarget)1594{1595 CvUnit* pkBestUnit = NULL;1596 if (kSearchList.size())1597 {1598 std::stable_sort(kSearchList.begin(), kSearchList.end());15991600 int iBestDistance = MAX_INT;1601 CvIgnoreUnitsPathFinder& kPathFinder = GC.getIgnoreUnitsPathFinder();1602 for (CvOperationSearchUnitList::iterator itr = kSearchList.begin(); itr != kSearchList.end(); ++itr)1603 {1604 CvUnit *pkLoopUnit = (*itr).GetUnit();1605 int iDistance = (*itr).GetDistance();16061607 int iPathDistance = MAX_INT;1608 // Now loop through the units, using the pathfinder to do the final evaluation1609 if (pkMusterPlot != NULL)1610 {1611 if (!kPathFinder.DoesPathExist(*pkLoopUnit, pkLoopUnit->plot(), pkMusterPlot))1612 continue;16131614 iPathDistance = kPathFinder.GetPathLength();1615 }16161617 if(pkTarget != NULL && bNeedToCheckTarget)1618 {1619 if (!kPathFinder.DoesPathExist(*pkLoopUnit, pkLoopUnit->plot(), pkTarget))1620 continue;16211622 if (pkMusterPlot == NULL)1623 iPathDistance = kPathFinder.GetPathLength();1624 }16251626 // Reasonably close?1627 if (iPathDistance <= iDistance && iPathDistance <= iBestDistance)1628 {1629 pkBestUnit = pkLoopUnit;1630 break;1631 }16321633 if (iPathDistance < iBestDistance)1634 {1635 pkBestUnit = pkLoopUnit;1636 iBestDistance = iPathDistance;1637 }16381639 // Were we far away? If so, this is probably the best we are going to do1640 if (iDistance >= GC.getAI_HOMELAND_ESTIMATE_TURNS_DISTANCE())1641 break;1642 }1643 }16441645 return pkBestUnit;1646}