Barb Hate and You

How do they pick a city as a "target city?"

Should you ever expose one of your cities temporarily to protect an improvement?
 
Nice job Marigold,
you're going in the good direction this time.

Through SGOTM games, it forced me to go through barb SDK many many times. I admit I have never resolved the AREAAI_OFFENSIVE case in terms of which target the barb team would take to pick on. I have noticed my early rushes tended to ease barb defense because seems barb team (I constantly use barb team as a whole because AREAAI_OFFENSIVE is a general order applied to all units under barb flag; it's not about individual barb units behaviours) target weak nations first.

One detail that may be of interest is AREAAI_OFFENSIVE is not limited to barb team, but is used for all AI players; indeed, many functions in the SDK are cross paths.
So, in the same line of reasoning, I recall there was a function for evaluating the city values as a target. I'm leaning towards this function explaining the barbs swarming towards a particular city.
Also, important to keep in mind: AI never target other than cities. If it targets a unit, it is just a opportunistic fight. In the long range, only cities are target.
 
This thread is where most of the forums knowledge of AI behaviours originate, you may find some answers here though I wish the way the game chooses 'areas' was revealed.

Awww, why my special thread about game mechanics never get through search results. :sad:
Anyways, the game doesn't choose areas; areas are defined since T0 of the game. Areas are defined as a landmass surrounded by waters. Even a one tile island is a landmass.

AREAAI_OFFENSIVE is just a word (or macro). IIRC, that was simply like a keyword connecting different functions. It has nothing special attached to it. It's not a function. For instance, this team gathers all pre-requisite conditions for an AREAAI_OFFENSIVE, then the memory keeps AREAAI_OFFENSIVE to define them. Later, several other functions check if this team has this keyword and if so, it enables a serie of particular behaviours.
 
From another thread where I define barb behaviours in mortal language.

I have seen the same behaviour too. When I'm into rushing mode and have a lot of strong units, barbs seem to sniff other weaker nations than me. Even if my cities are badly guarded. I haven't tried to explain this issue yet. Maybe I'll look at it later.

It's not really immunity. Early, barbs are in patrolling mode, which equates like being hoboes roaming around without aim. And the patrol function favors any tiles not owned. And adjacent tiles to owned tiles are discouraged. But if the barb ends up near a bonus or a city (which sometimes happen when the barb was sniffing a resource next to the city before), it goes after it. Why approaching so near in patrol mode; well, it is almost 100% caused by a stupid AI scout playing with UNITAI_ATTACK script of the barbs unit that basically say Kill'em all. After the bloodlust satisfied, they stumbly by that bonus under the barb pillage radar.

When the global city count reach a critical number (which is often 22 cities for 6 AI players+human player), it triggers a global order (or a mission onto the units) that force them to override their patrolling nature and then they rush after civilized nations.

When barb cities start to spawn, another hidden force is looming and when the #barb cities + their city pop reach a critical mass, the AREAAI_OFFENSIVE global order is dissolved. And barbs return to their original hobo-esque nature. That's what people call the pacification of barbs at some point of the game.
 
Here are two functions that may be of interest:

Spoiler :
Code:
int CvPlayerAI::AI_targetCityValue(CvCity* pCity, bool bRandomize, bool bIgnoreAttackers) const
{
	PROFILE_FUNC();

	CvCity* pNearestCity;
	CvPlot* pLoopPlot;
	int iValue;
	int iI;

	FAssertMsg(pCity != NULL, "City is not assigned a valid value");

	iValue = 1;

	iValue += ((pCity->getPopulation() * (50 + pCity->calculateCulturePercent(getID()))) / 100);

	if (pCity->getDefenseDamage() > 0)
	{
		iValue += ((pCity->getDefenseDamage() / 30) + 1);
	}

	if (pCity->isCoastal(GC.getMIN_WATER_SIZE_FOR_OCEAN()))
	{
		iValue++;
	}

	if (pCity->hasActiveWorldWonder())
	{
		iValue += 1 + pCity->getNumWorldWonders();
	}

	if (pCity->isHolyCity())
	{
		iValue += 2;
	}

	if (pCity->isEverOwned(getID()))
	{
		iValue += 3;
	}
	if (!bIgnoreAttackers)
	{
	iValue += AI_adjacentPotentialAttackers(pCity->plot());
	}

	for (iI = 0; iI < NUM_CITY_PLOTS; iI++)
	{
		pLoopPlot = plotCity(pCity->getX_INLINE(), pCity->getY_INLINE(), iI);

		if (pLoopPlot != NULL)
		{
			if (pLoopPlot->getBonusType(getTeam()) != NO_BONUS)
			{
				iValue++;
			}

			if (pLoopPlot->getOwnerINLINE() == getID())
			{
				iValue++;
			}

			if (pLoopPlot->isAdjacentPlayer(getID(), true))
			{
				iValue++;
			}
		}
	}

	pNearestCity = GC.getMapINLINE().findCity(pCity->getX_INLINE(), pCity->getY_INLINE(), getID());

	if (pNearestCity != NULL)
	{
		iValue += std::max(1, ((GC.getMapINLINE().maxStepDistance() * 2) - GC.getMapINLINE().calculatePathDistance(pNearestCity->plot(), pCity->plot())));
	}

	if (bRandomize)
	{
		iValue += GC.getGameINLINE().getSorenRandNum(((pCity->getPopulation() / 2) + 1), "AI Target City Value");
	}

	return iValue;
}


CvCity* CvPlayerAI::AI_findTargetCity(CvArea* pArea) const
{
	CvCity* pLoopCity;
	CvCity* pBestCity;
	int iValue;
	int iBestValue;
	int iLoop;
	int iI;

	iBestValue = 0;
	pBestCity = NULL;

	for (iI = 0; iI < MAX_CIV_PLAYERS; iI++)
	{
		if (GET_PLAYER((PlayerTypes)iI).isAlive())
		{
			if (isPotentialEnemy(getTeam(), GET_PLAYER((PlayerTypes)iI).getTeam()))
			{
				for (pLoopCity = GET_PLAYER((PlayerTypes)iI).firstCity(&iLoop); pLoopCity != NULL; pLoopCity = GET_PLAYER((PlayerTypes)iI).nextCity(&iLoop))
				{
					if (pLoopCity->area() == pArea)
					{
						iValue = AI_targetCityValue(pLoopCity, true);

						if (iValue > iBestValue)
						{
							iBestValue = iValue;
							pBestCity = pLoopCity;
						}
					}
				}
			}
		}
	}

	return pBestCity;
}
 
Awww, why my special thread about game mechanics never get through search results. :sad:
Which thread are you speaking of? I have checked in the contents post of this thread but it didn't get a mention so I assumed it wasn't there.
Anyways, the game doesn't choose areas; areas are defined since T0 of the game. Areas are defined as a landmass surrounded by waters. Even a one tile island is a landmass.

AREAAI_OFFENSIVE is just a word (or macro). IIRC, that was simply like a keyword connecting different functions. It has nothing special attached to it. It's not a function. For instance, this team gathers all pre-requisite conditions for an AREAAI_OFFENSIVE, then the memory keeps AREAAI_OFFENSIVE to define them. Later, several other functions check if this team has this keyword and if so, it enables a serie of particular behaviours.
I see, I was keeping an open mind to the term AREAAI_OFFENSIVE thinking it could have referred to anything, including target cities, but couldn't find a definition. Thanks for the clarification.
Here are two functions that may be of interest:
From the first half of the first function I can get that pop, wonders, being a holy city and number of adjacent attackers (barbs only?) are factors, but this...
(pCity->getDefenseDamage() > 0)
{
iValue += ((pCity->getDefenseDamage() / 30) + 1);
Not got a useful clue as to what this relates to, wounded defenders or bombarded defenses perhaps? Neither of which would seem as though they would be seriously influential in practice....

Also while a lot of these are given numerical weightings, being coastal (and several things further down) has this
if (pCity->isCoastal(GC.getMIN_WATER_SIZE_FOR_OCEAN()))
{
iValue++;
What does the bolded part mean?

The rest I haven't looked into understanding yet, but looking at what I mentioned above it would seem AI cities would get a pretty high iValue due to high level AI growth rates, and their wonderspam and religion founding tendencies. Does the rest give an indication as to why players get atatcked so much, or is it likely hidden elsewhere?
 
I don't remember when the religious sightseeing was canceled (was it in BTS or a patch somewhere on the road), but when it existed and I established an early religion it was quite obvious. I had to have not just 2 axeman per city, but a lot of axeman to defend the improvements from pillaging. At the same time the barbs were *hugging* the AI cities - not only they didn't attack or pillage the improvements, they even didn't enter the AI culture borders (they were behaving like the animals).
Now - what I know is what I see.
BTW - when I chop the GW the barbs still come first to my cities and then they are forced to hug them - sometimes I see 7-8 barbs or even more at the same time peeping through the wall with incredulous eyes "What!!! We traveled the whole world and now we cannot rape this plump city!!!"
 
LOL. It isn't all that important on smaller maps with fog-busting. But on huge maps, barbs become a huge issue since you can't fog-bust anymore.

So what's the lesson here? Should I keep a unit in my cities for defense at all times? I'm going to post another one with unrestricted leaders. If you're going to play Marathon/Huge, you may as well select your starts.
 
Not got a useful clue as to what this relates to, wounded defenders or bombarded defenses perhaps? Neither of which would seem as though they would be seriously influential in practice....

Bombarded defenses. 100 point scale; with rounding going down, this means that the defense effect on iValue is in the range [0..4].
 
the bolded part just means add 1 to the value. (equivalent to the line value +=1)
Bombarded defenses. 100 point scale; with rounding going down, this means that the defense effect on iValue is in the range [0..4].
Thank you both. So in practice the defense part usually won't mean much as barbs don't get seige.
LOL. It isn't all that important on smaller maps with fog-busting. But on huge maps, barbs become a huge issue since you can't fog-bust anymore.

So what's the lesson here? Should I keep a unit in my cities for defense at all times? I'm going to post another one with unrestricted leaders. If you're going to play Marathon/Huge, you may as well select your starts.
There doesn't seem to be any mention of defending units or power in the code posted by Tachy, I suppose you could set up a WB test to find out if it does have an impact though.
Mec AntiKythera said:
Wow, the new world of barb intentions. I never knew it existed.
Actually the code Tachy posted also governs AI civ city targetting behaviour.
 
About the thread of mine I talked about: It is called My personal thread: Clarification of Game Mechanics.
I clarified with the help of the source code some game mechanics.


Actually the code Tachy posted also governs AI civ city targetting behaviour.

I admit I was loosy when I gave those code sections. I'll take a small moment to show the facts about barbs with more clarity:

First, where is defined AI units is in CvUnitAI.cpp. There exists a function that defines barb units that bear the script UNITAI_ATTACK (which means all except those produced from barb cities).

The complete conditional structure that works in a sequential manner...that is the succession of "ifs" are verified and those that are not fulfilled are ignored from top to bottom of the function. In other words, actions priorities.

Spoiler :
Code:
void CvUnitAI::AI_barbAttackMove()
{
	PROFILE_FUNC();

	if (AI_guardCity(false, true, 1))
	{
		return;
	}

	if (plot()->isGoody())
	{
		if (plot()->plotCount(PUF_isUnitAIType, UNITAI_ATTACK, -1, getOwnerINLINE()) == 1)
		{
			getGroup()->pushMission(MISSION_SKIP);
			return;
		}
	}

	if (GC.getGameINLINE().getSorenRandNum(2, "AI Barb") == 0)
	{
		if (AI_pillageRange(1))
		{
			return;
		}
	}

	if (AI_anyAttack(1, 20))
	{
		return;
	}	

	if (GC.getGameINLINE().isOption(GAMEOPTION_RAGING_BARBARIANS))
	{
		if (AI_pillageRange(4))
		{
			return;
		}

		if (AI_cityAttack(3, 10))
		{
			return;
		}

		if (area()->getAreaAIType(getTeam()) == AREAAI_OFFENSIVE)
		{
			if (AI_targetCity())
			{
				return;
			}
		}
	}
	else if (GC.getGameINLINE().getNumCivCities() > (GC.getGameINLINE().countCivPlayersAlive() * 3))
	{
		if (AI_cityAttack(1, 15))
		{
			return;
		}

		if (AI_pillageRange(3))
		{
			return;
		}

		if (AI_cityAttack(2, 10))
		{
			return;
		}

		if (area()->getAreaAIType(getTeam()) == AREAAI_OFFENSIVE)
		{
			if (AI_targetCity())
			{
				return;
			}
		}
	}
	else if (GC.getGameINLINE().getNumCivCities() > (GC.getGameINLINE().countCivPlayersAlive() * 2))
	{
		if (AI_pillageRange(2))
		{
			return;
		}

		if (AI_cityAttack(1, 10))
		{
			return;
		}
	}
	
	if (AI_load(UNITAI_ASSAULT_SEA, MISSIONAI_LOAD_ASSAULT, NO_UNITAI, -1, -1, -1, -1, MOVE_SAFE_TERRITORY, 1))
	{
		return;
	}

	if (AI_heal())
	{
		return;
	}

	if (AI_guardCity(false, true, 2))
	{
		return;
	}

	if (AI_patrol())
	{
		return;
	}

	if (AI_retreatToCity())
	{
		return;
	}

	if (AI_safety())
	{
		return;
	}

	getGroup()->pushMission(MISSION_SKIP);
	return;
}

Unless some asks me details on several other sub-functions, I focus on this one particularly:

Spoiler :
Code:
	else if (GC.getGameINLINE().getNumCivCities() > (GC.getGameINLINE().countCivPlayersAlive() * 3))
	{
		if (AI_cityAttack(1, 15))
		{
			return;
		}

		if (AI_pillageRange(3))
		{
			return;
		}

		if (AI_cityAttack(2, 10))
		{
			return;
		}

		if (area()->getAreaAIType(getTeam()) == AREAAI_OFFENSIVE)
		{
			if (AI_targetCity())
			{
				return;
			}
		}
	}

It says for non raging barbs option (default) if the number of players' cities (excuding barb cities and including the human player) is at least equal to 3 times that number of players (AI and human one; if team playing is allowed, swaps the concept of players to team) plus 1, then the program looks into the memory of AreaAIType() to see if AREAAI_OFFENSIVE is saved, then the barb unit behavior returned is AI_targetCity(), which crosses the path of what I showed earlier. Wrong! I see my mistake. You can read the rest and for corrective part, see next post.

You see, AREAAI_OFFENSIVE is nothing more than a keyword without programming attached to it like a function. It stands for: for this landmass (area), let's go for an offensive, be disorganized (barb team) or organized (AI civilizations).
 
Uh. So what explains the barb behavior?

I can assure you that they do target specific cities. I've posted pictures of a dozen barb units all pointing and moving in the same direction. Sometimes yours, and sometimes the AI's. They don't just move randomly around the map.
 
Uh. So what explains the barb behavior?

I can assure you that they do target specific cities. I've posted pictures of a dozen barb units all pointing and moving in the same direction. Sometimes yours, and sometimes the AI's. They don't just move randomly around the map.

I am working on it. You see, clarification of things is not an instantaneous process. I have some hunches I think. Also, will use debugger to actually see the barb units trajectories...

And yes, the barb units do move randomly...until a certain threshold is hit.
 
I am working on it. You see, clarification of things is not an instantaneous process. I have some hunches I think. Also, will use debugger to actually see the barb units trajectories...

And yes, the barb units do move randomly...until a certain threshold is hit.

I would agree with that. Let me reprhase it:

Barbs stop moving randomly at some point in the game. Then they come after you (90% of the time).

Anyways, thank you for the code-digging! In the meantime, if you haven't done so, check out the explosion in Damascus, Syria. Something blew up. It was big.
 
About barb behaviors shifting towards a semi-organized "zerg" in group, I think I do not need to lay more explanations. This is solved ages ago to me and I did reported an earlier post of mine making a résumé of what is actually is going on. In a superb nutshell, Barb invasions come when global city count reaches a critical mass and is stopped when either all lands are claimed or barb city count reaches a critical mass versus the number of spawned barb units at this moment(which one is impossible to check without cheating in a regular game).

My target this time is: how barb team chooses one target over another one and how the power ratio counts in. I haven't found any reference to it yet.
 
Hmmm, distasteful defeat again.
Well, I haven't found the exact reason why a high power rating affect barb inclination towards certain over other nations. Sadly. This wasn't the first I attacked this particular behavior. And it still resists.
 
Hmmm, distasteful defeat again.
Well, I haven't found the exact reason why a high power rating affect barb inclination towards certain over other nations. Sadly. This wasn't the first I attacked this particular behavior. And it still resists.

Ah well. Play some games?
 
I know exactly what is the behaviour. I have felt it many times in the past. I want to find an explanation! *pouting*.
Anyways, it gotta be late. Gnite.

Sometimes, I get luckier another day. It occasionally happens.
 
Top Bottom