Hooks
static bool BeforeCvMilitaryAI_BuyEmergencyUnit(CvMilitaryAI* self, UnitAITypes eUnitType, CvCity* pCity, CvUnit*& result);static void AfterCvMilitaryAI_BuyEmergencyUnit(CvMilitaryAI* self, UnitAITypes eUnitType, CvCity* pCity, CvUnit*& result);static CvUnit* ReplaceCvMilitaryAI_BuyEmergencyUnit(CvUnit* (*original)(CvMilitaryAI*, UnitAITypes, CvCity*), CvMilitaryAI* self, UnitAITypes eUnitType, CvCity* pCity);CivRegisterBefore<CivHooks::CvMilitaryAI_BuyEmergencyUnit>(host, &BeforeCvMilitaryAI_BuyEmergencyUnit);CivRegisterAfter<CivHooks::CvMilitaryAI_BuyEmergencyUnit>(host, &AfterCvMilitaryAI_BuyEmergencyUnit);CivRegisterReplace<CivHooks::CvMilitaryAI_BuyEmergencyUnit>(host, &ReplaceCvMilitaryAI_BuyEmergencyUnit);
Code
849/// Spend money to quickly add a unit to a city850CvUnit* CvMilitaryAI::BuyEmergencyUnit(UnitAITypes eUnitType, CvCity* pCity)851{852 bool bIsVenice = m_pPlayer->GetPlayerTraits()->IsNoAnnexing();853854 // No units in puppet cities except for Venice!855 if(pCity->IsPuppet() && !bIsVenice)856 {857 return NULL;858 }859860 // Get best unit with this AI type861 UnitTypes eType = pCity->GetCityStrategyAI()->GetUnitProductionAI()->RecommendUnit(eUnitType);862 if(eType != NO_UNIT)863 {864 // Can we buy the primary unit type at the start city?865 if(pCity->IsCanPurchase(/*bTestPurchaseCost*/ true, /*bTestTrainable*/ true, eType, NO_BUILDING, NO_PROJECT, YIELD_GOLD))866 {867 int iGoldCost = pCity->GetPurchaseCost(eType);868 int iPriority = GC.getAI_GOLD_PRIORITY_UNIT();869 if(m_pPlayer->GetEconomicAI()->CanWithdrawMoneyForPurchase(PURCHASE_TYPE_UNIT, iGoldCost, iPriority))870 {871 if(pCity->getOwner() == m_pPlayer->GetID()) // Player must own the city or this will create a unit for another player872 {873 // This is an EXTRA build for the operation beyond any that are already assigned to this city, so pass in the right flag to CreateUnit()874 int iResult = pCity->CreateUnit(eType, NO_UNITAI, false /*bUseToSatisfyOperation*/);875876 CvAssertMsg(iResult != FFreeList::INVALID_INDEX, "Unable to create unit");877878 if (iResult != FFreeList::INVALID_INDEX)879 {880 CvUnit* pUnit = m_pPlayer->getUnit(iResult);881 m_pPlayer->GetTreasury()->LogExpenditure((CvString)pUnit->getUnitInfo().GetText(), iGoldCost, 7);882 m_pPlayer->GetTreasury()->ChangeGold(-iGoldCost);883884#if defined(MOD_BUGFIX_MOVE_AFTER_PURCHASE)885 if (!pUnit->getUnitInfo().CanMoveAfterPurchase())886 {887#endif888 pUnit->setMoves(0);889#if defined(MOD_BUGFIX_MOVE_AFTER_PURCHASE)890 }891#endif892893 CvString szMsg;894 szMsg.Format("Emergency Unit Purchased: %s, ", pUnit->getUnitInfo().GetDescription());895 szMsg += pCity->getName();896 m_pPlayer->GetTacticalAI()->LogTacticalMessage(szMsg);897898 return pUnit;899 }900 else901 {902 return NULL;903 }904 }905 }906 }907908 // Try again with Faith909 if(pCity->IsCanPurchase(/*bTestPurchaseCost*/ true, /*bTestTrainable*/ true, eType, NO_BUILDING, NO_PROJECT, YIELD_FAITH))910 {911 int iFaithCost = pCity->GetFaithPurchaseCost(eType, false /*bIncludeBeliefDiscounts*/);912913 if(pCity->getOwner() == m_pPlayer->GetID()) // Player must own the city or this will create a unit for another player914 {915 m_pPlayer->ChangeFaith(-iFaithCost);916917 // This is an EXTRA build for the operation beyond any that are already assigned to this city, so pass in the right flag to CreateUnit()918 int iResult = pCity->CreateUnit(eType, NO_UNITAI, false /*bUseToSatisfyOperation*/);919920 CvAssertMsg(iResult != FFreeList::INVALID_INDEX, "Unable to create unit");921 CvUnit* pUnit = m_pPlayer->getUnit(iResult);922#if defined(MOD_BUGFIX_MOVE_AFTER_PURCHASE)923 if (!pUnit->getUnitInfo().CanMoveAfterPurchase())924 {925#endif926 pUnit->setMoves(0);927#if defined(MOD_BUGFIX_MOVE_AFTER_PURCHASE)928 }929#endif930931 CvString szMsg;932 szMsg.Format("Emergency Faith Unit Purchase: %s, ", pUnit->getUnitInfo().GetDescription());933 szMsg += pCity->getName();934 m_pPlayer->GetTacticalAI()->LogTacticalMessage(szMsg);935936 return pUnit;937 }938 }939 }940 return NULL;941}