1. We have added a Gift Upgrades feature that allows you to gift an account upgrade to another member, just in time for the holiday season. You can see the gift option when going to the Account Upgrades screen, or on any user profile screen.
    Dismiss Notice

Barb Hate and You

Discussion in 'Civ4 - Strategy & Tips' started by MarigoldRan, May 1, 2013.

  1. MarigoldRan

    MarigoldRan WARLORD

    Joined:
    Mar 12, 2011
    Messages:
    2,328
    How do they pick a city as a "target city?"

    Should you ever expose one of your cities temporarily to protect an improvement?
     
  2. Manco Capac

    Manco Capac Friday,13 June,I Collapse

    Joined:
    Mar 1, 2010
    Messages:
    8,051
    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.
     
  3. Manco Capac

    Manco Capac Friday,13 June,I Collapse

    Joined:
    Mar 1, 2010
    Messages:
    8,051
    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.
     
  4. Manco Capac

    Manco Capac Friday,13 June,I Collapse

    Joined:
    Mar 1, 2010
    Messages:
    8,051
    From another thread where I define barb behaviours in mortal language.

     
  5. Manco Capac

    Manco Capac Friday,13 June,I Collapse

    Joined:
    Mar 1, 2010
    Messages:
    8,051
    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;
    }
    
     
  6. Ghpstage

    Ghpstage Deity

    Joined:
    Jan 15, 2009
    Messages:
    2,944
    Location:
    Bristol, England
    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.
    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.
    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...
    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
    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?
     
  7. Tall German Joe

    Tall German Joe Prince

    Joined:
    Dec 6, 2005
    Messages:
    514
    the bolded part just means add 1 to the value. (equivalent to the line value +=1)
     
  8. Handel

    Handel Prince

    Joined:
    Nov 29, 2005
    Messages:
    514
    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!!!"
     
  9. Mec AntiKythera

    Mec AntiKythera King

    Joined:
    Sep 24, 2010
    Messages:
    980
    Wow, the new world of barb intentions. I never knew it existed.
     
  10. MarigoldRan

    MarigoldRan WARLORD

    Joined:
    Mar 12, 2011
    Messages:
    2,328
    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.
     
  11. VoiceOfUnreason

    VoiceOfUnreason Deity

    Joined:
    Dec 5, 2005
    Messages:
    3,663
    Bombarded defenses. 100 point scale; with rounding going down, this means that the defense effect on iValue is in the range [0..4].
     
  12. Ghpstage

    Ghpstage Deity

    Joined:
    Jan 15, 2009
    Messages:
    2,944
    Location:
    Bristol, England
    Thank you both. So in practice the defense part usually won't mean much as barbs don't get seige.
    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.
    Actually the code Tachy posted also governs AI civ city targetting behaviour.
     
  13. Manco Capac

    Manco Capac Friday,13 June,I Collapse

    Joined:
    Mar 1, 2010
    Messages:
    8,051
    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.


    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).
     
  14. MarigoldRan

    MarigoldRan WARLORD

    Joined:
    Mar 12, 2011
    Messages:
    2,328
    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.
     
  15. Manco Capac

    Manco Capac Friday,13 June,I Collapse

    Joined:
    Mar 1, 2010
    Messages:
    8,051
    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.
     
  16. MarigoldRan

    MarigoldRan WARLORD

    Joined:
    Mar 12, 2011
    Messages:
    2,328
    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.
     
  17. Manco Capac

    Manco Capac Friday,13 June,I Collapse

    Joined:
    Mar 1, 2010
    Messages:
    8,051
    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.
     
  18. Manco Capac

    Manco Capac Friday,13 June,I Collapse

    Joined:
    Mar 1, 2010
    Messages:
    8,051
    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.
     
  19. MarigoldRan

    MarigoldRan WARLORD

    Joined:
    Mar 12, 2011
    Messages:
    2,328
    Ah well. Play some games?
     
  20. Manco Capac

    Manco Capac Friday,13 June,I Collapse

    Joined:
    Mar 1, 2010
    Messages:
    8,051
    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.
     

Share This Page