Guard against crash in getNumThreateningBarbarians without barbs (#5173)

* Guard against crash in getNumThreateningBarbarians without barbs

* Guard against crash in getNumThreateningBarbarians without barbs - missing inits
This commit is contained in:
SomeTroglodyte 2021-09-11 20:09:45 +02:00 committed by GitHub
parent 00e962c9ac
commit 45e87688cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 3 deletions

View File

@ -102,17 +102,35 @@ class GameInfo {
civIndex++
}
}
else return getBarbarianCivilization()// you aren't anyone. How did you even get this game? Can you spectate?
else return getSpectator(userId)// you aren't anyone. How did you even get this game? Can you spectate?
}
/** Get a civ by name
* @throws NoSuchElementException if no civ of than name is in the game (alive or dead)! */
fun getCivilization(civName: String) = civilizations.first { it.civName == civName }
fun getCurrentPlayerCivilization() = currentPlayerCiv
fun getBarbarianCivilization() = getCivilization(Constants.barbarians)
/** Get barbarian civ
* @throws NoSuchElementException in no-barbarians games! */
private fun getBarbarianCivilization() = getCivilization(Constants.barbarians)
fun getDifficulty() = difficultyObject
fun getCities() = civilizations.asSequence().flatMap { it.cities }
fun getAliveCityStates() = civilizations.filter { it.isAlive() && it.isCityState() }
fun getAliveMajorCivs() = civilizations.filter { it.isAlive() && it.isMajorCiv() }
/** Returns the first spectator for a [playerId] or creates one if none found */
fun getSpectator(playerId: String) =
civilizations.firstOrNull {
it.isSpectator() && it.playerId == playerId
} ?:
CivilizationInfo(Constants.spectator).also {
it.playerType = PlayerType.Human
it.playerId = playerId
civilizations.add(it)
it.gameInfo = this
it.setNationTransient()
it.setTransients()
}
fun hasReligionEnabled() =
// Temporary function to check whether religion should be used for this game
(gameParameters.religionEnabled || ruleSet.hasReligion())

View File

@ -463,7 +463,10 @@ class CityStateFunctions(val civInfo: CivilizationInfo) {
}
private fun getNumThreateningBarbarians(): Int {
return civInfo.gameInfo.getBarbarianCivilization().getCivUnits().count { it.threatensCiv(civInfo) }
if (civInfo.gameInfo.gameParameters.noBarbarians) return 0
val barbarianCiv = civInfo.gameInfo.civilizations.firstOrNull { it.isBarbarian() }
?: return 0
return barbarianCiv.getCivUnits().count { it.threatensCiv(civInfo) }
}
fun threateningBarbarianKilledBy(otherCiv: CivilizationInfo) {