Bazaar developer reference

CvMinorCivAI::DoFirstContactWithMajor(TeamTypes, bool)

Defined in
CvMinorCivAI.cpp, line 2440
Class
CvMinorCivAI
Hook name
CivHooks::CvMinorCivAI_DoFirstContactWithMajor
Lua events
GameEvents.MinorGift

Hand-placed hooks inside

A replacement for this function that does not call original stops these hooks from running.

Hooks

static bool BeforeCvMinorCivAI_DoFirstContactWithMajor(CvMinorCivAI* self, TeamTypes eTeam, bool bSuppressMessages);static void AfterCvMinorCivAI_DoFirstContactWithMajor(CvMinorCivAI* self, TeamTypes eTeam, bool bSuppressMessages);static void ReplaceCvMinorCivAI_DoFirstContactWithMajor(void (*original)(CvMinorCivAI*, TeamTypes, bool), CvMinorCivAI* self, TeamTypes eTeam, bool bSuppressMessages);CivRegisterBefore<CivHooks::CvMinorCivAI_DoFirstContactWithMajor>(host, &BeforeCvMinorCivAI_DoFirstContactWithMajor);CivRegisterAfter<CivHooks::CvMinorCivAI_DoFirstContactWithMajor>(host, &AfterCvMinorCivAI_DoFirstContactWithMajor);CivRegisterReplace<CivHooks::CvMinorCivAI_DoFirstContactWithMajor>(host, &ReplaceCvMinorCivAI_DoFirstContactWithMajor);

Code

