Barbarians

oyzar

Have quit civ/forums
Joined
Oct 7, 2006
Messages
6,923
Location
Norway
Here is some information on barbarians. Noone have seen fit to make it into an article yet which is a shame, as such I figured I might as well paste in the relevant info here(things in italics i've added):

A lot is gotten from http://forums.civfanatics.com/showthread.php?t=299483 this thread.
Well, here is what I found digging through the 3.17 SDK (finally with a new Debug dll, yeah!):
Barbs enter cultural borders if an appropriate mission is chosen in CvUnitAI::AI_barbAttackMove(). This function is only called for barbarian units with UNITAI_ATTACK (barb animals always have UNITAI_ANIMAL and they cannot enter owned plots anyway). So this represents a direct effect of the difficulty level as non-animal barbs (Warriors and Archers) spawn earlier plus in greater numbers on the higher levels. The indirect effect is indeed given by the conditional behavior of the barb units depending on the total number of non-barb cities on the whole map (not continents! -- the continents(=areas) only determine their number of needed barbs according to their unowned tiles).

There are 2 thresholds:

A) NumCities > 2*NumPlayersAlive:
  • Barbs will enter your territory if they can pillage an improvement on their next turn, so the common 1:move: barb Warriors and Archers will intrude to pillage improvements in your border tiles. I'm not certain about 2:move: mounted barb units (do barb Chariots spawn regularly?). I know about a random event which places barb Horse Archers on the map, but they demonstrate "special" behavior (they directly target the barbs' TargetCity of the area).
  • Barbs will enter your territory if they can attack a city on the same turn --> this should only be possible for the uncommon 2:move: units.
