Hooks
static bool BeforeCvUnit_UpdatePathCache(CvUnit* self, CvPlot* pDestPlot, int iFlags, bool& result);static void AfterCvUnit_UpdatePathCache(CvUnit* self, CvPlot* pDestPlot, int iFlags, bool& result);static bool ReplaceCvUnit_UpdatePathCache(bool (*original)(CvUnit*, CvPlot*, int), CvUnit* self, CvPlot* pDestPlot, int iFlags);CivRegisterBefore<CivHooks::CvUnit_UpdatePathCache>(host, &BeforeCvUnit_UpdatePathCache);CivRegisterAfter<CivHooks::CvUnit_UpdatePathCache>(host, &AfterCvUnit_UpdatePathCache);CivRegisterReplace<CivHooks::CvUnit_UpdatePathCache>(host, &ReplaceCvUnit_UpdatePathCache);
Code
22705bool CvUnit::UpdatePathCache(CvPlot* pDestPlot, int iFlags)22706{22707 bool bGenerated = false;22708 CvMap& kMap = GC.getMap();22709 // If we are in UNITFLAG_EVALUATING_MISSION mode, we can assume that other than the unit that is moving, nothing on the map will change so we can re-use the cached path data more efficiently.22710 if ( (m_iFlags & UNITFLAG_EVALUATING_MISSION) != 0 )22711 {22712 // Some pathing data left? (the last node is always the current units location)22713 if (m_kLastPath.size() >= 2)22714 {22715 // Was the path destination invisible at the time of generation? See if it is visible now.22716 CvPlot* pkPathDest;22717 if (m_kLastPath[0].GetFlag((int)CvPathNode::PLOT_INVISIBLE) && (pkPathDest = kMap.plot(m_kLastPath[0].m_iX, m_kLastPath[0].m_iY)) != NULL && pkPathDest->isVisible(getTeam()))22718 {22719 // The path destination is now visible, recalculate now in case it can't be reached (occupied)22720 if ((bGenerated = GeneratePath(pkPathDest, iFlags)) == false && pDestPlot != pkPathDest)22721 {22722 // Hmm, failed for some reason, re-do the entire path22723 bGenerated = GeneratePath(pDestPlot, iFlags);22724 }22725 }22726 else22727 {22728 // Was the next plot we are stepping into invisible at the time of generation?22729 if (m_kLastPath[m_kLastPath.size() - 2].GetFlag((int)CvPathNode::PLOT_INVISIBLE))22730 {22731 // We are trying to move into a plot that is invisible and we want to continue out into the darkness. We have to recalculate our path.22732 // Since we have already done a path find to the destination once, we can now just path find to how far out we can get in this turn.22733 const CvPathNode* pkPathNode = m_kLastPath.GetTurnDest(1);22734 if (pkPathNode)22735 {22736 CvPlot* pkTurnDest = kMap.plot(pkPathNode->m_iX, pkPathNode->m_iY);22737 if (pkTurnDest && (bGenerated = GeneratePath(pkTurnDest, iFlags)) == false && pDestPlot != pkTurnDest)22738 {22739 // Hmm, failed for some reason, re-do the entire path22740 bGenerated = GeneratePath(pDestPlot, iFlags);22741 }22742 }22743 else22744 {22745 bGenerated = GeneratePath(pDestPlot, iFlags); // Can't find the dest in the path, regenerate22746 }22747 }22748 else22749 {22750 // The path is still good, just use the next node22751 bGenerated = true;22752 }22753 }22754 }22755 else22756 {22757 // If we have already tried this, don't waste time and do it again.22758 if (m_uiLastPathCacheDest != pDestPlot->GetPlotIndex()) 22759 {22760 bGenerated = GeneratePath(pDestPlot, iFlags); // Need to regenerate22761 }22762 }22763 }22764 else22765 {22766 // In this case, we can't be sure about the state of the map, so do a full recalculate.22767 bGenerated = GeneratePath(pDestPlot, iFlags);22768 }2276922770 return bGenerated;22771}