Bazaar developer reference

CvAIOperationMerchantDelegation::ArmyInPosition(CvArmyAI *)

Defined in
CvAIOperation.cpp, line 3446
Class
CvAIOperationMerchantDelegation
Hook name
CivHooks::CvAIOperationMerchantDelegation_ArmyInPosition
Overrides
CvAIOperation::ArmyInPosition(CvArmyAI *)

Hooks

static bool BeforeCvAIOperationMerchantDelegation_ArmyInPosition(CvAIOperationMerchantDelegation* self, CvArmyAI* pArmy, bool& result);static void AfterCvAIOperationMerchantDelegation_ArmyInPosition(CvAIOperationMerchantDelegation* self, CvArmyAI* pArmy, bool& result);static bool ReplaceCvAIOperationMerchantDelegation_ArmyInPosition(bool (*original)(CvAIOperationMerchantDelegation*, CvArmyAI*), CvAIOperationMerchantDelegation* self, CvArmyAI* pArmy);CivRegisterBefore<CivHooks::CvAIOperationMerchantDelegation_ArmyInPosition>(host, &BeforeCvAIOperationMerchantDelegation_ArmyInPosition);CivRegisterAfter<CivHooks::CvAIOperationMerchantDelegation_ArmyInPosition>(host, &AfterCvAIOperationMerchantDelegation_ArmyInPosition);CivRegisterReplace<CivHooks::CvAIOperationMerchantDelegation_ArmyInPosition>(host, &ReplaceCvAIOperationMerchantDelegation_ArmyInPosition);

Code

3445/// If at target, cash in; if at muster point, merge merchant and escort and move out3446bool CvAIOperationMerchantDelegation::ArmyInPosition(CvArmyAI* pArmy)3447{3448	int iUnitID = 0;3449	bool bStateChanged = false;3450	CvUnit* pMerchant = 0, *pEscort = 0;3451	CvString strMsg;34523453	switch(m_eCurrentState)3454	{3455		// If we were gathering forces, we have to insist that any escort is in the same plot as the merchant.3456		// If not we'll fall through and just stay in this state.3457	case AI_OPERATION_STATE_GATHERING_FORCES:34583459		// No escort, can just let base class handle it3460		if(!m_bEscorted)3461		{3462			return CvAIOperation::ArmyInPosition(pArmy);3463		}34643465		// More complex if we are waiting for an escort3466		else3467		{3468			iUnitID = pArmy->GetFirstUnitID();3469			if(iUnitID != -1)3470			{3471				pMerchant = GET_PLAYER(m_eOwner).getUnit(iUnitID);3472			}3473			iUnitID = pArmy->GetNextUnitID();3474			if(iUnitID != -1)3475			{3476				pEscort = GET_PLAYER(m_eOwner).getUnit(iUnitID);3477			}3478			else3479			{3480				// Escort died while gathering forces.  Abort (and return TRUE since state changed)3481				m_eCurrentState = AI_OPERATION_STATE_ABORTED;3482				m_eAbortReason = AI_ABORT_ESCORT_DIED;3483				return true;3484			}3485			if(pMerchant != NULL && pEscort != NULL && pMerchant->plot() == pEscort->plot())3486			{3487				// let's see if the target still makes sense (this is modified from RetargetCivilian)3488				CvPlot* pBetterTarget = FindBestTarget(pMerchant, true);34893490				// No targets at all!  Abort3491				if(pBetterTarget == NULL)3492				{3493					m_eCurrentState = AI_OPERATION_STATE_ABORTED;3494					m_eAbortReason = AI_ABORT_NO_TARGET;3495					return false;3496				}3497				// If we have a target3498				else3499				{3500					SetTargetPlot(pBetterTarget);3501					pArmy->SetGoalPlot(pBetterTarget);3502				}3503				return CvAIOperation::ArmyInPosition(pArmy);3504			}3505		}3506		break;35073508	case AI_OPERATION_STATE_MOVING_TO_TARGET:3509	case AI_OPERATION_STATE_AT_TARGET:35103511		// Call base class version and see if it thinks we're done3512		bStateChanged = CvAIOperation::ArmyInPosition(pArmy);35133514		// Now get the merchant3515		iUnitID = pArmy->GetFirstUnitID();3516		if(iUnitID != -1)3517		{3518			pMerchant = GET_PLAYER(m_eOwner).getUnit(iUnitID);3519		}35203521		if(pMerchant != NULL)3522		{3523			// If the merchant made it, we don't care about the entire army3524			if(pMerchant->plot() == GetTargetPlot() && pMerchant->canMove() && pMerchant->canTrade(pMerchant->plot()))3525			{3526				if (pMerchant->canBuyCityState(pMerchant->plot()) && !GET_PLAYER(m_eOwner).GreatMerchantWantsCash())3527				{3528					pMerchant->PushMission(CvTypes::getMISSION_BUY_CITY_STATE());3529					if(GC.getLogging() && GC.getAILogging())3530					{3531						strMsg.Format("Great Merchant buying city-state, At X=%d, At Y=%d", pMerchant->plot()->getX(), pMerchant->plot()->getY());3532						LogOperationSpecialMessage(strMsg);3533					}3534				}3535				else3536				{3537					pMerchant->PushMission(CvTypes::getMISSION_TRADE());3538					if(GC.getLogging() && GC.getAILogging())3539					{3540						strMsg.Format("Great Merchant finishing trade mission, At X=%d, At Y=%d", pMerchant->plot()->getX(), pMerchant->plot()->getY());3541						LogOperationSpecialMessage(strMsg);3542					}3543				}35443545				m_eCurrentState = AI_OPERATION_STATE_SUCCESSFUL_FINISH;3546			}35473548			// Does it look like we should be done?3549			else if(pMerchant->plot() == GetTargetPlot())3550			{3551				// We're at our target but can no longer trade, city state was probably conquered3552				if(!pMerchant->canTrade(pMerchant->plot()))3553				{3554					if(GC.getLogging() && GC.getAILogging())3555					{3556						strMsg.Format("At target but can no longer trade here. Target was (X=%d Y=%d)", GetTargetPlot()->getX(), GetTargetPlot()->getY());3557						LogOperationSpecialMessage(strMsg);3558					}3559					RetargetCivilian(pMerchant, pArmy);3560					pMerchant->finishMoves();3561					iUnitID = pArmy->GetNextUnitID();3562					if(iUnitID != -1)3563					{3564						pEscort = GET_PLAYER(m_eOwner).getUnit(iUnitID);3565						pEscort->finishMoves();3566					}3567				}3568			}3569		}3570		break;35713572		// In all other cases use base class version3573	case AI_OPERATION_STATE_ABORTED:3574	case AI_OPERATION_STATE_RECRUITING_UNITS:3575		return CvAIOperation::ArmyInPosition(pArmy);3576		break;3577	};35783579	return bStateChanged;3580}

Calls