Harden GetRandomBotName against exceptions

This commit is contained in:
Subsonic154 2026-05-04 08:49:26 -04:00
parent bd6ca8b719
commit efae7f6d8c

View file

@ -95,7 +95,7 @@ class ShootingRangeTargetSpawnerActor(zone: Zone) extends Actor {
private val activeInfantryTargets = ListBuffer[AvatarBot]() private val activeInfantryTargets = ListBuffer[AvatarBot]()
private val activeVehicleTargets = ListBuffer[Vehicle]() private val activeVehicleTargets = ListBuffer[Vehicle]()
private val botNamesInUse = ListBuffer[String]() private var botNamesInUse = List[String]()
override def preStart() = { override def preStart() = {
if (Config.app.game.virtualTraining.shootingRangeTargetsEnabled) { if (Config.app.game.virtualTraining.shootingRangeTargetsEnabled) {
@ -112,7 +112,7 @@ class ShootingRangeTargetSpawnerActor(zone: Zone) extends Actor {
target.Actor ! AvatarBot.Release() target.Actor ! AvatarBot.Release()
} }
activeInfantryTargets.clear() activeInfantryTargets.clear()
botNamesInUse.clear() botNamesInUse = List[String]()
activeVehicleTargets.foreach{ target => activeVehicleTargets.foreach{ target =>
if (target.Actor != Default.Actor) { if (target.Actor != Default.Actor) {
target.Actor ! Vehicle.Deconstruct(None) target.Actor ! Vehicle.Deconstruct(None)
@ -209,27 +209,34 @@ class ShootingRangeTargetSpawnerActor(zone: Zone) extends Actor {
* @return the name as a string * @return the name as a string
*/ */
private def GetRandomBotName(gender: CharacterSex): String = { private def GetRandomBotName(gender: CharacterSex): String = {
gender match { try {
case CharacterSex.Male => gender match {
val availableNames = (maleOnlyBotNames ++ universalBotNames).filterNot(n => botNamesInUse.contains(n)) case CharacterSex.Male =>
if (!availableNames.isEmpty) { val availableNames = (maleOnlyBotNames ++ universalBotNames).filterNot(n => botNamesInUse.contains(n))
val name = availableNames(Random.nextInt(availableNames.size)) if (!availableNames.isEmpty) {
botNamesInUse.addOne(name) val name = availableNames(Random.nextInt(availableNames.size))
name botNamesInUse = botNamesInUse :+ name
} else { name
log.warn(s"Male bot name pool in ${zone.id} is empty!") } else {
"Undefined" log.warn(s"Male bot name pool in ${zone.id} is empty!")
} "Bot"
case CharacterSex.Female => }
val availableNames = (femaleOnlyBotNames ++ universalBotNames).filterNot(n => botNamesInUse.contains(n)) case CharacterSex.Female =>
if (!availableNames.isEmpty) { val availableNames = (femaleOnlyBotNames ++ universalBotNames).filterNot(n => botNamesInUse.contains(n))
val name = availableNames(Random.nextInt(availableNames.size)) if (!availableNames.isEmpty) {
botNamesInUse.addOne(name) val name = availableNames(Random.nextInt(availableNames.size))
name botNamesInUse = botNamesInUse :+ name
} else { name
log.warn(s"Female bot name pool in ${zone.id} is empty!") } else {
"Undefined" log.warn(s"Female bot name pool in ${zone.id} is empty!")
} "Bot"
}
}
} catch {
//while the issue that was causing a mutation during iteration exception to be thrown here rarely should hopefully be fixed now,
//this is still being put here as a fallback to not block the bot from spawning
case ex: Exception =>
"Bot"
} }
} }
@ -291,7 +298,7 @@ class ShootingRangeTargetSpawnerActor(zone: Zone) extends Actor {
//return bot name to name pool //return bot name to name pool
botNamesInUse.indexOf(bot.Name) match { botNamesInUse.indexOf(bot.Name) match {
case -1 => log.warn(s"Failed to restore bot name `${bot.Name}` to the bot name pool!") case -1 => log.warn(s"Failed to restore bot name `${bot.Name}` to the bot name pool!")
case index => botNamesInUse.remove(index) case index => botNamesInUse = botNamesInUse.filterNot(n => n == bot.Name)
} }
true true
} }