Bazaar developer reference

CvUnit::pillage()

Defined in
CvUnit.cpp, line 7808
Class
CvUnit
Hook name
CivHooks::CvUnit_pillage
Lua events
GameEvents.UnitPillageGold

Hooks

static bool BeforeCvUnit_pillage(CvUnit* self, bool& result);static void AfterCvUnit_pillage(CvUnit* self, bool& result);static bool ReplaceCvUnit_pillage(bool (*original)(CvUnit*), CvUnit* self);CivRegisterBefore<CivHooks::CvUnit_pillage>(host, &BeforeCvUnit_pillage);CivRegisterAfter<CivHooks::CvUnit_pillage>(host, &AfterCvUnit_pillage);CivRegisterReplace<CivHooks::CvUnit_pillage>(host, &ReplaceCvUnit_pillage);

Code

7808bool CvUnit::pillage()7809{7810	VALIDATE_OBJECT7811	CvString strBuffer;78127813	ImprovementTypes eTempImprovement = NO_IMPROVEMENT;78147815	CvPlot* pPlot = plot();78167817	if(!canPillage(pPlot))7818	{7819		return false;7820	}78217822	if(pPlot->isOwned())7823	{7824		// we should not be calling this without declaring war first, so do not declare war here7825		if(!isEnemy(pPlot->getTeam(), pPlot))7826		{7827			if((pPlot->getImprovementType() == NO_IMPROVEMENT && !pPlot->isRoute()) || (pPlot->getOwner() != getOwner()))7828			{7829				return false;7830			}7831		}7832	}78337834	bool bImprovement = false;78357836	// Has an Improvement7837	if(pPlot->getImprovementType() != NO_IMPROVEMENT)7838	{7839		if(!pPlot->IsImprovementPillaged())7840		{7841			bImprovement = true;7842		}7843	}78447845	// Is an Improvement here that hasn't already been pillaged?7846	bool bSuccessfulNonRoadPillage = false;7847	if(bImprovement)7848	{7849		eTempImprovement = pPlot->getImprovementType();7850		CvImprovementEntry* pkImprovement = GC.getImprovementInfo(eTempImprovement);7851		if(pkImprovement)7852		{7853			if(pPlot->getTeam() != getTeam())7854			{7855				int iPillageGold = 0;78567857				// TODO: add scripting support for "doPillageGold"7858				iPillageGold = GC.getGame().getJonRandNum(pkImprovement->GetPillageGold(), "Pillage Gold 1");7859				iPillageGold += GC.getGame().getJonRandNum(pkImprovement->GetPillageGold(), "Pillage Gold 2");78607861#if defined(MOD_EVENTS_UNIT_ACTIONS)7862				if (MOD_EVENTS_UNIT_ACTIONS) {7863					int iValue = 0;7864					if (GAMEEVENTINVOKE_VALUE(iValue, GAMEEVENT_UnitPillageGold, getOwner(), GetID(), eTempImprovement, pkImprovement->GetPillageGold()) == GAMEEVENTRETURN_VALUE) {7865						iPillageGold = iValue;7866						CUSTOMLOG("Pillage gold is %i", iPillageGold);7867					}7868				}7869#endif78707871				iPillageGold += (getPillageChange() * iPillageGold) / 100;78727873#if defined(MOD_API_EXTENSIONS)7874				// Trust the modder if they set negative gold7875				if (iPillageGold != 0)7876#else7877				if (iPillageGold > 0)7878#endif7879				{7880					GET_PLAYER(getOwner()).GetTreasury()->ChangeGold(iPillageGold);78817882					if(getOwner() == GC.getGame().getActivePlayer())7883					{7884						strBuffer = GetLocalizedText("TXT_KEY_MISC_PLUNDERED_GOLD_FROM_IMP", iPillageGold, pkImprovement->GetTextKey());7885						DLLUI->AddUnitMessage(0, GetIDInfo(), getOwner(), true, GC.getEVENT_MESSAGE_TIME(), strBuffer/*, "AS2D_PILLAGE", MESSAGE_TYPE_INFO, m_pUnitInfo->GetButton(), (ColorTypes)GC.getInfoTypeForString("COLOR_GREEN"), pPlot->getX(), pPlot->getY()*/);7886						MILITARYLOG(getOwner(), strBuffer.c_str(), plot(), plot()->getOwner());7887					}78887889					if(pPlot->isOwned() && pPlot->getOwner() == GC.getGame().getActivePlayer())7890					{7891						strBuffer = GetLocalizedText("TXT_KEY_MISC_IMP_DESTROYED", pkImprovement->GetTextKey(), getNameKey(), getVisualCivAdjective(pPlot->getTeam()));7892						DLLUI->AddPlotMessage(0, pPlot->GetPlotIndex(), pPlot->getOwner(), false, GC.getEVENT_MESSAGE_TIME(), strBuffer/*, "AS2D_PILLAGED", MESSAGE_TYPE_INFO, m_pUnitInfo->GetButton(), (ColorTypes)GC.getInfoTypeForString("COLOR_RED"), pPlot->getX(), pPlot->getY(), true, true*/);7893						MILITARYLOG(pPlot->getOwner(), strBuffer.c_str(), pPlot, plot()->getOwner());7894					}7895				}7896			}78977898#if !defined(NO_ACHIEVEMENTS)7899			//Unlock any possible achievements.7900			if(getOwner() == GC.getGame().getActivePlayer() && strcmp(pkImprovement->GetType(), "IMPROVEMENT_FARM") == 0)7901				CvAchievementUnlocker::FarmImprovementPillaged();7902#endif79037904			// Improvement that's destroyed?7905			bSuccessfulNonRoadPillage = true;7906			if(pkImprovement->IsDestroyedWhenPillaged())7907			{7908#if defined(MOD_GLOBAL_ALPINE_PASSES)7909				// If this improvement auto-added a route, we also need to remove the route7910				ImprovementTypes eOldImprovement = pPlot->getImprovementType();7911				7912				// Find the build for this improvement7913				for (int i = 0; i < GC.getNumBuildInfos(); i++) {7914					CvBuildInfo* pkBuild = GC.getBuildInfo((BuildTypes)i);7915					7916					if (pkBuild && ((ImprovementTypes)pkBuild->getImprovement()) == eOldImprovement) {7917						// Found it, but did it auto-add a route?7918						if (pkBuild->getRoute() != NO_ROUTE) {7919							// Yes, so remove the route as well7920							pPlot->setRouteType(NO_ROUTE);7921						}7922						7923						// Our work here is done7924						break;7925					}7926				}7927#endif79287929				pPlot->setImprovementType(NO_IMPROVEMENT);7930			}7931			// Improvement that's pillaged?7932			else7933			{7934				pPlot->SetImprovementPillaged(true);7935			}79367937			if (pkImprovement->IsDisplacePillager())7938			{7939				jumpToNearestValidPlot();7940			}7941		}7942	}7943	else if(pPlot->isRoute())7944	{7945		pPlot->SetRoutePillaged(true);7946	}79477948	if(!hasFreePillageMove())7949	{7950		changeMoves(-GC.getMOVE_DENOMINATOR());7951	}79527953	if(bSuccessfulNonRoadPillage)7954	{7955		if (hasHealOnPillage())7956		{7957			// completely heal unit7958			changeDamage(-getDamage());7959		}7960		else7961		{7962			int iHealAmount = min(getDamage(), GC.getPILLAGE_HEAL_AMOUNT());7963			changeDamage(-iHealAmount);7964		}7965	}79667967	return true;7968}

Calls

Called by