diff --git a/core/src/com/unciv/logic/city/CityInfo.kt b/core/src/com/unciv/logic/city/CityInfo.kt index 854803e988..9be165fa32 100644 --- a/core/src/com/unciv/logic/city/CityInfo.kt +++ b/core/src/com/unciv/logic/city/CityInfo.kt @@ -6,6 +6,7 @@ import com.unciv.logic.civilization.diplomacy.DiplomacyFlags import com.unciv.logic.map.RoadStatus import com.unciv.logic.map.TileInfo import com.unciv.logic.map.TileMap +import com.unciv.models.Counter import com.unciv.models.ruleset.tile.ResourceSupplyList import com.unciv.models.ruleset.tile.ResourceType import com.unciv.models.ruleset.unit.BaseUnit @@ -46,6 +47,7 @@ class CityInfo { var health = 200 var resistanceCounter = 0 + var religion = CityInfoReligionManager() var population = PopulationManager() var cityConstructions = CityConstructions() var expansion = CityExpansionManager() @@ -149,6 +151,7 @@ class CityInfo { toReturn.turnAcquired = turnAcquired toReturn.isPuppet = isPuppet toReturn.isOriginalCapital = isOriginalCapital + toReturn.religion = CityInfoReligionManager().apply { putAll(religion) } return toReturn } @@ -315,6 +318,7 @@ class CityInfo { cityStats.cityInfo = this cityConstructions.cityInfo = this cityConstructions.setTransients() + religion.cityInfo = this } fun startTurn() { @@ -365,6 +369,8 @@ class CityInfo { } } else population.nextTurn(foodForNextTurn()) + if (getRuleset().hasReligion()) religion.getAffectedBySurroundingCities() + if (this in civInfo.cities) { // city was not destroyed health = min(health + 20, getMaxHealth()) population.unassignExtraPopulation() @@ -476,3 +482,47 @@ class CityInfo { //endregion } +class CityInfoReligionManager: Counter(){ + @Transient + lateinit var cityInfo: CityInfo + + fun getNumberOfFollowers(): Counter { + val totalInfluence = values.sum() + val population = cityInfo.population.population + if (totalInfluence > 100 * population) { + val toReturn = Counter() + for ((key, value) in this) + if (value > 100) + toReturn.add(key, value / 100) + return toReturn + } + + val toReturn = Counter() + + for ((key, value) in this) { + val percentage = value.toFloat() / totalInfluence + val relativePopulation = (percentage * population).roundToInt() + toReturn.add(key, relativePopulation) + } + return toReturn + } + + fun getMajorityReligion():String? { + val followersPerReligion = getNumberOfFollowers() + if (followersPerReligion.isEmpty()) return null + val religionWithMaxFollowers = followersPerReligion.maxByOrNull { it.value }!! + if (religionWithMaxFollowers.value >= cityInfo.population.population) return religionWithMaxFollowers.key + else return null + } + + fun getAffectedBySurroundingCities() { + val allCitiesWithin10Tiles = cityInfo.civInfo.gameInfo.civilizations.asSequence().flatMap { it.cities } + .filter { it != cityInfo && it.getCenterTile().aerialDistanceTo(cityInfo.getCenterTile()) <= 10 } + for (city in allCitiesWithin10Tiles) { + val majorityReligionOfCity = city.religion.getMajorityReligion() + if (majorityReligionOfCity == null) continue + else add(majorityReligionOfCity, 6) // todo - when holy cities are implemented, *5 + } + } +} + diff --git a/core/src/com/unciv/models/Counter.kt b/core/src/com/unciv/models/Counter.kt index 5cd9473d4b..bad65ade8a 100644 --- a/core/src/com/unciv/models/Counter.kt +++ b/core/src/com/unciv/models/Counter.kt @@ -2,7 +2,7 @@ package com.unciv.models import java.util.* -class Counter : LinkedHashMap() { +open class Counter : LinkedHashMap() { override operator fun get(key: K): Int? { // don't return null if empty if (containsKey(key))