Code
4064/// Moves a great general into an important city to aid its defense4065void CvHomelandAI::ExecuteGeneralMoves()4066{4067 FStaticVector< CvHomelandUnit, 64, true, c_eCiv5GameplayDLL >::iterator it;4068 CvPlot* pHolyCityPlot = NULL;4069 CvCity* pHolyCity = NULL;40704071 // Do we have an Apollo program to stay clear of?4072 bool bHaveApolloInCapital = false;4073 ProjectTypes eApolloProgram = (ProjectTypes) GC.getSPACE_RACE_TRIGGER_PROJECT();4074 if(eApolloProgram != NO_PROJECT)4075 {4076 if(GET_TEAM(m_pPlayer->getTeam()).getProjectCount(eApolloProgram) > 0)4077 {4078 bHaveApolloInCapital = true;4079 }4080 }40814082 // Do we have a holy city to stay clear of?4083 bool bKeepHolyCityClear = false;4084 CvGameReligions* pReligions = GC.getGame().GetGameReligions();4085 ReligionTypes eMyReligion = pReligions->GetReligionCreatedByPlayer(m_pPlayer->GetID());4086 const CvReligion* pMyReligion = pReligions->GetReligion(eMyReligion, m_pPlayer->GetID());4087 if(pMyReligion)4088 {4089 pHolyCityPlot = GC.getMap().plot(pMyReligion->m_iHolyCityX, pMyReligion->m_iHolyCityY);4090 pHolyCity = pHolyCityPlot->getPlotCity();4091 if(pHolyCity && (pHolyCity->getOwner() == m_pPlayer->GetID()))4092 {4093 bKeepHolyCityClear = true;4094 }4095 }40964097 for(it = m_CurrentMoveUnits.begin(); it != m_CurrentMoveUnits.end(); ++it)4098 {4099 UnitHandle pUnit = m_pPlayer->getUnit(it->GetID());4100 if(!pUnit)4101 {4102 continue;4103 }41044105 // this is for the citadel/culture bomb4106 if (pUnit->GetGreatPeopleDirective() == GREAT_PEOPLE_DIRECTIVE_USE_POWER)4107 {4108 int iValue = 0;4109 CvPlot* pTargetPlot = GET_PLAYER(m_pPlayer->GetID()).FindBestArtistTargetPlot(pUnit.pointer(), iValue);4110 if(pTargetPlot)4111 {4112 if(pUnit->plot() == pTargetPlot)4113 {4114 // find the great general improvement4115 BuildTypes eSelectedBuildType = NO_BUILD;4116 BuildTypes eBuild;4117 int iBuildIndex;4118 for(iBuildIndex = 0; iBuildIndex < GC.getNumBuildInfos(); iBuildIndex++)4119 {4120 eBuild = (BuildTypes)iBuildIndex;4121 CvBuildInfo* pkBuild = GC.getBuildInfo(eBuild);4122 if(pkBuild == NULL)4123 continue;4124 4125 if(!pUnit->canBuild(pTargetPlot, eBuild))4126 {4127 continue;4128 }4129 4130 eSelectedBuildType = eBuild;4131 break;4132 }41334134 CvAssertMsg(eSelectedBuildType != NO_BUILD, "Great General trying to build something it doesn't qualify for");4135 if (eSelectedBuildType != NO_BUILD)4136 {4137 pUnit->PushMission(CvTypes::getMISSION_BUILD(), eSelectedBuildType, -1, 0, (pUnit->GetLengthMissionQueue() > 0), false, MISSIONAI_BUILD, pTargetPlot);4138 if(GC.getLogging() && GC.getAILogging())4139 {4140 CvString strLogString;4141 strLogString.Format("Great General culture bombed/citadel'd at, X: %d, Y: %d", pUnit->getX(), pUnit->getY());4142 LogHomelandMessage(strLogString);4143 }4144 }4145 }4146 else4147 {4148 pUnit->PushMission(CvTypes::getMISSION_MOVE_TO(), pTargetPlot->getX(), pTargetPlot->getY());4149 pUnit->finishMoves();4150 UnitProcessed(pUnit->GetID());41514152 if(GC.getLogging() && GC.getAILogging())4153 {4154 CvString strLogString;4155 strLogString.Format("Great general moving to culture bomb/citadel at, X: %d, Y: %d", pUnit->getX(), pUnit->getY());4156 LogHomelandMessage(strLogString);4157 }4158 }4159 continue;4160 }4161 }416241634164 if(pUnit->GetGreatPeopleDirective() == GREAT_PEOPLE_DIRECTIVE_GOLDEN_AGE)4165 {4166 ExecuteGoldenAgeMove(pUnit.pointer());4167 continue;4168 }41694170 // if we already built the Apollo Program we don't want the general in the capital because it'll block spaceship parts41714172 // Already in a friendly city?4173 CvPlot* pUnitPlot = pUnit->plot();4174 if(pUnitPlot->isFriendlyCity(*pUnit, false) && (!bHaveApolloInCapital || !pUnitPlot->getPlotCity()->isCapital()) && (!bKeepHolyCityClear || pUnitPlot != pHolyCityPlot))4175 {4176 pUnit->finishMoves();4177 UnitProcessed(pUnit->GetID());4178 if(GC.getLogging() && GC.getAILogging())4179 {4180 CvString strLogString;4181 strLogString.Format("Great General remaining as garrison for %s, X: %d, Y: %d", pUnit->plot()->getPlotCity()->getName().GetCString(), pUnit->getX(), pUnit->getY());4182 LogHomelandMessage(strLogString);4183 }4184 }41854186 else4187 {4188 CvCity* pLoopCity;4189 int iLoopCity = 0;4190 int iBestTurns = MAX_INT;4191 CvPlot* pBestCityPlot = NULL;4192 for(pLoopCity = m_pPlayer->firstCity(&iLoopCity); pLoopCity != NULL; pLoopCity = m_pPlayer->nextCity(&iLoopCity))4193 {4194 if(bHaveApolloInCapital && pLoopCity->isCapital())4195 {4196 continue;4197 }41984199 if(bKeepHolyCityClear && pLoopCity == pHolyCity)4200 {4201 continue;4202 }42034204 bool bSkipCity = false;42054206 CvPlot* pTarget = pLoopCity->plot();4207 for(int iUnitLoop = 0; iUnitLoop < pTarget->getNumUnits(); iUnitLoop++)4208 {4209 // Don't go here if a general or admiral is already present4210 if(pTarget->getUnitByIndex(iUnitLoop)->AI_getUnitAIType() == UNITAI_GENERAL)4211 {4212 bSkipCity = true;4213 break;4214 }4215 else if(pTarget->getUnitByIndex(iUnitLoop)->AI_getUnitAIType() == UNITAI_ADMIRAL)4216 {4217 bSkipCity = true;4218 break;4219 }4220 }42214222 if(!bSkipCity)4223 {4224 int iTurns = TurnsToReachTarget(pUnit, pTarget);4225 if(iTurns < iBestTurns)4226 {4227 iBestTurns = iTurns;4228 pBestCityPlot = pTarget;4229 }4230 }4231 }42324233 if(pBestCityPlot)4234 {4235 pUnit->PushMission(CvTypes::getMISSION_MOVE_TO(), pBestCityPlot->getX(), pBestCityPlot->getY());4236 pUnit->finishMoves();4237 UnitProcessed(pUnit->GetID());42384239 if(GC.getLogging() && GC.getAILogging())4240 {4241 CvString strLogString;4242 strLogString.Format("Moving Great General to city garrison, X: %d, Y: %d", pBestCityPlot->getX(), pBestCityPlot->getY());4243 LogHomelandMessage(strLogString);4244 }4245 }4246 else4247 {4248 if(GC.getLogging() && GC.getAILogging())4249 {4250 CvString strLogString;4251 strLogString.Format("No place to move Great General at, X: %d, Y: %d", pUnit->getX(), pUnit->getY());4252 LogHomelandMessage(strLogString);4253 }4254 }4255 }4256 }4257}