/* Based on code cut-and-pasted from createBarbarianUnits,
but extensively modified. */
int CvGame::numBarbariansToSpawn(int tilesPerUnit, int nTiles,
int nUnowned, int nUnitsPresent, int nBarbarianCities) {
double divisor = tilesPerUnit; // Floating point to curb rounding errors
if(isOption(GAMEOPTION_RAGING_BARBARIANS)) {
int nOwned = nTiles - nUnowned;
int peakPercent = std::max(0, std::min(100,
GC.getDefineINT("ADVRI_RAGE-SPAWNING-PEAK_PERCENT")));
if(peakPercent > 0 && nOwned > 0) { /* i.e. full "Rage" immediately
if none owned */
double peakQuotient = peakPercent / 100.0;
/* When barbarians first appear, at most quintuple the divisor, i.e.
use at least one fifth of the peak spawn rate. This lower bound
for barbarian activity increases over time to make sure that e.g. an
isolated civ that doesn't expand comes under pressure eventually. */
divisor *= std::min<double>(
(5.0 * turnsElapsedForBarbarianCreation()) / getElapsedGameTurns(),
std::max(1.0, (nTiles * peakQuotient) / nOwned));
}
// Double spawn rate for Rage (no change here)
divisor = std::max(1.0, (divisor / 2));
}
int r = 0;
if (divisor > 0) {
int initialDefenders = GC.getHandicapInfo(getHandicapType()).
getBarbarianInitialDefenders();
r = (int)(((nUnowned / divisor) - nUnitsPresent)
+ nBarbarianCities * std::max(0, initialDefenders - 1));
}
if(r > 0)
r = (r / 4) + 1;
return std::max(r, 0);
}
// Cut-and-pasted from createBarbarianUnits
int CvGame::turnsElapsedForBarbarianCreation() {
return ((GC.getHandicapInfo(getHandicapType()).
getBarbarianCreationTurnsElapsed() *
GC.getGameSpeedInfo(getGameSpeedType()).
getBarbPercent()) / 100);
}