B) NumCities > 3*NumPlayersAlive:
The restrictions for the missions are lifted to
  • pillage on the 3rd turn
  • attack city on the second turn
  • If the barbs have calculated AREAAI_OFFENSIVE for their AreaAI (huge numbers of ATTACK barb units, most likely after random event; normally it's AREAAI_MASSING) they will straight target their chosen city without any such restrictions.
With GAMEOPTION_RAGING_BARBARIANS it gets even worse (NumCities is irrelevant then):
  • pillage on the 4th turn
  • attack city on the 3rd turn
  • target city when AREAAI_OFFENSIVE
Important things to keep in mind:
  • Barbs will ALWAYS enter your borders if they can attack one of your units directly (--> watch your Workers!).
  • Once they have entered your territory, they will consider the following options for their current turn
  1. pillage an improvement (50%)
  2. attack somebody
  3. depending on conditions A and B move to another tile to pillage or attack a city (or target a city)
  4. patrol --> here they always prefer to move into unowned tiles (this is why they don't enter in the first place; if possible they will move out again; if they somehow get boxed in by cultural borders they might also "have to" enter your territory with this mission)
  • Hence they can also change their minds - if they enter to pillage on their 3rd turn, they will choose to attack somebody on their second turn if possible (if you move a defending unit to an adjacent tile).
  • There are certainly special situations where barb behavior might contradict the above rules (the code for unit AI is pretty complex...)

Barb units do not differ from other AI units in general, they also have a certain courage to attack. They just behave more erraticly and have lower inhibition thresholds on average.
The decision to attack a unit or not is made via a comparison of the unit's AttackOdds vs. a threshold individually specificied for all mission test calls (AI_anyAttack, AI_cityAttack). There are a lot of calculations adjusting the odds and thresholds depending on the situation of the combatants. What I read from all this is, that the AttackOdds are similar to the well known combat odds (depending on the strength, damage and bonuses of attacker and defender) + a "dare-devil offset", the AttackOddsChange. Each AI leader recalculates his/her AttackOddsChange each turn during CvPlayerAI::AI_doMilitary() according to:
[pre]AttackOddsChange = BaseAttackOddsChange + RNG(AttackOddsChangeRand) + RNG(AttackOddsChangeRand)[/pre]IIUC this value should vary around an average of BaseAttackOddsChange + AttackOddsChangeRand-1. As you can read from the Leaderhead-xml (or my converted spreadsheet :p) the barbs have BaseAttackOddsChange = 0 (it is 0 for Gandhi ... 6 for Ragnar), which would make them cowards, but their AttackOddsChangeRand = 16, that is double the value of all other leaders (8), so on average they should attack more often than Ragnar (0 + 15 > 6 + 7).
The great variation of the barbs' AttackOddsChange (0...30) is responsible for their erratic behavior -- they will often shy from combat on one turn and move pass your defenders only to attack them on the very next turn. As mentioned above the thresholds used in CvUnitAI::AI_barbAttackMove() are especially low (20, 15, 10), so that the barbs might attack regardless of the actual combat odds during certain turns, when a very high AttackOddsChange might bring them over the relevant threshold alone.

Oh yeah, and they of course have a very advanced radar to detect all your units, cities and improvements. SearchRange = (Range+1)*(BaseMovements+1), with Range being the other parameter passed in the mission test calls besides the thresholds --> a 1:move: unit will scan a circle with a diameter of 9 tiles at the minimum (Range = 1).

Edit: It's not a circle but a sqaure area of 9 x 9 tiles.

Have you ever wondered about the sudden "wave" of barb units storming towards your capital followed by surprisingly very calm periods? That's exactly when threshold B) has been passed (NumCities > 3*NumPlayers) and the barbs have calculated AREAAI_OFFENSIVE for your area. In contrast to the misleading comment in the code of CvTeamAI::AI_calculateAreaAIType ("// XXX does this ever happen?") this is almost guaranteed to happen in every game. If the barbs haven't founded a city on your landmass yet, they only need TWO attack units for that AreaAI.
The formula for this minimum number of offensive units is:
[pre]N = ((NumBarbPop+NumBarbCities+1)*20)/100 + 2[/pre]So once they have a few cities on your landmass they more or less leave you alone again, because the number of "needed" barbs per landmass is limited by the number of unowned tiles and the difficulty level.

But during this period of harassment, not even the finest [game] tactics ;) or the broadest creative-boosted cultural borders will protect you...

Just to make this clear "Huge = 2"? (Probably a case of increasing knowledge, such a nasty remarks in comments could be confusing). It does make more sense though as I've indeed noticed that after some point barbs go straight to my cities in hordes; even if I had ring 3.

More seriously, Numbarpop= the total population in all barbarian cities?
Correct = increased my knowledge while watching the barbs storm my improvement-less 100:culture: capital in a WBed test scenario and took a closer look at the code...

Only the total barbarian population on your landmass (NumBarbCities likewise). So with 3 barb cities on your continent size 4 each, the barbs need N = ((3*4+3+1)*20)/100 + 2 = 5 roaming attackers for AREAAI_OFFENSIVE (hmmm, actually not *that* many).

Barb Galleys use UNITAI_ATTACK_SEA and thus require their own AI function CvUnitAI::AI_barbAttackSeaMove(). Obviously, you won't find any AI_attackCity or AI_targetCity calls in there, as boats are only allowed to attack units outside cities. Also, there aren't any similar NumCities-thresholds implemented here and the AreaAI of their area (= water!) has no effect. So once they are around, they will check their valid options in the following order:
  1. flip a coin (50% chance) and -if lucky- check whether an improvement can be pillaged this turn (AI_pillageRange(iRange=1))
    --> they will move onto Fishing Boats in adjacent tiles and pillage them
    .
  2. check whether an enemy unit can be attacked on this or on the next turn (AI_anyAttack(iRange=2, iOddsThreshold=25))
    --> they will target your Work Boats 4 tiles away; this can be exploited to keep them away from your improvements by some nice disco-fox dancing ;) or to lure them into the fishing grounds of your neighbors
    .
  3. check whether an improvement can be pillaged within 4 turns ((AI_pillageRange(iRange=4))
    --> they will use their advanced AI radar to spot your improvements 7 tiles away :eek:, however, this radar can only detect improvements in the fog, if they exist for more than 20 turns (improved knowledge!, 5 turns for land units and improvements on land); the closest improvement "wins" (max(pillageValue/pathTurns))
    .
  4. AI_heal()
    .
  5. AI_patrol()
    --> move into a random adjacent tile (iValue = 1...10,000); prefer unowned tiles (+20,000) and tiles not adjacent to borders (+10,000); if movement points left, recalculate ALL options (if the patrol-move reveals a "fresh" improvement, that was hidden from the AI radar in the fog before, option 3 will now be enabled and the respective mission (AI_pillageRange(iRange=4)) will be pushed)

At what time will the game spawn barb Galleys?
  • the era of barb animals must be over (NumCities >= 3/2*NumPlayers; GameTurns > Handicap.BarbarianCreationTurnsElapsed * GameSpeed.BarbPercent)
  • the barbs must know Sailing

How do barbs acquire their techs?
  • Their real research rate is almost guaranteed to be 0, since they aren't allowed to have a Palace (8 commerce) and thus suffer from high maintenance (but they don't lose units via STRIKE).
  • So they merely receive the 1 free beaker everybody gets (BASE_RESEARCH_RATE) for their current research plus 3% (BARBARIAN_FREE_TECH_PERCENT) of the cost of a tech scaled by the percentage of players who know it for each technology each turn. Hence, if the majority of the players in the game knows Sailing, it won't be long until the first barb Galleys come visit your fishing grounds.

