Bazaar developer reference

CvPlayerEspionage::AttemptCoup(uint)

Defined in
CvEspionageClasses.cpp, line 2229
Class
CvPlayerEspionage
Hook name
CivHooks::CvPlayerEspionage_AttemptCoup

Hooks

static bool BeforeCvPlayerEspionage_AttemptCoup(CvPlayerEspionage* self, uint uiSpyIndex, bool& result);static void AfterCvPlayerEspionage_AttemptCoup(CvPlayerEspionage* self, uint uiSpyIndex, bool& result);static bool ReplaceCvPlayerEspionage_AttemptCoup(bool (*original)(CvPlayerEspionage*, uint), CvPlayerEspionage* self, uint uiSpyIndex);CivRegisterBefore<CivHooks::CvPlayerEspionage_AttemptCoup>(host, &BeforeCvPlayerEspionage_AttemptCoup);CivRegisterAfter<CivHooks::CvPlayerEspionage_AttemptCoup>(host, &AfterCvPlayerEspionage_AttemptCoup);CivRegisterReplace<CivHooks::CvPlayerEspionage_AttemptCoup>(host, &ReplaceCvPlayerEspionage_AttemptCoup);

Code

2228/// AttemptCoup - Have a spy try to overthrow a city state. If success, the spy's owner becomes the ally. If failure, the spy dies.2229bool CvPlayerEspionage::AttemptCoup(uint uiSpyIndex)2230{2231	// if you're not allowed to stage a coup here, the coup fails2232	if(!CanStageCoup(uiSpyIndex))2233	{2234		return false;2235	}22362237	CvCity* pCity = GetCityWithSpy(uiSpyIndex);2238	CvAssertMsg(pCity, "Spy isn't in a city.");2239	if(!pCity)2240	{2241		return false;2242	}22432244	PlayerTypes eCityOwner = pCity->getOwner();2245	if(!GET_PLAYER(eCityOwner).isMinorCiv())2246	{2247		// this city state is not a minor civ2248		return false;2249	}22502251	CvMinorCivAI* pMinorCivAI = GET_PLAYER(eCityOwner).GetMinorCivAI();22522253	PlayerTypes ePreviousAlly = pMinorCivAI->GetAlly();2254	CvAssertMsg(ePreviousAlly != NO_PLAYER, "City state has no ally. Something's going wrong");2255	if(ePreviousAlly == NO_PLAYER)2256	{2257		return false;2258	}22592260	int aiNewInfluenceValueTimes100[MAX_MAJOR_CIVS];2261	for(uint ui = 0; ui < MAX_MAJOR_CIVS; ui++)2262	{2263		aiNewInfluenceValueTimes100[ui] = pMinorCivAI->GetEffectiveFriendshipWithMajorTimes100((PlayerTypes)ui);2264	}22652266	m_aSpyList[uiSpyIndex].m_bEvaluateReassignment = true; // flag for reassignment2267	if(GC.getLogging())2268	{2269		CvString strMsg;2270		strMsg.Format("Re-eval: attempting coup, %d,", uiSpyIndex);2271#if defined(MOD_BUGFIX_SPY_NAMES)2272		strMsg += GetLocalizedText(m_aSpyList[uiSpyIndex].GetSpyName(m_pPlayer));2273#else2274		strMsg += GetLocalizedText(m_pPlayer->getCivilizationInfo().getSpyNames(m_aSpyList[uiSpyIndex].m_iName));2275#endif2276		LogEspionageMsg(strMsg);2277	}22782279	bool bAttemptSuccess = false;2280	int iRandRoll = GC.getGame().getJonRandNum(100, "Roll for the result of an attempted coup");2281	if(iRandRoll <= GetCoupChanceOfSuccess(uiSpyIndex))2282	{2283		// swap influence from ally to 2nd place ally2284		int iInfluenceTemp = aiNewInfluenceValueTimes100[ePreviousAlly];2285		aiNewInfluenceValueTimes100[ePreviousAlly] = aiNewInfluenceValueTimes100[m_pPlayer->GetID()];2286		aiNewInfluenceValueTimes100[m_pPlayer->GetID()] = iInfluenceTemp;22872288		// reduce the influence of all the other players2289		for(uint ui = 0; ui < MAX_MAJOR_CIVS; ui++)2290		{2291			if(ui == m_pPlayer->GetID())2292			{2293				continue;2294			}22952296			// only drop the influence if they have positive influence2297			if(aiNewInfluenceValueTimes100[ui] > 0)2298			{2299				int iNewInfluence = aiNewInfluenceValueTimes100[ui] - (GC.getESPIONAGE_COUP_OTHER_PLAYERS_INFLUENCE_DROP() * 100);2300				iNewInfluence = max(iNewInfluence, 0);2301				aiNewInfluenceValueTimes100[ui] = iNewInfluence;2302			}2303		}23042305		bAttemptSuccess = true;2306	}2307	else2308	{2309		// reduce influence of player2310		// right now move the influence into a negative space2311		aiNewInfluenceValueTimes100[m_pPlayer->GetID()] = (-10 * 100);2312		bAttemptSuccess = false;23132314		// kill the spy2315		ExtractSpyFromCity(uiSpyIndex); // move the dead body out so that someone else can move in2316#if defined(MOD_API_ESPIONAGE)2317		m_aSpyList[uiSpyIndex].SetSpyState(m_pPlayer->GetID(), uiSpyIndex, SPY_STATE_DEAD); // have to official kill him after the extraction2318#else2319		m_aSpyList[uiSpyIndex].m_eSpyState = SPY_STATE_DEAD; // have to official kill him after the extraction2320#endif2321	}23222323	// do others influence first so that the potential coup person will be the ally2324	pMinorCivAI->SetDisableNotifications(true);2325	for(uint ui = 0; ui < MAX_MAJOR_CIVS; ui++)2326	{2327		PlayerTypes ePlayer = (PlayerTypes)ui;2328		if(!GET_PLAYER(ePlayer).isAlive())2329		{2330			continue;2331		}23322333		// skip the spy player2334		if(ePlayer == m_pPlayer->GetID())2335		{2336			continue;2337		}23382339		// send notification to other civs if they have met this minor2340		bool bMetMinor = GET_TEAM(GET_PLAYER(ePlayer).getTeam()).isHasMet(GET_PLAYER(eCityOwner).getTeam());2341		bool bNotify = false;2342		int iFriendship = pMinorCivAI->GetEffectiveFriendshipWithMajor(ePlayer);2343		int iRelationshipAnchor = pMinorCivAI->GetFriendshipAnchorWithMajor(ePlayer);2344		bool bFriends = pMinorCivAI->IsFriends(ePlayer);23452346		if (iFriendship > iRelationshipAnchor || bFriends)2347		{2348			bNotify = true;2349		}23502351		CvPlayerEspionage* pOtherEspionage = GET_PLAYER(ePlayer).GetEspionage();2352		int iSpyIndex = pOtherEspionage->GetSpyIndexInCity(pCity);2353		if (iSpyIndex >= 0)2354		{2355			bNotify = true;2356		}23572358		pMinorCivAI->SetFriendshipWithMajorTimes100(ePlayer, aiNewInfluenceValueTimes100[ui]);23592360		CvNotifications* pNotifications = GET_PLAYER(ePlayer).GetNotifications();2361		if(pNotifications && bMetMinor && bNotify)2362		{2363			NotificationTypes eNotification;2364			Localization::String strSummary;2365			Localization::String strNotification;2366			if(bAttemptSuccess)2367			{2368				eNotification = NOTIFICATION_SPY_STAGE_COUP_SUCCESS;2369				strSummary = Localization::Lookup("TXT_KEY_NOTIFICATION_SPY_STAGE_COUP_SUCCESS_S");2370				strSummary << m_pPlayer->getCivilizationAdjectiveKey();2371				strSummary << pCity->getNameKey();2372				strNotification = Localization::Lookup("TXT_KEY_NOTIFICATION_SPY_STAGE_COUP_SUCCESS");2373				strNotification << m_pPlayer->getCivilizationShortDescriptionKey();2374				strNotification << pCity->getNameKey();2375				strNotification << GET_PLAYER(ePreviousAlly).getCivilizationAdjectiveKey();2376			}2377			else2378			{2379				eNotification = NOTIFICATION_SPY_STAGE_COUP_FAILURE;2380				strSummary = Localization::Lookup("TXT_KEY_NOTIFICATION_SPY_STAGE_COUP_FAILURE_S");2381				strSummary << m_pPlayer->getCivilizationAdjectiveKey();2382				strSummary << pCity->getNameKey();2383				strNotification = Localization::Lookup("TXT_KEY_NOTIFICATION_SPY_STAGE_COUP_FAILURE");2384				strNotification << m_pPlayer->getCivilizationShortDescriptionKey();2385				strNotification << pCity->getNameKey();2386				strNotification << GET_PLAYER(ePreviousAlly).getCivilizationAdjectiveKey();2387			}2388			pNotifications->Add(eNotification, strNotification.toUTF8(), strSummary.toUTF8(), pCity->getX(), pCity->getY(), -1);2389		}2390	}23912392	pMinorCivAI->SetFriendshipWithMajorTimes100(m_pPlayer->GetID(), aiNewInfluenceValueTimes100[m_pPlayer->GetID()]);2393	pMinorCivAI->SetDisableNotifications(false);2394	// send notification to player2395	CvNotifications* pNotifications = m_pPlayer->GetNotifications();2396	if(pNotifications)2397	{2398		NotificationTypes eNotification;2399		Localization::String strSummary;2400		Localization::String strNotification;2401		if(bAttemptSuccess)2402		{2403			eNotification = NOTIFICATION_SPY_YOU_STAGE_COUP_SUCCESS;2404			strSummary = Localization::Lookup("TXT_KEY_NOTIFICATION_SPY_YOU_STAGE_COUP_SUCCESS_S");2405			strSummary << pCity->getNameKey();2406			strNotification = Localization::Lookup("TXT_KEY_NOTIFICATION_SPY_YOU_STAGE_COUP_SUCCESS");2407			strNotification << GetSpyRankName(m_aSpyList[uiSpyIndex].m_eRank);2408#if defined(MOD_BUGFIX_SPY_NAMES)2409			strNotification << m_aSpyList[uiSpyIndex].GetSpyName(m_pPlayer);2410#else2411			strNotification << m_pPlayer->getCivilizationInfo().getSpyNames(m_aSpyList[uiSpyIndex].m_iName);2412#endif2413			strNotification << pCity->getNameKey();2414			strNotification << GET_PLAYER(ePreviousAlly).getCivilizationAdjectiveKey();2415		}2416		else2417		{2418			eNotification = NOTIFICATION_SPY_YOU_STAGE_COUP_FAILURE;2419			strSummary = Localization::Lookup("TXT_KEY_NOTIFICATION_SPY_YOU_STAGE_COUP_FAILURE_S");2420			strSummary << pCity->getNameKey();2421			strNotification = Localization::Lookup("TXT_KEY_NOTIFICATION_SPY_YOU_STAGE_COUP_FAILURE");2422			strNotification << GetSpyRankName(m_aSpyList[uiSpyIndex].m_eRank);2423#if defined(MOD_BUGFIX_SPY_NAMES)2424			strNotification << m_aSpyList[uiSpyIndex].GetSpyName(m_pPlayer);2425#else2426			strNotification << m_pPlayer->getCivilizationInfo().getSpyNames(m_aSpyList[uiSpyIndex].m_iName);2427#endif2428			strNotification << pCity->getNameKey();2429			strNotification << GET_PLAYER(ePreviousAlly).getCivilizationAdjectiveKey();2430		}2431		pNotifications->Add(eNotification, strNotification.toUTF8(), strSummary.toUTF8(), pCity->getX(), pCity->getY(), -1);2432	}24332434#if !defined(NO_ACHIEVEMENTS)2435	//Achievements!2436	if(bAttemptSuccess && m_pPlayer->GetID() == GC.getGame().getActivePlayer())2437	{2438		gDLL->UnlockAchievement(ACHIEVEMENT_XP1_13);2439	}2440#endif24412442	// Update City banners and game info2443	GC.GetEngineUserInterface()->setDirty(GameData_DIRTY_BIT, true);2444	GC.GetEngineUserInterface()->setDirty(CityInfo_DIRTY_BIT, true);24452446	return bAttemptSuccess;2447}

Calls

Called by