Hooks
static bool BeforeCvUnit_jumpToNearestValidPlotWithinRange(CvUnit* self, int iRange, bool& result);static void AfterCvUnit_jumpToNearestValidPlotWithinRange(CvUnit* self, int iRange, bool& result);static bool ReplaceCvUnit_jumpToNearestValidPlotWithinRange(bool (*original)(CvUnit*, int), CvUnit* self, int iRange);CivRegisterBefore<CivHooks::CvUnit_jumpToNearestValidPlotWithinRange>(host, &BeforeCvUnit_jumpToNearestValidPlotWithinRange);CivRegisterAfter<CivHooks::CvUnit_jumpToNearestValidPlotWithinRange>(host, &AfterCvUnit_jumpToNearestValidPlotWithinRange);CivRegisterReplace<CivHooks::CvUnit_jumpToNearestValidPlotWithinRange>(host, &ReplaceCvUnit_jumpToNearestValidPlotWithinRange);
Code
3765bool CvUnit::jumpToNearestValidPlotWithinRange(int iRange)3766{3767 VALIDATE_OBJECT3768 CvPlot* pLoopPlot;3769 CvPlot* pBestPlot;3770 int iValue;3771 int iBestValue;37723773 CvAssertMsg(!isAttacking(), "isAttacking did not return false as expected");3774 CvAssertMsg(!isFighting(), "isFighting did not return false as expected");37753776 iBestValue = INT_MAX;3777 pBestPlot = NULL;37783779 for(int iDX = -iRange; iDX <= iRange; iDX++)3780 {3781 for(int iDY = -iRange; iDY <= iRange; iDY++)3782 {3783 pLoopPlot = plotXYWithRangeCheck(getX(), getY(), iDX, iDY, iRange);37843785 if(pLoopPlot != NULL)3786 {3787 if(pLoopPlot->isValidDomainForLocation(*this))3788 {3789 if(canMoveInto(*pLoopPlot))3790 {3791#if defined(MOD_GLOBAL_STACKING_RULES)3792 if(pLoopPlot->getNumFriendlyUnitsOfType(this) < pLoopPlot->getUnitLimit())3793#else3794 if(pLoopPlot->getNumFriendlyUnitsOfType(this) < GC.getPLOT_UNIT_LIMIT())3795#endif3796 {3797 // Can only jump to a plot if we can enter the territory, and it's NOT enemy territory OR we're a barb3798 if(canEnterTerritory(pLoopPlot->getTeam()) && (isBarbarian() || !isEnemy(pLoopPlot->getTeam(), pLoopPlot)))3799 {3800 CvAssertMsg(!atPlot(*pLoopPlot), "atPlot(pLoopPlot) did not return false as expected");38013802 if((getDomainType() != DOMAIN_AIR) || pLoopPlot->isFriendlyCity(*this, true))3803 {3804 if(pLoopPlot->isRevealed(getTeam()))3805 {3806 iValue = (plotDistance(getX(), getY(), pLoopPlot->getX(), pLoopPlot->getY()) * 2);38073808 if(pLoopPlot->area() != area())3809 {3810 iValue *= 3;3811 }38123813 if(iValue < iBestValue)3814 {3815 iBestValue = iValue;3816 pBestPlot = pLoopPlot;3817 }3818 }3819 }3820 }3821 }3822 }3823 }3824 }3825 }3826 }38273828 if(pBestPlot != NULL)3829 {3830 if(GC.getLogging() && GC.getAILogging())3831 {3832 CvString strLogString;3833 strLogString.Format("Jump to nearest valid plot within range by %s , X: %d, Y: %d, From X: %d, From Y: %d", getName().GetCString(),3834 pBestPlot->getX(), pBestPlot->getY(), getX(), getY());3835#if defined(MOD_BUGFIX_USE_GETTERS)3836 GET_MY_PLAYER().GetHomelandAI()->LogHomelandMessage(strLogString);3837#else3838 GET_PLAYER(m_eOwner).GetHomelandAI()->LogHomelandMessage(strLogString);3839#endif3840 }3841 setXY(pBestPlot->getX(), pBestPlot->getY(), false, true, pBestPlot->isVisibleToWatchingHuman(), false);3842 }3843 else3844 {3845 if(GC.getLogging() && GC.getAILogging())3846 {3847 CvString strLogString;3848 strLogString.Format("Can't find a valid plot within range for %s, X: %d, Y: %d", getName().GetCString(), getX(), getY());3849#if defined(MOD_BUGFIX_USE_GETTERS)3850 GET_MY_PLAYER().GetHomelandAI()->LogHomelandMessage(strLogString);3851#else3852 GET_PLAYER(m_eOwner).GetHomelandAI()->LogHomelandMessage(strLogString);3853#endif3854 }3855 CUSTOMLOG("jumpToNearestValidPlotWithinRange(%i) failed for unit %s at plot (%i, %i)", iRange, getName().GetCString(), getX(), getY());3856 return false;3857 }38583859 return true;3860}