Bazaar developer reference

CvPlayer::DoCivilianReturnLogic(bool, PlayerTypes, int)

Defined in
CvPlayer.cpp, line 19380
Class
CvPlayer
Hook name
CivHooks::CvPlayer_DoCivilianReturnLogic
Changed in
0.5.0 (added hand-placed hook UnitReplaced)

Hand-placed hooks inside

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

Hooks

static bool BeforeCvPlayer_DoCivilianReturnLogic(CvPlayer* self, bool bReturn, PlayerTypes eToPlayer, int iUnitID);static void AfterCvPlayer_DoCivilianReturnLogic(CvPlayer* self, bool bReturn, PlayerTypes eToPlayer, int iUnitID);static void ReplaceCvPlayer_DoCivilianReturnLogic(void (*original)(CvPlayer*, bool, PlayerTypes, int), CvPlayer* self, bool bReturn, PlayerTypes eToPlayer, int iUnitID);CivRegisterBefore<CivHooks::CvPlayer_DoCivilianReturnLogic>(host, &BeforeCvPlayer_DoCivilianReturnLogic);CivRegisterAfter<CivHooks::CvPlayer_DoCivilianReturnLogic>(host, &AfterCvPlayer_DoCivilianReturnLogic);CivRegisterReplace<CivHooks::CvPlayer_DoCivilianReturnLogic>(host, &ReplaceCvPlayer_DoCivilianReturnLogic);

Code

