Hooks
static bool BeforeCvBuilderTaskingAI_AddImprovingResourcesDirectives(CvBuilderTaskingAI* self, CvUnit* pUnit, CvPlot* pPlot, int iMoveTurnsAway);static void AfterCvBuilderTaskingAI_AddImprovingResourcesDirectives(CvBuilderTaskingAI* self, CvUnit* pUnit, CvPlot* pPlot, int iMoveTurnsAway);static void ReplaceCvBuilderTaskingAI_AddImprovingResourcesDirectives(void (*original)(CvBuilderTaskingAI*, CvUnit*, CvPlot*, int), CvBuilderTaskingAI* self, CvUnit* pUnit, CvPlot* pPlot, int iMoveTurnsAway);CivRegisterBefore<CivHooks::CvBuilderTaskingAI_AddImprovingResourcesDirectives>(host, &BeforeCvBuilderTaskingAI_AddImprovingResourcesDirectives);CivRegisterAfter<CivHooks::CvBuilderTaskingAI_AddImprovingResourcesDirectives>(host, &AfterCvBuilderTaskingAI_AddImprovingResourcesDirectives);CivRegisterReplace<CivHooks::CvBuilderTaskingAI_AddImprovingResourcesDirectives>(host, &ReplaceCvBuilderTaskingAI_AddImprovingResourcesDirectives);
Code
877/// Evaluating a plot to see if we can build resources there878void CvBuilderTaskingAI::AddImprovingResourcesDirectives(CvUnit* pUnit, CvPlot* pPlot, int iMoveTurnsAway)879{880 ImprovementTypes eExistingPlotImprovement = pPlot->getImprovementType();881882 // Do we have a special improvement here? (great person improvement, gifted improvement from major civ)883 if(eExistingPlotImprovement != NO_IMPROVEMENT && pPlot->HasSpecialImprovement() && !pPlot->IsImprovementPillaged())884 {885 return;886 }887888 // check to see if a resource is here. If not, bail out!889 ResourceTypes eResource = pPlot->getResourceType(m_pPlayer->getTeam());890 if(eResource == NO_RESOURCE)891 {892 return;893 }894895 CvResourceInfo* pkResource = GC.getResourceInfo(eResource);896 if(pkResource->getResourceUsage() == RESOURCEUSAGE_BONUS)897 {898 // evaluate bonus resources as normal improvements899 return;900 }901902 // loop through the build types to find one that we can use903 BuildTypes eBuild;904 BuildTypes eOriginalBuild;905 int iBuildIndex;906 for(iBuildIndex = 0; iBuildIndex < GC.getNumBuildInfos(); iBuildIndex++)907 {908 eBuild = (BuildTypes)iBuildIndex;909 CvBuildInfo* pkBuild = GC.getBuildInfo(eBuild);910 if(pkBuild == NULL)911 continue;912913 eOriginalBuild = eBuild;914 ImprovementTypes eImprovement = (ImprovementTypes)pkBuild->getImprovement();915 if(eImprovement == NO_IMPROVEMENT)916 {917 continue;918 }919920 CvImprovementEntry* pkImprovementInfo = GC.getImprovementInfo(eImprovement);921 if(pkImprovementInfo == NULL || !pkImprovementInfo->IsImprovementResourceTrade(eResource))922 {923 continue;924 }925926 if(eImprovement == eExistingPlotImprovement)927 {928 if(pPlot->IsImprovementPillaged())929 {930 eBuild = m_eRepairBuild;931 }932 else933 {934 // this plot already has the appropriate improvement to use the resource935 break;936 }937 }938 else939 {940 // Do we have a special improvement here? (great person improvement, gifted improvement from major civ)941 if (eExistingPlotImprovement != NO_IMPROVEMENT && pPlot->HasSpecialImprovement())942 continue;943 }944945 if(!pUnit->canBuild(pPlot, eBuild))946 {947 break;948 }949950 BuilderDirective::BuilderDirectiveType eDirectiveType = BuilderDirective::BUILD_IMPROVEMENT_ON_RESOURCE;951 int iWeight = GC.getBUILDER_TASKING_BASELINE_BUILD_RESOURCE_IMPROVEMENTS();952 if(eBuild == m_eRepairBuild)953 {954 eDirectiveType = BuilderDirective::REPAIR;955 iWeight = GC.getBUILDER_TASKING_BASELINE_REPAIR();956 }957958 iWeight = GetBuildCostWeight(iWeight, pPlot, eBuild);959960 // this is to deal with when the plot is already improved with another improvement that doesn't enable the resource961 int iInvestedImprovementTime = 0;962 if(eExistingPlotImprovement != NO_IMPROVEMENT)963 {964 BuildTypes eExistingBuild = NO_BUILD;965 BuildTypes eBuild2 = NO_BUILD;966 for(int iBuildIndex2 = 0; iBuildIndex2 < GC.getNumBuildInfos(); iBuildIndex2++)967 {968 eBuild2 = (BuildTypes)iBuildIndex2;969 CvBuildInfo* pkBuild2 = GC.getBuildInfo(eBuild2);970 if(pkBuild2 && pkBuild2->getImprovement() == eExistingPlotImprovement)971 {972 eExistingBuild = eBuild2;973 break;974 }975 }976977 if(eExistingBuild != NO_BUILD)978 {979 iInvestedImprovementTime = pPlot->getBuildTime(eExistingBuild, m_pPlayer->GetID());980 }981 }982983 int iBuildTimeWeight = GetBuildTimeWeight(pUnit, pPlot, eBuild, DoesBuildHelpRush(pUnit, pPlot, eBuild), iInvestedImprovementTime + iMoveTurnsAway);984 iWeight += iBuildTimeWeight;985 iWeight = CorrectWeight(iWeight);986987 iWeight += GetResourceWeight(eResource, eImprovement, pPlot->getNumResource());988 iWeight = CorrectWeight(iWeight);989990 UpdateProjectedPlotYields(pPlot, eBuild);991 int iScore = ScorePlot();992 if(iScore > 0)993 {994 iWeight *= iScore;995 iWeight = CorrectWeight(iWeight);996 }997998 {999 CvCity* pLogCity = NULL;1000 int iProduction = pPlot->getFeatureProduction(eBuild, pUnit->getOwner(), &pLogCity);1001 if(DoesBuildHelpRush(pUnit, pPlot, eBuild))1002 {1003 iWeight += iProduction; // a nominal benefit for choosing this production10041005 if(m_bLogging)1006 {1007 CvString strLog;1008 strLog.Format("Helps rush, %d", iProduction);1009 LogInfo(strLog, m_pPlayer);1010 }1011 }1012 }10131014 if(iWeight <= 0)1015 {1016 continue;1017 }10181019 BuilderDirective directive;1020 directive.m_eDirective = eDirectiveType;1021 directive.m_eBuild = eBuild;1022 directive.m_eResource = eResource;1023 directive.m_sX = pPlot->getX();1024 directive.m_sY = pPlot->getY();1025 //directive.m_iGoldCost = m_pPlayer->getBuildCost(pPlot, eBuild);1026 directive.m_sMoveTurnsAway = iMoveTurnsAway;10271028 if(m_bLogging)1029 {1030 CvString strTemp;1031 strTemp.Format("%d, Build Time Weight, %d, Weight, %d", pUnit->GetID(), iBuildTimeWeight, iWeight);1032 LogInfo(strTemp, m_pPlayer);1033 }10341035 m_aDirectives.push_back(directive, iWeight);1036 }1037}