I have been reviewing the code in CvUnitAI.cpp, function AI_assaultSeaMove, for Beyond the Sword 3.17, and I am finding a curious section of code, which is the following:
if (bEmpty)
{
if (AI_anyAttack(1, 65))
{
return;
}
if (AI_anyAttack(1, 40))
{
return;
}
}
The thing that I am finding curious is the fact that AI_anyAttack gets checked twice: first with a second argument of 65, second with a second argument of 40. From reviewing the code for AI_anyAttack, it seems to me that the above code is identical to the following:
if (bEmpty)
{
if (AI_anyAttack(1, 40))
{
return;
}
}
If I understand AI_anyAttack at its most basic level, it is testing to see if you have at least a [second argument] odds of winning a battle against any enemy unit within a range of [first argument]. Let us consider a very basic scenario with the code that AI_assaultSeaMove is using: There is only one enemy unit within the range of a transport unit that is empty, and there is a 50% chance that the transport unit will win the battle. So AI_anyAttack(1, 65) gets called, and ignoring any extreme calculations that it may do, it returns false because 50 is less than 65. So then AI_anyAttack(1, 40) gets called, but (again ignoring any extreme calculations) it returns true because 50 is greater than 40.
So why is AI_anyAttack(1, 65) being utilized in the code even though AI_anyAttack(1, 40) is called right after it? Isn't AI_anyAttack(1, 65) useless code for situations where the odds are between 40 and 64? I could understand this code being utilized if there was another section of code between the two if statements or if AI_anyAttack was not returning the weakest unit to attack, but this appears to not be the case. Also, I don't see any random calculations that are being done in AI_anyAttack that would result in a situation where AI_anyAttack(1, 65) would return true but AI_anyAttack(1, 40) would return false for a given unit. Am I missing something, or is calling AI_anyAttack(1, 65) inefficient code? Can somebody explain a situation where we would actually need AI_anyAttack(1, 65)?
if (bEmpty)
{
if (AI_anyAttack(1, 65))
{
return;
}
if (AI_anyAttack(1, 40))
{
return;
}
}
The thing that I am finding curious is the fact that AI_anyAttack gets checked twice: first with a second argument of 65, second with a second argument of 40. From reviewing the code for AI_anyAttack, it seems to me that the above code is identical to the following:
if (bEmpty)
{
if (AI_anyAttack(1, 40))
{
return;
}
}
If I understand AI_anyAttack at its most basic level, it is testing to see if you have at least a [second argument] odds of winning a battle against any enemy unit within a range of [first argument]. Let us consider a very basic scenario with the code that AI_assaultSeaMove is using: There is only one enemy unit within the range of a transport unit that is empty, and there is a 50% chance that the transport unit will win the battle. So AI_anyAttack(1, 65) gets called, and ignoring any extreme calculations that it may do, it returns false because 50 is less than 65. So then AI_anyAttack(1, 40) gets called, but (again ignoring any extreme calculations) it returns true because 50 is greater than 40.
So why is AI_anyAttack(1, 65) being utilized in the code even though AI_anyAttack(1, 40) is called right after it? Isn't AI_anyAttack(1, 65) useless code for situations where the odds are between 40 and 64? I could understand this code being utilized if there was another section of code between the two if statements or if AI_anyAttack was not returning the weakest unit to attack, but this appears to not be the case. Also, I don't see any random calculations that are being done in AI_anyAttack that would result in a situation where AI_anyAttack(1, 65) would return true but AI_anyAttack(1, 40) would return false for a given unit. Am I missing something, or is calling AI_anyAttack(1, 65) inefficient code? Can somebody explain a situation where we would actually need AI_anyAttack(1, 65)?
Comment