mirror of
https://github.com/yairm210/Unciv.git
synced 2025-07-24 14:49:23 +07:00
Fix CS unit gift crash (#6393)
* Fix CS unit gift crash * Clarify CityStateGreatPersonGift decision * Removed misplaced comment
This commit is contained in:
@ -7,6 +7,7 @@ import com.unciv.models.ruleset.Ruleset
|
|||||||
import com.unciv.models.ruleset.tile.ResourceSupplyList
|
import com.unciv.models.ruleset.tile.ResourceSupplyList
|
||||||
import com.unciv.models.ruleset.unique.Unique
|
import com.unciv.models.ruleset.unique.Unique
|
||||||
import com.unciv.models.ruleset.unique.UniqueType
|
import com.unciv.models.ruleset.unique.UniqueType
|
||||||
|
import com.unciv.models.ruleset.unit.BaseUnit
|
||||||
import com.unciv.models.stats.Stat
|
import com.unciv.models.stats.Stat
|
||||||
import com.unciv.ui.victoryscreen.RankingType
|
import com.unciv.ui.victoryscreen.RankingType
|
||||||
import java.util.*
|
import java.util.*
|
||||||
@ -88,14 +89,24 @@ class CityStateFunctions(val civInfo: CivilizationInfo) {
|
|||||||
fun giveMilitaryUnitToPatron(receivingCiv: CivilizationInfo) {
|
fun giveMilitaryUnitToPatron(receivingCiv: CivilizationInfo) {
|
||||||
val cities = NextTurnAutomation.getClosestCities(receivingCiv, civInfo)
|
val cities = NextTurnAutomation.getClosestCities(receivingCiv, civInfo)
|
||||||
val city = cities.city1
|
val city = cities.city1
|
||||||
val uniqueUnit = civInfo.gameInfo.ruleSet.units[civInfo.cityStateUniqueUnit]
|
|
||||||
// If the receiving civ has discovered the required tech and not the obsolete tech for our unique, always give them the unique
|
fun giftableUniqueUnit(): BaseUnit? {
|
||||||
val militaryUnit = if (uniqueUnit != null && receivingCiv.tech.isResearched(uniqueUnit.requiredTech!!)
|
val uniqueUnit = civInfo.gameInfo.ruleSet.units[civInfo.cityStateUniqueUnit]
|
||||||
&& (uniqueUnit.obsoleteTech == null || !receivingCiv.tech.isResearched(uniqueUnit.obsoleteTech!!))) uniqueUnit
|
?: return null
|
||||||
// Otherwise pick at random
|
if (uniqueUnit.requiredTech != null && !receivingCiv.tech.isResearched(uniqueUnit.requiredTech!!))
|
||||||
else city.cityConstructions.getConstructableUnits()
|
return null
|
||||||
.filter { !it.isCivilian() && it.isLandUnit() && it.uniqueTo==null }
|
if (uniqueUnit.obsoleteTech != null && receivingCiv.tech.isResearched(uniqueUnit.obsoleteTech!!))
|
||||||
.toList().random()
|
return null
|
||||||
|
return uniqueUnit
|
||||||
|
}
|
||||||
|
fun randomGiftableUnit() =
|
||||||
|
city.cityConstructions.getConstructableUnits()
|
||||||
|
.filter { !it.isCivilian() && it.isLandUnit() && it.uniqueTo == null }
|
||||||
|
.toList().randomOrNull()
|
||||||
|
val militaryUnit = giftableUniqueUnit() // If the receiving civ has discovered the required tech and not the obsolete tech for our unique, always give them the unique
|
||||||
|
?: randomGiftableUnit() // Otherwise pick at random
|
||||||
|
?: return // That filter _can_ result in no candidates, if so, quit silently
|
||||||
|
|
||||||
// placing the unit may fail - in that case stay quiet
|
// placing the unit may fail - in that case stay quiet
|
||||||
val placedUnit = receivingCiv.placeUnitNearTile(city.location, militaryUnit.name) ?: return
|
val placedUnit = receivingCiv.placeUnitNearTile(city.location, militaryUnit.name) ?: return
|
||||||
|
|
||||||
@ -110,7 +121,12 @@ class CityStateFunctions(val civInfo: CivilizationInfo) {
|
|||||||
// Point to the places mentioned in the message _in that order_ (debatable)
|
// Point to the places mentioned in the message _in that order_ (debatable)
|
||||||
val placedLocation = placedUnit.getTile().position
|
val placedLocation = placedUnit.getTile().position
|
||||||
val locations = LocationAction(placedLocation, cities.city2.location, city.location)
|
val locations = LocationAction(placedLocation, cities.city2.location, city.location)
|
||||||
receivingCiv.addNotification("[${civInfo.civName}] gave us a [${militaryUnit.name}] as gift near [${city.name}]!", locations, civInfo.civName, militaryUnit.name)
|
receivingCiv.addNotification(
|
||||||
|
"[${civInfo.civName}] gave us a [${militaryUnit.name}] as gift near [${city.name}]!",
|
||||||
|
locations,
|
||||||
|
civInfo.civName,
|
||||||
|
militaryUnit.name
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun influenceGainedByGift(donorCiv: CivilizationInfo, giftAmount: Int): Int {
|
fun influenceGainedByGift(donorCiv: CivilizationInfo, giftAmount: Int): Int {
|
||||||
|
@ -876,14 +876,15 @@ class CivilizationInfo {
|
|||||||
if (!flagsCountdown.containsKey(flag)) continue
|
if (!flagsCountdown.containsKey(flag)) continue
|
||||||
|
|
||||||
if (flag == CivFlags.CityStateGreatPersonGift.name) {
|
if (flag == CivFlags.CityStateGreatPersonGift.name) {
|
||||||
val cityStateAllies = getKnownCivs().filter { it.isCityState() && it.getAllyCiv() == civName }
|
val cityStateAllies: List<CivilizationInfo> =
|
||||||
|
getKnownCivs().filter { it.isCityState() && it.getAllyCiv() == civName }
|
||||||
|
val givingCityState = cityStateAllies.filter { it.cities.isNotEmpty() }.randomOrNull()
|
||||||
|
|
||||||
if (cityStateAllies.any()) flagsCountdown[flag] = flagsCountdown[flag]!! - 1
|
if (cityStateAllies.isNotEmpty()) flagsCountdown[flag] = flagsCountdown[flag]!! - 1
|
||||||
|
|
||||||
if (flagsCountdown[flag]!! < min(cityStateAllies.count(), 10) && cities.isNotEmpty()
|
if (flagsCountdown[flag]!! < min(cityStateAllies.size, 10) && cities.isNotEmpty()
|
||||||
&& cityStateAllies.any { it.cities.isNotEmpty() }
|
&& givingCityState != null
|
||||||
) {
|
) {
|
||||||
val givingCityState = getKnownCivs().filter { it.isCityState() && it.getAllyCiv() == civName && it.cities.isNotEmpty()}.random()
|
|
||||||
givingCityState.cityStateFunctions.giveGreatPersonToPatron(this)
|
givingCityState.cityStateFunctions.giveGreatPersonToPatron(this)
|
||||||
flagsCountdown[flag] = turnsForGreatPersonFromCityState()
|
flagsCountdown[flag] = turnsForGreatPersonFromCityState()
|
||||||
}
|
}
|
||||||
@ -1247,10 +1248,9 @@ class CivilizationInfo {
|
|||||||
|
|
||||||
return proximity
|
return proximity
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////////////////// City State wrapper functions ////////////////////////
|
//////////////////////// City State wrapper functions ////////////////////////
|
||||||
|
|
||||||
/** Gain a random great person from the city state */
|
|
||||||
fun receiveGoldGift(donorCiv: CivilizationInfo, giftAmount: Int) =
|
fun receiveGoldGift(donorCiv: CivilizationInfo, giftAmount: Int) =
|
||||||
cityStateFunctions.receiveGoldGift(donorCiv, giftAmount)
|
cityStateFunctions.receiveGoldGift(donorCiv, giftAmount)
|
||||||
fun turnsForGreatPersonFromCityState(): Int = ((37 + Random().nextInt(7)) * gameInfo.gameParameters.gameSpeed.modifier).toInt()
|
fun turnsForGreatPersonFromCityState(): Int = ((37 + Random().nextInt(7)) * gameInfo.gameParameters.gameSpeed.modifier).toInt()
|
||||||
|
Reference in New Issue
Block a user