Hooks
static bool BeforeCvAIEscortedOperation_Init(CvAIEscortedOperation* self, int iID, PlayerTypes eOwner, PlayerTypes arg2, int arg3, CvCity* arg4, CvCity* arg5);static void AfterCvAIEscortedOperation_Init(CvAIEscortedOperation* self, int iID, PlayerTypes eOwner, PlayerTypes arg2, int arg3, CvCity* arg4, CvCity* arg5);static void ReplaceCvAIEscortedOperation_Init(void (*original)(CvAIEscortedOperation*, int, PlayerTypes, PlayerTypes, int, CvCity*, CvCity*), CvAIEscortedOperation* self, int iID, PlayerTypes eOwner, PlayerTypes arg2, int arg3, CvCity* arg4, CvCity* arg5);CivRegisterBefore<CivHooks::CvAIEscortedOperation_Init>(host, &BeforeCvAIEscortedOperation_Init);CivRegisterAfter<CivHooks::CvAIEscortedOperation_Init>(host, &AfterCvAIEscortedOperation_Init);CivRegisterReplace<CivHooks::CvAIEscortedOperation_Init>(host, &ReplaceCvAIEscortedOperation_Init);
Code
2755/// Kick off this operation2756void CvAIEscortedOperation::Init(int iID, PlayerTypes eOwner, PlayerTypes /* eEnemy */, int /* iDefaultArea */, CvCity* /*pTarget*/, CvCity* /*pMuster*/)2757{2758 CvUnit* pOurCivilian;2759 CvPlot* pTargetSite, *pNewTarget;27602761 Reset();2762 m_eMoveType = AI_OPERATION_MOVETYPE_SINGLE_HEX;2763 m_iID = iID;2764 m_eOwner = eOwner;27652766 // Find the free civilian (that triggered this operation)2767 pOurCivilian = FindBestCivilian();27682769 if(pOurCivilian != NULL && iID != -1)2770 {2771 // Find a destination (not worrying about safe paths)2772 pTargetSite = FindBestTarget(pOurCivilian, false);27732774 if(pTargetSite != NULL)2775 {2776 SetTargetPlot(pTargetSite);27772778 // create the armies that are needed and set the state to ARMYAISTATE_WAITING_FOR_UNITS_TO_REINFORCE2779 CvArmyAI* pArmyAI = GET_PLAYER(m_eOwner).addArmyAI();2780 if(pArmyAI)2781 {2782 m_viArmyIDs.push_back(pArmyAI->GetID());2783 pArmyAI->Init(pArmyAI->GetID(),m_eOwner,m_iID);2784 pArmyAI->SetArmyAIState(ARMYAISTATE_WAITING_FOR_UNITS_TO_REINFORCE);2785 pArmyAI->SetFormationIndex(GetFormation());27862787 // Figure out the initial rally point - for this operation it is wherever our civilian is standing2788 pArmyAI->SetGoalPlot(pTargetSite);2789 CvPlot* pMusterPt = pOurCivilian->plot();2790 SetMusterPlot(pMusterPt);2791 pArmyAI->SetXY(pMusterPt->getX(), pMusterPt->getY());2792 SetDefaultArea(pMusterPt->getArea());27932794 // Add the civilian to our army2795 pArmyAI->AddUnit(pOurCivilian->GetID(), 0);27962797 // Skip escorting if this operation has to go across water2798 if (pTargetSite->getArea() != pOurCivilian->getArea())2799 {2800 m_bEscorted = false;2801 }28022803 if (m_bEscorted)2804 {2805 m_viListOfUnitsWeStillNeedToBuild.clear();2806 OperationSlot thisOperationSlot;2807 thisOperationSlot.m_iOperationID = m_iID;2808 thisOperationSlot.m_iArmyID = pArmyAI->GetID();2809 thisOperationSlot.m_iSlotID = 1;2810 m_viListOfUnitsWeStillNeedToBuild.push_back(thisOperationSlot);28112812 // try to get the escort from existing units that are waiting around2813 GrabUnitsFromTheReserves(pMusterPt, pTargetSite);2814 if(pArmyAI->GetNumSlotsFilled() > 1)2815 {2816 pArmyAI->SetArmyAIState(ARMYAISTATE_WAITING_FOR_UNITS_TO_CATCH_UP);2817 m_eCurrentState = AI_OPERATION_STATE_GATHERING_FORCES;2818 }2819 else2820 {2821 m_bEscorted = false;2822 }2823 }28242825 if (!m_bEscorted)2826 {2827 // There was no escort immediately available. Let's look for a "safe" city site instead2828 pNewTarget = FindBestTarget(pOurCivilian, true);28292830 // If no better target, we'll wait it out for an escort2831 if(pNewTarget == NULL)2832 {2833 m_bEscorted = true;28342835 // Need to add it back in to list of what to build (was cleared before since marked optional)2836 m_viListOfUnitsWeStillNeedToBuild.clear();2837 OperationSlot thisOperationSlot2;2838 thisOperationSlot2.m_iOperationID = m_iID;2839 thisOperationSlot2.m_iArmyID = pArmyAI->GetID();2840 thisOperationSlot2.m_iSlotID = 1;2841 m_viListOfUnitsWeStillNeedToBuild.push_back(thisOperationSlot2);2842 m_eCurrentState = AI_OPERATION_STATE_RECRUITING_UNITS;2843 }28442845 // Send the civilian by himself to this safe location2846 else2847 {2848 // Clear the list of units we need2849 m_viListOfUnitsWeStillNeedToBuild.clear();28502851 // Change the muster point2852 pArmyAI->SetGoalPlot(pNewTarget);2853 SetMusterPlot(pOurCivilian->plot());2854 pArmyAI->SetXY(GetMusterPlot()->getX(), GetMusterPlot()->getY());28552856 // Send the civilian directly to the target2857 pArmyAI->SetArmyAIState(ARMYAISTATE_MOVING_TO_DESTINATION);2858 m_eCurrentState = AI_OPERATION_STATE_MOVING_TO_TARGET;2859 }2860 }2861 LogOperationStart();2862 }2863 }28642865 else2866 {2867 // Lost our target, abort2868 m_eCurrentState = AI_OPERATION_STATE_ABORTED;2869 m_eAbortReason = AI_ABORT_LOST_TARGET;2870 }2871 }2872}