19379/// Someone sent us a present!19380void CvPlayer::DoCivilianReturnLogic(bool bReturn, PlayerTypes eToPlayer, int iUnitID)19381{19382	CvUnit* pUnit = getUnit(iUnitID);19383	if(!pUnit)19384	{19385		return;19386	}1938719388	CvPlot* pPlot = pUnit->plot();19389	if(!pPlot)19390	{19391		return;19392	}1939319394	// Kill any units this guy is transporting19395	IDInfo* pUnitNode = pPlot->headUnitNode();19396	while(pUnitNode != NULL)19397	{19398		CvUnit* pLoopUnit = ::getUnit(*pUnitNode);19399		pUnitNode = pPlot->nextUnitNode(pUnitNode);1940019401		if(NULL != pLoopUnit && pLoopUnit->getTransportUnit() == pUnit)19402			pLoopUnit->kill(true);19403	}1940419405	// What are the details for the new unit?19406	UnitTypes eNewUnitType = pUnit->getUnitType();1940719408	if(!bReturn)19409		eNewUnitType = pUnit->getCaptureUnitType(getCivilizationType());1941019411	int iX = pUnit->getX();19412	int iY = pUnit->getY();1941319414	// Returns to the previous owner19415	if(bReturn)19416	{19417		pUnit->kill(true);19418		CvUnit* pNewUnit = GET_PLAYER(eToPlayer).initUnit(eNewUnitType, iX, iY);19419#if defined(CIV_HOST_API)19420		if (pNewUnit)19421			CivHandUnitReplaced(GetID(), iUnitID, pNewUnit);hand-placed hook: UnitReplaced19422#endif19423		CvAssert(pNewUnit != NULL);19424		if (pNewUnit)19425		{19426			if (!pNewUnit->jumpToNearestValidPlot())19427				pNewUnit->kill(false);	// Could not find a spot!19428		}1942919430		// Returned to a city-state19431		if(GET_PLAYER(eToPlayer).isMinorCiv())19432		{19433			int iInfluence = /*45*/ GC.getRETURN_CIVILIAN_FRIENDSHIP();19434			GET_PLAYER(eToPlayer).GetMinorCivAI()->ChangeFriendshipWithMajor(GetID(), iInfluence);19435		}19436		// Returned to major power19437		else if(!GET_PLAYER(eToPlayer).isHuman())19438		{19439			GET_PLAYER(eToPlayer).GetDiplomacyAI()->ChangeNumCiviliansReturnedToMe(GetID(), 1);19440		}19441	}19442	// Kept for oneself19443	else19444	{19445		// Make a new unit because the kind we should capture doesn't match (e.g. Settler to Worker)19446		if(eNewUnitType != pUnit->getUnitType())19447		{19448#if defined(MOD_GLOBAL_GRATEFUL_SETTLERS)19449			// In OCC games, all captured settlers are converted19450			if ((pUnit->isFound() || pUnit->IsFoundAbroad()) && MOD_GLOBAL_GRATEFUL_SETTLERS && !(GC.getGame().isOption(GAMEOPTION_ONE_CITY_CHALLENGE) && isHuman())) {19451				int iDefectProb = gCustomMods.getOption("GLOBAL_GRATEFUL_SETTLERS_PERCENT", 20);19452				int iPercent = 0;1945319454				// Approach is very important19455				switch (GetDiplomacyAI()->GetMajorCivApproach(eToPlayer, false)) {19456					case MAJOR_CIV_APPROACH_WAR:19457						iPercent = iDefectProb * gCustomMods.getOption("GLOBAL_GRATEFUL_SETTLERS_WAR_MULTIPLIER", 0) / gCustomMods.getOption("GLOBAL_GRATEFUL_SETTLERS_DIVISOR", 8);19458						break;19459					case MAJOR_CIV_APPROACH_HOSTILE:19460						iPercent = iDefectProb * gCustomMods.getOption("GLOBAL_GRATEFUL_SETTLERS_HOSTILE_MULTIPLIER", 2) / gCustomMods.getOption("GLOBAL_GRATEFUL_SETTLERS_DIVISOR", 8);19461						break;19462					case MAJOR_CIV_APPROACH_GUARDED:19463						iPercent = iDefectProb * gCustomMods.getOption("GLOBAL_GRATEFUL_SETTLERS_GUARDED_MULTIPLIER", 4) / gCustomMods.getOption("GLOBAL_GRATEFUL_SETTLERS_DIVISOR", 8);19464						break;19465					case MAJOR_CIV_APPROACH_DECEPTIVE:19466						iPercent = iDefectProb * gCustomMods.getOption("GLOBAL_GRATEFUL_SETTLERS_DECEPTIVE_MULTIPLIER", 4) / gCustomMods.getOption("GLOBAL_GRATEFUL_SETTLERS_DIVISOR", 8);19467						break;19468					case MAJOR_CIV_APPROACH_AFRAID:19469						iPercent = iDefectProb * gCustomMods.getOption("GLOBAL_GRATEFUL_SETTLERS_AFRAID_MULTIPLIER", 4) / gCustomMods.getOption("GLOBAL_GRATEFUL_SETTLERS_DIVISOR", 8);19470						break;19471					case MAJOR_CIV_APPROACH_NEUTRAL:19472						iPercent = iDefectProb * gCustomMods.getOption("GLOBAL_GRATEFUL_SETTLERS_NEUTRAL_MULTIPLIER", 6) / gCustomMods.getOption("GLOBAL_GRATEFUL_SETTLERS_DIVISOR", 8);19473						break;19474					case MAJOR_CIV_APPROACH_FRIENDLY:19475						iPercent = iDefectProb * gCustomMods.getOption("GLOBAL_GRATEFUL_SETTLERS_FRIENDLY_MULTIPLIER", 10) / gCustomMods.getOption("GLOBAL_GRATEFUL_SETTLERS_DIVISOR", 8);19476						break;19477					default:19478						break;19479				}1948019481				// Opinion also matters19482				switch (GetDiplomacyAI()->GetMajorCivOpinion(eToPlayer)) {19483					case MAJOR_CIV_OPINION_ALLY:19484						iPercent += iDefectProb * 5 / 4;19485						break;19486					case MAJOR_CIV_OPINION_FRIEND:19487						iPercent += iDefectProb * 1;19488						break;19489					case MAJOR_CIV_OPINION_FAVORABLE:19490						iPercent += iDefectProb * 3 / 4;19491						break;19492					case MAJOR_CIV_OPINION_NEUTRAL:19493						iPercent += iDefectProb * 1 / 2;19494						break;19495					case MAJOR_CIV_OPINION_COMPETITOR:19496						iPercent += iDefectProb * 0;19497						break;19498					case MAJOR_CIV_OPINION_ENEMY:19499						iPercent += iDefectProb * -1;19500						break;19501					case MAJOR_CIV_OPINION_UNFORGIVABLE:19502						iPercent += iDefectProb * -1;19503						break;19504					default:19505						break;19506				}1950719508				// Limit the outcome19509				iPercent = std::min(50, std::max(5, iPercent));1951019511				// Our masters sent us into a war-zone, we'll stick where we are!19512				if (GetDiplomacyAI()->GetNumOurEnemiesPlayerAtWarWith(eToPlayer) > 0) {19513					iPercent *= 2;19514				}1951519516				// If they will found the second city (ie kudos) multiply by 1.519517				if (getNumCities() == 1) {19518					iPercent *= 3;19519					iPercent /= 2;19520				}1952119522#if defined(MOD_GLOBAL_RELIGIOUS_SETTLERS)19523				if (MOD_GLOBAL_RELIGIOUS_SETTLERS) {19524					ReligionTypes eReligion = pUnit->GetReligionData()->GetReligion();1952519526					if (eReligion > RELIGION_PANTHEON) {19527						const CvReligion* pkReligion = GC.getGame().GetGameReligions()->GetReligion(eReligion, NO_PLAYER);1952819529						if (pkReligion) {19530							CvPlot* pPlot = GC.getMap().plot(pkReligion->m_iHolyCityX, pkReligion->m_iHolyCityY);1953119532							if (pPlot) {19533								CvCity* pHolyCity = pPlot->getPlotCity();1953419535								if (pHolyCity->getOriginalOwner() == GetID()) {19536									// Bonus if the liberator founded their holy city19537									iPercent += gCustomMods.getOption("GLOBAL_GRATEFUL_SETTLERS_HOLYCITY_FOUNDER", 20);19538								} else if (pHolyCity->getOwner() == GetID()) {19539									// Serious bad karma if the liberator has captured their holy city19540									iPercent += gCustomMods.getOption("GLOBAL_GRATEFUL_SETTLERS_HOLYCITY_OCCUPIER", -20);19541								}19542							}19543						}19544					}19545				}19546#endif1954719548				// Limit the outcome19549				iPercent = std::min(80, iPercent);19550				19551				// Use the popularity difference between the players to skew the probability19552				int iSmileRatio = (GetExcessHappiness() * 100) / (std::max(0, GET_PLAYER(eToPlayer).GetExcessHappiness()) + 10);19553				iPercent = iPercent * std::min(150, std::max(75, iSmileRatio)) / 100;19554				CUSTOMLOG("Settler defect percent: %i (Approach=%i, Opinion=%i)", iPercent, GetDiplomacyAI()->GetMajorCivApproach(eToPlayer, false), GetDiplomacyAI()->GetMajorCivOpinion(eToPlayer));1955519556				if (GC.getGame().getJonRandNum(100, "Settlers defect") < iPercent) {19557					if (GC.getGame().getActivePlayer() == GetID()) {19558						CvPopupInfo kPopupInfo(BUTTONPOPUP_TEXT);19559						strcpy_s(kPopupInfo.szText, "TXT_KEY_GRATEFUL_SETTLERS");19560						GC.GetEngineUserInterface()->AddPopup(kPopupInfo);19561					}19562				} else {19563					pUnit->kill(true);19564					CvUnit* pNewUnit = initUnit(eNewUnitType, iX, iY);19565#if defined(CIV_HOST_API)19566					if (pNewUnit)19567						CivHandUnitReplaced(GetID(), iUnitID, pNewUnit);hand-placed hook: UnitReplaced19568#endif19569					CvAssert(pNewUnit != NULL);19570					if (pNewUnit)19571						pNewUnit->finishMoves();19572				}19573			} else {19574#endif19575				pUnit->kill(true);19576				CvUnit* pNewUnit = initUnit(eNewUnitType, iX, iY);19577#if defined(CIV_HOST_API)19578				if (pNewUnit)19579					CivHandUnitReplaced(GetID(), iUnitID, pNewUnit);hand-placed hook: UnitReplaced19580#endif19581				CvAssert(pNewUnit != NULL);19582				if (pNewUnit)19583					pNewUnit->finishMoves();19584#if defined(MOD_GLOBAL_GRATEFUL_SETTLERS)19585			}19586#endif19587		}19588	}19589}

Calls

Called by