On barb visbility:
They generally see all units and cities within the current search radius for a specific mission -- this radius is a function of their base movements and the passed iRange argument. Improvements are a bit special, they need to be revealed already or be old enough (see above), otherwise they get a pillage value of 0 and thus won't be pillaged. The barbs always know exactly where their target city is located (mostly your capital or coastal city with a wonder).

Barb Animals start appearing after game turn 5 (hard coded, independent of game speed). As mentioned earlier the game stops spawning them if both of the following two conditions are met:
  1. The average number of (non-barb) cities per player must be >= 1.5.
    .
  2. A certain number of game turns must have elapsed (depending on difficulty [iBarbarianCreationTurnsElapsed] and game speed [iBarbPercent], marathon is special with 400%).

So in a quick deity game animals are usually only created in just 1 IBT (5->6).

When the era of barb animals is over, the existing creatures are not eradicated in 1 major extinction event. Instead, the game kills off 1 animal per turn until they are all gone and will insert regular barb units (Warriors, Archers, ...) as they are "needed" during the next IBT (can be > 1 unit/turn!, ALL required units are created immediately).

Barb animals don't enter resources unless to attack an unit already there.

Roads/railroads aren't Improvements technically but Routes instead, they do have an AI_pillageValue > 0 which is required to trigger the AI_pillageRange mission.

So barbs will enter borders to pillage if there are roads nearby.

A quick tip on fogbusting:

I'm not sure how many of you fellow civfanatics know about the GlobalDefines value MIN_BARBARIAN_STARTING_DISTANCE = 2 and already exploit it. It means that new barbarian units are not allowed to spawn within a 2 tile radius of ANY existing unit (including other barb units) on the map. This rule allows very efficient "fogbusting" on smaller landmasses. Here is an example (spoiler for current LHC Huayna Capac checkpoint 1):
Spoiler :