2439/// First contact2440void CvMinorCivAI::DoFirstContactWithMajor(TeamTypes eTeam, bool bSuppressMessages)2441{2442	// Set intrusion flag (used for unit movement)2443	for(int iPlayerLoop = 0; iPlayerLoop < MAX_MAJOR_CIVS; iPlayerLoop++)2444	{2445		PlayerTypes ePlayer = (PlayerTypes) iPlayerLoop;2446		if(GET_PLAYER(ePlayer).getTeam() == eTeam)2447		{2448			if(GET_PLAYER(ePlayer).isHuman())2449			{2450				// Humans intrude automatically upon meeting (allowing them to enter our territory if they choose)2451				SetMajorIntruding(ePlayer, true);2452			}2453			else2454			{2455				SetMajorIntruding(ePlayer, false);2456			}2457		}2458	}24592460	// This guy's a warmonger or at war with our ally, so we DoW him2461	if(IsPeaceBlocked(eTeam))2462	{2463#if defined(MOD_EVENTS_WAR_AND_PEACE)2464		GET_TEAM(GetPlayer()->getTeam()).declareWar(eTeam, false, GetPlayer()->GetID());2465#else2466		GET_TEAM(GetPlayer()->getTeam()).declareWar(eTeam);2467#endif2468	}2469	// Normal diplo2470	else2471	{2472#if defined(MOD_GLOBAL_CS_GIFTS)2473		// Ideally we want the actual meeting player, but we'll have to settle for the lead player of the team2474		// most of the time they will be one and the same2475		PlayerTypes eMeetingPlayer = GET_TEAM(eTeam).getLeaderID();2476		CvPlayer* pMeetingPlayer = &GET_PLAYER(eMeetingPlayer);2477		if (!(pMeetingPlayer->isMinorCiv() || pMeetingPlayer->isBarbarian())) {2478			MinorCivTraitTypes eTrait = GetTrait();2479			MinorCivPersonalityTypes eRealPersonality = GetPersonality();2480			MinorCivPersonalityTypes eFakePersonality = eRealPersonality;24812482			int iFriendshipBoost = 0;2483			int iCultureGift = 0;2484			int iFoodGift = 0;2485			int iUnitGift = 0;24862487			int iGift = 0;2488			char* szTxtKeySuffix = "UNKNOWN";2489#endif2490			int iGoldGift = 0;2491			int iFaithGift = 0;2492			bool bFirstMajorCiv = false;24932494			// If this guy has been mean then no Gold gifts2495			if(!GET_TEAM(eTeam).IsMinorCivAggressor())2496			{2497#if defined(MOD_GLOBAL_CS_GIFTS)2498				// Gift depends on the type of city state met2499				if (MOD_GLOBAL_CS_GIFTS) {2500					bFirstMajorCiv = (GET_TEAM(GetPlayer()->getTeam()).getHasMetCivCount(true) == 0);2501					iFriendshipBoost = GC.getMINOR_CIV_FIRST_CONTACT_BONUS_FRIENDSHIP(); // All CS types give an initial friendship boost25022503					if (eTrait == MINOR_CIV_TRAIT_CULTURED) {2504						iCultureGift = GC.getMINOR_CIV_FIRST_CONTACT_BONUS_CULTURE();2505						szTxtKeySuffix = "CULTURE";2506					} else if (eTrait == MINOR_CIV_TRAIT_RELIGIOUS) {2507						iFaithGift = GC.getMINOR_CIV_FIRST_CONTACT_BONUS_FAITH();2508						szTxtKeySuffix = "FAITH";2509					} else if (eTrait == MINOR_CIV_TRAIT_MERCANTILE) {2510						iGoldGift = GC.getMINOR_CIV_FIRST_CONTACT_BONUS_GOLD();2511						szTxtKeySuffix = "GOLD";2512					} else if (eTrait == MINOR_CIV_TRAIT_MARITIME) {2513						iFoodGift = GC.getMINOR_CIV_FIRST_CONTACT_BONUS_FOOD();2514						szTxtKeySuffix = "FOOD";2515					} else if (eTrait == MINOR_CIV_TRAIT_MILITARISTIC) {2516						iUnitGift = GC.getMINOR_CIV_FIRST_CONTACT_BONUS_UNIT();2517						szTxtKeySuffix = "UNIT";2518					}2519				}2520#endif25212522				// Hasn't met anyone yet?2523				if(GET_TEAM(GetPlayer()->getTeam()).getHasMetCivCount(true) == 0)2524				{2525#if defined(MOD_GLOBAL_CS_GIFTS)2526					// If we're not using the new gift system, just stick with the default gold and faith gifts2527					if (!MOD_GLOBAL_CS_GIFTS) {2528#endif2529						iGoldGift = /*60*/ GC.getMINOR_CIV_CONTACT_GOLD_FIRST();2530						if(GetTrait() == MINOR_CIV_TRAIT_RELIGIOUS)2531							iFaithGift = 8; //antonjs: todo: XML2532#if defined(MOD_GLOBAL_CS_GIFTS)2533					}2534#endif2535					2536					bFirstMajorCiv = true;2537				}2538				else2539				{2540#if defined(MOD_GLOBAL_CS_GIFTS)2541					if (MOD_GLOBAL_CS_GIFTS) {2542						// Reduce gifts if we're not the first team to meet the CS2543						int iBonusMultiplier = GC.getMINOR_CIV_FIRST_CONTACT_SUBSEQUENT_TEAM_MULTIPLIER();2544						int iBonusDivisor = GC.getMINOR_CIV_FIRST_CONTACT_SUBSEQUENT_TEAM_DIVISOR();25452546						if (iBonusDivisor == 0) {2547							iFriendshipBoost = 0;2548						} else {2549							iFriendshipBoost = iFriendshipBoost * iBonusMultiplier / iBonusDivisor;2550						}25512552						iCultureGift = iCultureGift / iBonusDivisor;2553						iFaithGift = iFaithGift / iBonusDivisor;25542555						iGoldGift = 0;2556						iFoodGift = 0;2557						iUnitGift = 0;2558					} else {2559#endif2560						iGoldGift = /*30*/ GC.getMINOR_CIV_CONTACT_GOLD_OTHER();2561						if(GetTrait() == MINOR_CIV_TRAIT_RELIGIOUS)2562							iFaithGift = 4; //antonjs: todo: XML2563#if defined(MOD_GLOBAL_CS_GIFTS)2564					}2565#endif2566				}25672568#if defined(MOD_GLOBAL_CS_GIFTS)2569				if (MOD_GLOBAL_CS_GIFTS) {2570					if (eRealPersonality == MINOR_CIV_PERSONALITY_IRRATIONAL) {2571						// Assumes MINOR_CIV_PERSONALITY_IRRATIONAL is the last entry in the enum2572						eFakePersonality = (MinorCivPersonalityTypes) GC.getGame().getJonRandNum(NUM_MINOR_CIV_PERSONALITY_TYPES-1, "Minor Civ AI: Picking fake personality for irrational CS");2573					}2574					2575		 			// Personality modifiers - friendly = x1.5, hostile = x0.52576					if (eFakePersonality == MINOR_CIV_PERSONALITY_FRIENDLY) {2577						int iBonusMultiplier = GC.getMINOR_CIV_FIRST_CONTACT_FRIENDLY_BONUS_MULTIPLIER();2578						int iBonusDivisor = GC.getMINOR_CIV_FIRST_CONTACT_FRIENDLY_BONUS_DIVISOR();25792580						if (iBonusDivisor == 0) {2581							iFriendshipBoost = 0;2582							2583							iCultureGift = 0;2584							iFaithGift = 0;2585							iGoldGift = 0;2586							iFoodGift = 0;2587						} else {2588							iFriendshipBoost = iFriendshipBoost * iBonusMultiplier / iBonusDivisor;2589							2590							iCultureGift = iCultureGift * iBonusMultiplier / iBonusDivisor;2591							iFaithGift = iFaithGift * iBonusMultiplier / iBonusDivisor;2592							iGoldGift = iGoldGift * iBonusMultiplier / iBonusDivisor;2593							iFoodGift = iFoodGift * iBonusMultiplier / iBonusDivisor;2594						}25952596						int iUnitMultiplier = GC.getMINOR_CIV_FIRST_CONTACT_FRIENDLY_UNIT_MULTIPLIER();2597						int iUnitDivisor = GC.getMINOR_CIV_FIRST_CONTACT_FRIENDLY_UNIT_DIVISOR();25982599						if (iUnitDivisor == 0) {2600							iUnitGift = 0;2601						} else {2602							iUnitGift = iUnitGift * iUnitMultiplier / iUnitDivisor;2603						}2604					} else if (eFakePersonality == MINOR_CIV_PERSONALITY_HOSTILE) {2605						int iBonusMultiplier = GC.getMINOR_CIV_FIRST_CONTACT_HOSTILE_BONUS_MULTIPLIER();2606						int iBonusDivisor = GC.getMINOR_CIV_FIRST_CONTACT_HOSTILE_BONUS_DIVISOR();26072608						if (iBonusDivisor == 0) {2609							iFriendshipBoost = 0;2610							2611							iCultureGift = 0;2612							iFaithGift = 0;2613							iGoldGift = 0;2614							iFoodGift = 0;2615						} else {2616							iFriendshipBoost = iFriendshipBoost * iBonusMultiplier / iBonusDivisor;26172618							iCultureGift = iCultureGift * iBonusMultiplier / iBonusDivisor;2619							iFaithGift = iFaithGift * iBonusMultiplier / iBonusDivisor;2620							iGoldGift = iGoldGift * iBonusMultiplier / iBonusDivisor;2621							iFoodGift = iFoodGift * iBonusMultiplier / iBonusDivisor;2622						}26232624						int iUnitMultiplier = GC.getMINOR_CIV_FIRST_CONTACT_HOSTILE_UNIT_MULTIPLIER();2625						int iUnitDivisor = GC.getMINOR_CIV_FIRST_CONTACT_HOSTILE_UNIT_DIVISOR();26262627						if (iUnitDivisor == 0) {2628							iUnitGift = 0;2629						} else {2630							iUnitGift = iUnitGift * iUnitMultiplier / iUnitDivisor;2631						}2632					}2633				}2634#endif2635			}26362637#if defined(MOD_GLOBAL_CS_GIFTS)2638			if (MOD_GLOBAL_CS_GIFTS) {2639				CUSTOMLOG("CS Gift: Id %i, Trait %i, Personality %i: %sFriendship=%i, Gold=%i, Culture=%i, Faith=%i, Food=%i, Unit=%i", GetPlayer()->GetID(), eTrait, eRealPersonality, (bFirstMajorCiv ? "First " : ""), iFriendshipBoost, iGoldGift, iCultureGift, iFaithGift, iFoodGift, iUnitGift);2640			}2641#endif26422643	 		PlayerTypes ePlayer;2644 			for(int iPlayerLoop = 0; iPlayerLoop < MAX_MAJOR_CIVS; iPlayerLoop++)2645			{2646				ePlayer = (PlayerTypes) iPlayerLoop;26472648				if(GET_PLAYER(ePlayer).getTeam() == eTeam)2649				{2650#if defined(MOD_GLOBAL_CS_GIFTS)2651					if (MOD_GLOBAL_CS_GIFTS) {2652						// Give the friendship boost to this team member2653						ChangeFriendshipWithMajor(ePlayer, iFriendshipBoost, /*bFromQuest*/ false);26542655						// Give the gifts to this team member2656						GET_PLAYER(ePlayer).GetTreasury()->ChangeGold(iGoldGift);2657						GET_PLAYER(ePlayer).changeJONSCulture(iCultureGift);2658						GET_PLAYER(ePlayer).ChangeFaith(iFaithGift);26592660						iGift = iGoldGift + iCultureGift + iFaithGift;26612662						// Food and unit gifts only go to the player who actually met the CS2663						if (eMeetingPlayer == ePlayer) {2664							int iBonusMultiplier = GC.getMINOR_CIV_FIRST_CONTACT_PLAYER_MULTIPLIER();2665							int iBonusDivisor = GC.getMINOR_CIV_FIRST_CONTACT_PLAYER_DIVISOR();26662667							// Give the friendship boost again to this team member (ie the meeting player gets twice the friendship boost)2668							int iExtraFriendship = (iFriendshipBoost * iBonusMultiplier / iBonusDivisor) - iFriendshipBoost;2669							ChangeFriendshipWithMajor(ePlayer, iExtraFriendship, /*bFromQuest*/ false);2670							iFriendshipBoost = iFriendshipBoost + iExtraFriendship;  // Need this adjusting for the popup dialog26712672							CvPlayer* pPlayer = &GET_PLAYER(ePlayer);2673							if (eTrait == MINOR_CIV_TRAIT_MILITARISTIC) {2674								if (iUnitGift > 0) {2675									if (GC.getGame().getJonRandNum(100, "Minor Civ AI: Decide if we give a unit to the meeting player") < iUnitGift) {2676										CvUnit* pUnit = DoSpawnUnit(ePlayer, true, true);2677										if (pUnit != NULL) {2678#if defined(MOD_UNITS_XP_TIMES_100)2679											pUnit->changeExperienceTimes100(100 * (pPlayer->GetCurrentEra() * GC.getMINOR_CIV_FIRST_CONTACT_XP_PER_ERA() + GC.getGame().getJonRandNum(GC.getMINOR_CIV_FIRST_CONTACT_XP_RANDOM(), "Minor Civ AI: Random XP for unit")));2680#else2681											pUnit->changeExperience(pPlayer->GetCurrentEra() * GC.getMINOR_CIV_FIRST_CONTACT_XP_PER_ERA() + GC.getGame().getJonRandNum(GC.getMINOR_CIV_FIRST_CONTACT_XP_RANDOM(), "Minor Civ AI: Random XP for unit"));2682#endif2683											iGift = pUnit->getUnitType();2684										}2685									}2686								}2687							} else if (eTrait == MINOR_CIV_TRAIT_MARITIME && pPlayer->getCapitalCity() != NULL) {2688								iGift = iGift + iFoodGift;26892690								if (iFoodGift > 0) {2691									CvCity* pMinorCapital = GetPlayer()->getCapitalCity();2692									CvPlot* pPlot = (pMinorCapital == NULL) ? GetPlayer()->getStartingPlot() : pMinorCapital->plot();26932694									CvCity* pBestCity = NULL;26952696									if (pPlayer->GetCurrentEra() < GC.getInfoTypeForString("ERA_MEDIEVAL") && pPlayer->getCapitalCity()->plot()->getArea() == pPlot->getArea()) {2697										// Pre-Medieval and on the same landmass, just add the food to the capital2698										pBestCity = pPlayer->getCapitalCity();2699									} else {2700										// Ripped from CvPlayer::receiveGoody()2701										int iDistance;2702										int iBestCityDistance = -1;27032704										CvCity* pLoopCity;2705										int iLoop;2706										// Find the closest City to us2707										for (pLoopCity = pPlayer->firstCity(&iLoop); pLoopCity != NULL; pLoopCity = pPlayer->nextCity(&iLoop)) {2708											iDistance = plotDistance(pPlot->getX(), pPlot->getY(), pLoopCity->getX(), pLoopCity->getY());27092710											if (iBestCityDistance == -1 || iDistance < iBestCityDistance) {2711												iBestCityDistance = iDistance;2712												pBestCity = pLoopCity;2713											}2714										}2715									}27162717									if (pBestCity != NULL) {2718										pBestCity->changeFood(iFoodGift);2719									}2720								}2721							}2722						}2723					} else {2724#endif2725						// Gold gift2726						GET_PLAYER(ePlayer).GetTreasury()->ChangeGold(iGoldGift);27272728						// Faith gift2729						if(iFaithGift > 0)2730							GET_PLAYER(ePlayer).ChangeFaith(iFaithGift);2731#if defined(MOD_GLOBAL_CS_GIFTS)2732					}2733#endif27342735					// Need to seed quest counter?2736					if(GC.getGame().getElapsedGameTurns() > GetFirstPossibleTurnForPersonalQuests())2737					{2738						DoTestSeedQuestCountdownForPlayer(ePlayer);2739					}27402741					// See if Threatening Barbarians event is active2742					if(GetTurnsSinceThreatenedAnnouncement() >= 0 && GetTurnsSinceThreatenedAnnouncement() < 10)2743					{2744						DoTestThreatenedAnnouncementForPlayer(ePlayer);2745					}27462747					// See if Proxy War event is active2748					DoTestProxyWarAnnouncementOnFirstContact(ePlayer);27492750					// See if there are any quests you can join now2751					DoTestQuestsOnFirstContact(ePlayer);27522753#if defined(CIV_HOST_API)2754					if(!bSuppressMessages)2755					{2756#if defined(MOD_GLOBAL_CS_GIFTS)2757						if (MOD_GLOBAL_CS_GIFTS)2758							CivHandPopupRaised(ePlayer, BUTTONPOPUP_CITY_STATE_GREETING, GetPlayer()->GetID(), iGift, iFriendshipBoost, bFirstMajorCiv, false, szTxtKeySuffix);hand-placed hook: PopupRaised2759						else2760#endif2761							CivHandPopupRaised(ePlayer, BUTTONPOPUP_CITY_STATE_GREETING, GetPlayer()->GetID(), iGoldGift, iFaithGift, bFirstMajorCiv, false, "");hand-placed hook: PopupRaised2762					}2763#endif27642765					// Greeting for active human player2766					if(ePlayer == GC.getGame().getActivePlayer() && !bSuppressMessages)2767					{2768						if(!GC.getGame().isNetworkMultiPlayer())	// KWG: Should this be !GC.getGame().isMPOption(MPOPTION_SIMULTANEOUS_TURNS)2769						{2770#if defined(MOD_GLOBAL_CS_GIFTS)2771							/* Current assignments of members of popupInfo2772							 *   Data1 is the player id2773							 *   Data2 is the gold gift value2774							 *   Data3 is the faith gift value2775							 *   Option1 is first met2776							 *   Option2 is nil2777							 *   Text is nil2778							 *2779							 * Updated assignments of members of popupInfo2780							 *   Data1 is the player id (unchanged)2781							 *   Data2 is the gift "value" (Gold/Culture/Faith/Food amount, UnitId)2782							 *   Data3 is the friendship boost2783							 *   Option1 is first met (unchanged)2784							 *   Option2 is nil (unchanged)2785							 *   Text is suffix for the TXT_KEY_ to format with2786							 */2787							if (MOD_GLOBAL_CS_GIFTS) {2788								CvPopupInfo kPopupInfo(BUTTONPOPUP_CITY_STATE_GREETING, GetPlayer()->GetID(), iGift, iFriendshipBoost, 0, bFirstMajorCiv);2789								strcpy_s(kPopupInfo.szText, szTxtKeySuffix);2790								GC.GetEngineUserInterface()->AddPopup(kPopupInfo);2791							} else {2792#endif2793								CvPopupInfo kPopupInfo(BUTTONPOPUP_CITY_STATE_GREETING, GetPlayer()->GetID(), iGoldGift, iFaithGift, 0, bFirstMajorCiv);2794								GC.GetEngineUserInterface()->AddPopup(kPopupInfo);2795#if defined(MOD_GLOBAL_CS_GIFTS)2796							}2797#endif2798							// We are adding a popup that the player must make a choice in, make sure they are not in the end-turn phase.2799							CancelActivePlayerEndTurn();2800						}2801						2802						// update the mouseover text for the city-state's city banners2803						int iLoop = 0;2804						CvCity* pLoopCity = NULL;2805						for(pLoopCity = m_pPlayer->firstCity(&iLoop); pLoopCity != NULL; pLoopCity = m_pPlayer->nextCity(&iLoop))2806						{2807							if(pLoopCity->plot()->isRevealed(eTeam))2808							{2809								auto_ptr<ICvCity1> pDllLoopCity = GC.WrapCityPointer(pLoopCity);2810								GC.GetEngineUserInterface()->SetSpecificCityInfoDirty(pDllLoopCity.get(), CITY_UPDATE_TYPE_BANNER);2811							}2812						}2813					}28142815#if defined(MOD_GLOBAL_CS_GIFTS)2816					if (MOD_GLOBAL_CS_GIFTS && MOD_EVENTS_MINORS_GIFTS)2817					{2818						// Send an event with the details2819						GAMEEVENTINVOKE_HOOK(GAMEEVENT_MinorGift, GetPlayer()->GetID(), ePlayer, iGift, iFriendshipBoost, 0, bFirstMajorCiv, false, szTxtKeySuffix);2820					}2821#endif2822				}2823			}2824#if defined(MOD_GLOBAL_CS_GIFTS)2825		}2826#endif2827	}2828}

Calls

Called by