Read that function again. In the merge function, all units are checked to see if they should adopt the AItype of the head unit, not whether the AItype should be changed for the head unit alone. I have a feeling that might be more the intention of whoever designed it though.
I not able to see that? only the new head units ai is changed there to that of the old head units ai. Maybe I should watch it using the debugger.
After looking at this again i still can't see how that function changes every Units AI to that of the head Units AI





Code:
void CvSelectionGroup::mergeIntoGroup(CvSelectionGroup* pSelectionGroup)
{
CvPlayerAI& kPlayer = GET_PLAYER(getOwnerINLINE());
// merge groups, but make sure we do not change the head unit AI
// this means that if a new unit is going to become the head, change its AI to match, if possible
// AI_setUnitAIType removes the unit from the current group (at least currently), so we have to be careful in the loop here
// so, loop until we have not changed unit AIs
bool bChangedUnitAI;
do
{
bChangedUnitAI = false;
// loop over all the units, moving them to the new group,
// stopping if we had to change a unit AI, because doing so removes that unit from our group, so we have to start over
CLLNode<IDInfo>* pUnitNode = headUnitNode();
while (pUnitNode != NULL && !bChangedUnitAI)
{
CvUnit* pLoopUnit = ::getUnit(pUnitNode->m_data);
pUnitNode = nextUnitNode(pUnitNode);
if (pLoopUnit != NULL)
{
UnitAITypes eUnitAI = pLoopUnit->AI_getUnitAIType();
// if the unitAIs are different, and the loop unit has a higher val, then the group unitAI would change
// change this UnitAI to the old group UnitAI if possible
CvUnit* pNewHeadUnit = pSelectionGroup->getHeadUnit();
UnitAITypes eNewHeadUnitAI = pSelectionGroup->getHeadUnitAI();
if (pNewHeadUnit!= NULL && eUnitAI != eNewHeadUnitAI && pLoopUnit->AI_groupFirstVal() > pNewHeadUnit->AI_groupFirstVal())
{
// non-zero AI_unitValue means that this UnitAI is valid for this unit (that is the check used everywhere)
if (kPlayer.AI_unitValue(pLoopUnit->getUnitType(), eNewHeadUnitAI, NULL) > 0)
{
FAssert(pLoopUnit->AI_getUnitAIType() != UNITAI_HUNTER);
// this will remove pLoopUnit from the current group
pLoopUnit->AI_setUnitAIType(eNewHeadUnitAI);
bChangedUnitAI = true;
}
}
pLoopUnit->joinGroup(pSelectionGroup);
}
}
}
while (bChangedUnitAI);
}
It only changes the AI in case another unit becomes the head unit and the old head units AI is valid for the new head unit.. If a unit doesn't become the new head unit no AI Change is made.
Whatever big issue you have been seeing doesn't come from that function. It is valid that the new head unit get's the AI of the old head unit if that AI is a valid AI for the new head unit.