2 Quechuas and 1 Scout are all I need to keep my lands barb-free :D. It is not required to have active vision for all the tiles (the fog doesn't matter). I've changed the Dotmap-function of BUG to display the no-spawn areas for my three units.

Note that barb cities CAN appear 2 tiles away from my units if the tile is in the fog though.

So basically the range around a unit is just a BFC but with the corners as well? Does this count for barb galleys as well?

Yeah, maybe we call it BFS = big fat square or barb f....forbidding square :crazyeye:. The rule applies to the infamous barb galleys as well --> it's nice to barb-bust in barb galley breeding areas with those 1-tile offshore islands using work boats which can be sent into the fishing grounds later. As mentioned above, the barb units (warriors/archers) stationed in spawned barb cities do the same job.

Can a barb city spawn where a unit couldn't?

Although MIN_BARBARIAN_CITY_STARTING_DISTANCE = MIN_BARBARIAN_STARTING_DISTANCE = 2, the rules are different. Barb cities can definitely spawn in a units BFS as long as no non-barb player has active vision for that tile. (That's also why the AI sends there GUARD_CITY units out to the chosen city sites.) It has happened to me before.

Even though barbs can't appear at the very beging of the game can you still pop the from huts?

It's not possible if you haven't founded your capital yet or if the hut is at a PlotDistance < 8 tiles away from your capital. So if you move your settler and your warrior diagonally away in opposite directions and found your capital first on turn 1 to achieve a great enough distance, you can then pop barbs from a hut that early. (Try it with random seed in a deity WB-scenario.)

Even though the entire continent is within 2 squares of a unit, a city can still spawn in a single tile of fog?

Yes, I recommend actual fogbusting in the juicy areas with high founding values (lot's of resources, often coastal) where you plan to get a city up via a settler. It's interesting to note that the likelihood for an early barb city spawning on your LHC island is rather low -- the game prefers to spawn them on the continents with the highest number of owned tiles, even if these places are much crappier than the sites on your continent.


Any unit can spawnbust; Workers, Settlers, Missionaries, Spies, Great People ... ;)

Spawn: The spawning of new barbarian units will stop after the game has reached the Renaissance era (bNoBarbUnits = 1 in CIV4EraInfos.xml). The game's era is calculated by averaging the current eras of the individual players (including the usual rounding down to the lower integer -- if everybody has advanced to the Renaissance but only Toku is still power-teching through the Medieval era, the barbs will still spawn).

Attack: As described above, the behaviour of the individual units is a function of their UnitAI (which AFAIK is imprinted on them at the moment they are created), the barb's AreaAI (which gets recalculated for each area each turn) and the current situation in the game (average number of cities of non-barb players and game options). E.g. all of you will have noticed that the Warriors/Archers of newly spawned barb cities will not start marching towards the barbs' target city (very often the capital of the human player). This is because they are created with a UnitAI CITY_DEFENSE when the city pops up. New units built by these cities however can have different UnitAIs and will therefore most likely show a behaviour of active aggression. BTW, the UnitAI of any unit and the AreaAI can be queried in debug-mode (press ALT while mousing over a tile with barb culture for AreaAI).

The city defenders will test the missions AI_leaveAttack() and AI_chokeDefend(); if one of those missions gets pushed they will leave the city to attack weak enough units in the vicinity of the city. AI_leaveAttack() exclusively checks the tiles adjacent to the city, but AI_chokeDefend() calls AI_anyAttack() during which movement on roads/railroads is also considered --> the defenders will even leave their culture to attack a unit 2 tiles away if they can reach it via road. However, it appears these checks are only performed in the case that AI_getPlotDanger() returns true for the city plot --> since non-military units like Workers and Settlers don't "cause danger" it might be wiser to let these units scurry past the barb cities unescorted :eek:, than to escort them with weak but danger-evoking units like Warriors :lol: but I don't give any guarantees here ...

Each team calculates an AreaAIType for each landmass according to the number and UnitAI of its units and the number and size of its cities located on that landmass -- so do the barbs (usually team 18). Thus, they cannot remove their AreaAIs but they continuously update them.

In general the possible types are: AREAAI_OFFENSIVE, AREAAI_DEFENSIVE, AREAAI_MASSING, AREAAI_ASSAULT, AREAAI_ASSAULT_MASSING, AREAAI_ASSAULT_ASSIST, AREAAI_NEUTRAL.

For the barbs the typical sequence over the course of the early game is:
  • First non-animal barb unit spawns on the landmass, no cities yet --> AREAAI_MASSING
  • More barb units spawn, no cities yet --> AREAAI_OFFENSIVE
  • Barb cities with city defenders pop up and the majority of the roaming attack units get killed off --> AREAAI_MASSING
    ---------
  • Barb cities on uninhabited islands (no civ-players) --> AREAAI_ASSAULT

Oops, seems I was wrong about AREAAI_ASSAULT_MASSING -- that AreaAIType is not used by the barbs.
I assumed the mechanics here were similar to the ones responsible for the progression from AREAAI_MASSING --> AREAAI_OFFENSIVE depending on the number and UnitAIs of the available units. But the units don't have an effect.

Regular AIs will calculate AREAAI_ASSAULT_MASSING in situations, where they are preparing for an intercontinental invasion (WARPLAN_PREPARING_TOTAL or WARPLAN_PREPARING_LIMITED). After the fixed turns of preparations are over they will switch to WARPLAN_TOTAL / WARPLAN_LIMITED and the corresponding AreaAIType AREAAI_ASSAULT. (There is no switching out of WHEOOHRN ala ALC#24-Saladin if the target is on another landmass.)

So the barbs just use AREAAI_ASSAULT if they are alone on an island.
The AreaAITypes have an effect on what the AIs choose to produce in their cities, and how they value and move their units (very complex). AREAAI_ASSAULT will let the barbs launch intercontinental raids Viking-style IF they can reach other civs' cities via Galleys. They are not allowed to build better transport ships (but they can build Privateers, Frigates and Paratroopers :eek:).

Ok, try to klarify barb research:

The barb team (in other words we) gain

floor((c * floor ( ( 3*nw ) / na))/ 100)

c = cost of tech
nw = number of teams with tech
na = number teams alive (not counting us)

beakers per turn on every tech.

That means when na=7
1% if 3 teams have the tech
2% if 5 teams have the tech
3% if 7 teams have the tech

all percentages are rounded down. So e.g. 1 beaker/turn for fishing in the beginning (3 civs know it = 1%; cost around 120)

It hasn't anything to do with what we or the barbs research.
As monarch AI start all with archery we get 3% of the cost of archery every turn and will learn it after 35 turns (because of rounding down). We will learn it even w/o having the prerequisite hunting and by that unable to research it.

BTW killing one AI would be nice, as already then it would be only 2 AIs with the tech necessary to get beakers.

The longer we don't research techs known to the AIs the more beakers we get for free. As the starting techs get researched pretty quickly, we should restrict ourselves to only research those we absolutely need.

The second benefit of waiting is that when we finally research a tech (we will not wait for all to come for free) we get a bonus on our research if more civs know it already.

You can see the beakers the team gains by just going into the tech adviser and hover over the techs. Even after one turn you will see that we have gained beakers in fishing, mysticism and archery, even if we research something else.
BTW, that's the team beakers (also in normal games for PA or MP teams) what you see here. When the barbs start to research, their measly beakers will also get added there immediately.
 
Reserved just in case I find more to put in (and also if I or someone else(dan) decides to reorganize this into something a bit more structured).
 
Would you want to add information about free wins against barbs? At Prince and lower difficulties, you get at least 1 "barb free win". At Prince it's 1, and +1 for every difficulty level you go below that.

Your free wins counter will decrease every time you win a battle vs. a barbarian (including animals) until it reaches 0, at which point you have no barb free wins left and barb combat will be as usual.

When you still have barb free wins left, you are not quite guaranteed to win any battles with barbs but very nearly! For every combat round, the barb's odds will be reduced to 10% if not already lower than 10% (consequently your odds in each combat round cannot be lower than 90%). This means that even if the barbarian is very strong compared to your unit, it will be very unlikely to kill your unit, especially if your unit is uninjured.

Note that when you have barb free wins left, the combat odds shown to you by the game are false. Typically you will have more than 95% odds against any barbarian in any situation when you still have barb free wins left.
 
This doesn't include combat against barbs at all, but adding it is a good idea. Not many know about the free +25% bonus you get when defending a city against barbs on all levels for example. There is also an inherit bonus against barbs on a lot of levels and another bonus against animals on almost all levels. I'll have to check how much bonuses you get at different levels as I don't have it of the top of my head.
 
Are there other rules when you select 'raging barbarians', because ones i played on deity with raging barbarians and they came in about turn 15, when i had just 2 warriors and they could go to my city easelly, 50 turns later my city culture was 100+ and i still ahd none improvements and the barb archers and warriors could cross trough the culture border of my city
 
It is mentioned above that:

With GAMEOPTION_RAGING_BARBARIANS it gets even worse (NumCities is irrelevant then):
pillage on the 4th turn
attack city on the 3rd turn
target city when AREAAI_OFFENSIVE

They will always enter borders if they can pillage within 4 turns or attack cities within 3 turns, that is pretty much always the case.
 
Top Bottom