Hooks
static bool BeforeCvCity_CreateUnit(CvCity* self, UnitTypes eUnitType, UnitAITypes eAIType, bool bUseToSatisfyOperation, int& result);static void AfterCvCity_CreateUnit(CvCity* self, UnitTypes eUnitType, UnitAITypes eAIType, bool bUseToSatisfyOperation, int& result);static int ReplaceCvCity_CreateUnit(int (*original)(CvCity*, UnitTypes, UnitAITypes, bool), CvCity* self, UnitTypes eUnitType, UnitAITypes eAIType, bool bUseToSatisfyOperation);CivRegisterBefore<CivHooks::CvCity_CreateUnit>(host, &BeforeCvCity_CreateUnit);CivRegisterAfter<CivHooks::CvCity_CreateUnit>(host, &AfterCvCity_CreateUnit);CivRegisterReplace<CivHooks::CvCity_CreateUnit>(host, &ReplaceCvCity_CreateUnit);
Code
13544int CvCity::CreateUnit(UnitTypes eUnitType, UnitAITypes eAIType, bool bUseToSatisfyOperation)13545{13546 VALIDATE_OBJECT13547 CvPlayer& thisPlayer = GET_PLAYER(getOwner());13548 CvUnit* pUnit = thisPlayer.initUnit(eUnitType, getX(), getY(), eAIType);13549 CvAssertMsg(pUnit, "");13550 if(!pUnit)13551 {13552 CvAssertMsg(false, "CreateUnit failed");13553 return FFreeList::INVALID_INDEX;13554 }1355513556 if(pUnit->IsHasNoValidMove())13557 {13558 pUnit->kill(false);13559 return FFreeList::INVALID_INDEX;13560 }1356113562 addProductionExperience(pUnit);1356313564 CvPlot* pRallyPlot = getRallyPlot();13565 if(pRallyPlot != NULL)13566 {13567 pUnit->PushMission(CvTypes::getMISSION_MOVE_TO(), pRallyPlot->getX(), pRallyPlot->getY());13568 }1356913570 if(bUseToSatisfyOperation && m_unitBeingBuiltForOperation.IsValid())13571 {13572 thisPlayer.CityFinishedBuildingUnitForOperationSlot(m_unitBeingBuiltForOperation, pUnit);13573 m_unitBeingBuiltForOperation.Invalidate();13574 }1357513576 // Any AI unit with explore AI as a secondary unit AI (e.g. warriors) are assigned that unit AI if this AI player needs to explore more13577 else if(!pUnit->isHuman() && !thisPlayer.isMinorCiv())13578 {13579 EconomicAIStrategyTypes eStrategy = (EconomicAIStrategyTypes) GC.getInfoTypeForString("ECONOMICAISTRATEGY_NEED_RECON");13580 if(thisPlayer.GetEconomicAI()->IsUsingStrategy(eStrategy))13581 {13582 if(pUnit->getUnitInfo().GetUnitAIType(UNITAI_EXPLORE) && pUnit->AI_getUnitAIType() != UNITAI_EXPLORE)13583 {1358413585 // Now make sure there isn't a critical military threat13586 CvMilitaryAI* thisPlayerMilAI = thisPlayer.GetMilitaryAI();13587 int iThreat = thisPlayerMilAI->GetThreatTotal();13588 iThreat += thisPlayerMilAI->GetBarbarianThreatTotal();13589 if(iThreat < thisPlayerMilAI->GetThreatWeight(THREAT_CRITICAL))13590 {13591 pUnit->AI_setUnitAIType(UNITAI_EXPLORE);13592 if(GC.getLogging() && GC.getAILogging())13593 {13594 CvString strLogString;13595 strLogString.Format("Assigning explore unit AI to %s, X: %d, Y: %d", pUnit->getName().GetCString(), pUnit->getX(), pUnit->getY());13596 thisPlayer.GetHomelandAI()->LogHomelandMessage(strLogString);13597 }13598 }13599 else13600 {13601 if(GC.getLogging() && GC.getAILogging())13602 {13603 CvString strLogString;13604 strLogString.Format("Not assigning explore AI to %s due to threats, X: %d, Y: %d", pUnit->getName().GetCString(), pUnit->getX(), pUnit->getY());13605 thisPlayer.GetHomelandAI()->LogHomelandMessage(strLogString);13606 }13607 }13608 }13609 }13610 eStrategy = (EconomicAIStrategyTypes) GC.getInfoTypeForString("ECONOMICAISTRATEGY_NEED_RECON_SEA");13611 EconomicAIStrategyTypes eOtherStrategy = (EconomicAIStrategyTypes) GC.getInfoTypeForString("ECONOMICAISTRATEGY_REALLY_NEED_RECON_SEA");13612 if(thisPlayer.GetEconomicAI()->IsUsingStrategy(eStrategy) || thisPlayer.GetEconomicAI()->IsUsingStrategy(eOtherStrategy))13613 {13614 if(pUnit->getUnitInfo().GetUnitAIType(UNITAI_EXPLORE_SEA))13615 {13616 pUnit->AI_setUnitAIType(UNITAI_EXPLORE_SEA);13617 if(GC.getLogging() && GC.getAILogging())13618 {13619 CvString strLogString;13620 strLogString.Format("Assigning explore sea unit AI to %s, X: %d, Y: %d", pUnit->getName().GetCString(), pUnit->getX(), pUnit->getY());13621 thisPlayer.GetHomelandAI()->LogHomelandMessage(strLogString);13622 }13623 }13624 }13625 }13626 //Increment for stat tracking and achievements13627 if(pUnit->isHuman())13628 {13629 IncrementUnitStatCount(pUnit);13630 }1363113632 return pUnit->GetID();13633}