What determines which type of unit is spawned for the barbarians?
For this we go to CvGame and there into the rather big createBarbarianUnits method. There's a lot more in the method, but that is so far well known already, like when barbs are spawning. The interesting part is this:
We start this by iterating through all the units available to the civs in this case the barbarians. You can find this info in the CIV4CivilizationInfos.xml. The barbarian civ is at the end. Here is the complete list of units available to the barbs either for spawning or building:
We look through this list for the best valid unit to spawn. First we look if the current unit is valid by doing various checks:
We start with a rather simple check. The unit needs to have a strength bigger then 0 (no worker or settler) and it shouldn't be a defensive unit (scouts, explorers, machine guns)
Next part is a little bit more interesting:
Here we just check that the unit has the right domain to spawn in that area. This pLoopArea was determined earlier in the method. Basically we prevent water units spawning on land and vice versa.
Here we have the first really interesting part. This is your standard canTrain check like it does for every other players. The important things that are checked in canTrain are:
This code is rather interesting. We check if the unit requires resources. But then we do not check for this resource itself, but rather we check if the barbs have the necessary tech to trade this resource.
Now let me pause quickly here for a second, because this is something that not everybody knows about Civ 4. You actually need certain techs to be able to trade resources. For the most parts these are the same techs that enable the improvement you need to connect the resource anyway, so I won't go through the whole list. You can find all the info here in the BonusXML, which I turned into a spreadsheet here: BonusInfo.XML
The important resources for unit training are:
Copper - tradable with Mining
Horse - tradable with Animal Husbandry
Iron - tradable with Mining
Ivory - tradable with Hunting
Oil - tradable with Combustion
Uranium - tradable with Fission (this is the only resource that needs a different tech then the one enabling the improvement)
By the way this tech requirement for trading resources is the one single reason, why you don't want to skip Agriculture, because without Agriculture you can't get those resources from other players.
Let's return to the unit spawning again:
This very long part is basically the same check as the last one. Only difference, last time we checked for AND connections between multiple resources and here we check for OR connections.
With that out of the way, we now know if the unit is valid and we only need to determine the best unit
We start by rolling a random number between 1-1000. Then we add 200 if the unit has a certain UnitAIType. eBarbUnitAI was set previously in the method to UNITAI_ATTACK_SEA for water units and UNITAI_ATTACK for others. This means if the unit has one of those UnitAITypes it is more likely to spawn. Here is the complete list of barbarian units with these types:
UNITAI_ATTACK:
UNITAI_ATTACK_SEA:
Lastly we check for the unit with the highest value and spawn it.
Summary
The important factors for which units are available for barb spawning are:
Warrior - no requirement
Axeman - Bronze Working and Mining
Spearman - Hunting and Mining
Archer - Archery
Horse Archer - Horseback Riding and Animal Husbandry
Galley - Sailing
Next I will look into how barbarians gain techs. Stay tuned.
EDIT: I was notified that I had a mistake in this. I didn't consider that the barbs have a unit list of their own in the CIV4CivilizationInfos.xml. I edited this post to reflect that.
For this we go to CvGame and there into the rather big createBarbarianUnits method. There's a lot more in the method, but that is so far well known already, like when barbs are spawning. The interesting part is this:
Code:
for (iJ = 0; iJ < GC.getNumUnitClassInfos(); iJ++)
{
bool bValid = false;
eLoopUnit = ((UnitTypes)(GC.getCivilizationInfo(GET_PLAYER(BARBARIAN_PLAYER).getCivilizationType()).getCivilizationUnits(iJ)));
if (eLoopUnit != NO_UNIT)
{
CvUnitInfo& kUnit = GC.getUnitInfo(eLoopUnit);
bValid = (kUnit.getCombat() > 0 && !kUnit.isOnlyDefensive());
if (bValid)
{
if (pLoopArea->isWater() && kUnit.getDomainType() != DOMAIN_SEA)
{
bValid = false;
}
else if (!pLoopArea->isWater() && kUnit.getDomainType() != DOMAIN_LAND)
{
bValid = false;
}
}
if (bValid)
{
if (!GET_PLAYER(BARBARIAN_PLAYER).canTrain(eLoopUnit))
{
bValid = false;
}
}
if (bValid)
{
if (NO_BONUS != kUnit.getPrereqAndBonus())
{
if (!GET_TEAM(BARBARIAN_TEAM).isHasTech((TechTypes)GC.getBonusInfo((BonusTypes)kUnit.getPrereqAndBonus()).getTechCityTrade()))
{
bValid = false;
}
}
}
if (bValid)
{
bool bFound = false;
bool bRequires = false;
for (int i = 0; i < GC.getNUM_UNIT_PREREQ_OR_BONUSES(); ++i)
{
if (NO_BONUS != kUnit.getPrereqOrBonuses(i))
{
TechTypes eTech = (TechTypes)GC.getBonusInfo((BonusTypes)kUnit.getPrereqOrBonuses(i)).getTechCityTrade();
if (NO_TECH != eTech)
{
bRequires = true;
if (GET_TEAM(BARBARIAN_TEAM).isHasTech(eTech))
{
bFound = true;
break;
}
}
}
}
if (bRequires && !bFound)
{
bValid = false;
}
}
if (bValid)
{
iValue = (1 + getSorenRandNum(1000, "Barb Unit Selection"));
if (kUnit.getUnitAIType(eBarbUnitAI))
{
iValue += 200;
}
if (iValue > iBestValue)
{
eBestUnit = eLoopUnit;
iBestValue = iValue;
}
}
}
}
We start this by iterating through all the units available to the civs in this case the barbarians. You can find this info in the CIV4CivilizationInfos.xml. The barbarian civ is at the end. Here is the complete list of units available to the barbs either for spawning or building:
We look through this list for the best valid unit to spawn. First we look if the current unit is valid by doing various checks:
Code:
bValid = (kUnit.getCombat() > 0 && !kUnit.isOnlyDefensive());
We start with a rather simple check. The unit needs to have a strength bigger then 0 (no worker or settler) and it shouldn't be a defensive unit (scouts, explorers, machine guns)
Next part is a little bit more interesting:
Code:
if (bValid)
{
if (pLoopArea->isWater() && kUnit.getDomainType() != DOMAIN_SEA)
{
bValid = false;
}
else if (!pLoopArea->isWater() && kUnit.getDomainType() != DOMAIN_LAND)
{
bValid = false;
}
}
Here we just check that the unit has the right domain to spawn in that area. This pLoopArea was determined earlier in the method. Basically we prevent water units spawning on land and vice versa.
Code:
if (bValid)
{
if (!GET_PLAYER(BARBARIAN_PLAYER).canTrain(eLoopUnit))
{
bValid = false;
}
}
Here we have the first really interesting part. This is your standard canTrain check like it does for every other players. The important things that are checked in canTrain are:
- is the units production cost not -1
- does the player has the necessary technologies
- is the maximum for national units reached
Code:
if (bValid)
{
if (NO_BONUS != kUnit.getPrereqAndBonus())
{
if (!GET_TEAM(BARBARIAN_TEAM).isHasTech((TechTypes)GC.getBonusInfo((BonusTypes)kUnit.getPrereqAndBonus()).getTechCityTrade()))
{
bValid = false;
}
}
}
This code is rather interesting. We check if the unit requires resources. But then we do not check for this resource itself, but rather we check if the barbs have the necessary tech to trade this resource.
Now let me pause quickly here for a second, because this is something that not everybody knows about Civ 4. You actually need certain techs to be able to trade resources. For the most parts these are the same techs that enable the improvement you need to connect the resource anyway, so I won't go through the whole list. You can find all the info here in the BonusXML, which I turned into a spreadsheet here: BonusInfo.XML
The important resources for unit training are:
Copper - tradable with Mining
Horse - tradable with Animal Husbandry
Iron - tradable with Mining
Ivory - tradable with Hunting
Oil - tradable with Combustion
Uranium - tradable with Fission (this is the only resource that needs a different tech then the one enabling the improvement)
By the way this tech requirement for trading resources is the one single reason, why you don't want to skip Agriculture, because without Agriculture you can't get those resources from other players.
Let's return to the unit spawning again:
Code:
if (bValid)
{
bool bFound = false;
bool bRequires = false;
for (int i = 0; i < GC.getNUM_UNIT_PREREQ_OR_BONUSES(); ++i)
{
if (NO_BONUS != kUnit.getPrereqOrBonuses(i))
{
TechTypes eTech = (TechTypes)GC.getBonusInfo((BonusTypes)kUnit.getPrereqOrBonuses(i)).getTechCityTrade();
if (NO_TECH != eTech)
{
bRequires = true;
if (GET_TEAM(BARBARIAN_TEAM).isHasTech(eTech))
{
bFound = true;
break;
}
}
}
}
if (bRequires && !bFound)
{
bValid = false;
}
}
This very long part is basically the same check as the last one. Only difference, last time we checked for AND connections between multiple resources and here we check for OR connections.
With that out of the way, we now know if the unit is valid and we only need to determine the best unit
Code:
if (bValid)
{
iValue = (1 + getSorenRandNum(1000, "Barb Unit Selection"));
if (kUnit.getUnitAIType(eBarbUnitAI))
{
iValue += 200;
}
if (iValue > iBestValue)
{
eBestUnit = eLoopUnit;
iBestValue = iValue;
}
}
We start by rolling a random number between 1-1000. Then we add 200 if the unit has a certain UnitAIType. eBarbUnitAI was set previously in the method to UNITAI_ATTACK_SEA for water units and UNITAI_ATTACK for others. This means if the unit has one of those UnitAITypes it is more likely to spawn. Here is the complete list of barbarian units with these types:
UNITAI_ATTACK:
UNITAI_ATTACK_SEA:
Lastly we check for the unit with the highest value and spawn it.
Summary
The important factors for which units are available for barb spawning are:
- Only units with combat strength > 0 are allowed
- Defensive only units (Scouts, Explorers, Machine Guns) can't spawn for them
- Do the barbarians have the necessary techs for the unit itself
- Do the barbarians have the necessary tech to trade the resource required for the unit
Warrior - no requirement
Axeman - Bronze Working and Mining
Spearman - Hunting and Mining
Archer - Archery
Horse Archer - Horseback Riding and Animal Husbandry
Galley - Sailing
Next I will look into how barbarians gain techs. Stay tuned.
EDIT: I was notified that I had a mistake in this. I didn't consider that the barbs have a unit list of their own in the CIV4CivilizationInfos.xml. I edited this post to reflect that.
Mods: RtR CtH
Pitboss: PB39, PB40, PB52, PB59 Useful Collections: Pickmethods, Mapmaking, Curious Civplayer
Buy me a coffee
Pitboss: PB39, PB40, PB52, PB59 Useful Collections: Pickmethods, Mapmaking, Curious Civplayer
Buy me a coffee