Fixed crash when starting new game

This commit is contained in:
Yair Morgenstern 2018-03-07 22:35:41 +02:00
parent 7f8e1707be
commit 414025dbbe
2 changed files with 79 additions and 84 deletions

View file

@ -12,87 +12,80 @@ class CityStats {
@Transient @JvmField var currentCityStats: Stats = Stats() // This is so we won't have to calculate this multiple times - takes a lot of time, especially on phones
@Transient lateinit var cityInfo: CityInfo
private val statsFromTiles: Stats
get() {
val stats = Stats()
for (cell in cityInfo.tilesInRange.filter { cityInfo.name == it.workingCity })
stats.add(cell.getTileStats(cityInfo, cityInfo.civInfo))
return stats
}
private fun getStatsFromTiles(): Stats {
val stats = Stats()
for (cell in cityInfo.tilesInRange.filter { cityInfo.name == it.workingCity })
stats.add(cell.getTileStats(cityInfo, cityInfo.civInfo))
return stats
}
private
val statsFromTradeRoute: Stats
get() {
val stats = Stats()
if (!isCapital && isConnectedToCapital(RoadStatus.Road)) {
val civInfo = cityInfo.civInfo
var goldFromTradeRoute = civInfo.capital.population.population * 0.15 + cityInfo.population.population * 1.1 - 1 // Calculated by http://civilization.wikia.com/wiki/Trade_route_(Civ5)
if (civInfo.policies.isAdopted("Trade Unions")) goldFromTradeRoute += 2.0
if (civInfo.buildingUniques.contains("TradeRouteGoldIncrease")) goldFromTradeRoute *= 1.25 // Machu Pichu speciality
stats.gold += goldFromTradeRoute.toFloat()
}
return stats
fun getStatsFromTradeRoute(): Stats {
val stats = Stats()
if (!isCapital && isConnectedToCapital(RoadStatus.Road)) {
val civInfo = cityInfo.civInfo
var goldFromTradeRoute = civInfo.capital.population.population * 0.15 + cityInfo.population.population * 1.1 - 1 // Calculated by http://civilization.wikia.com/wiki/Trade_route_(Civ5)
if (civInfo.policies.isAdopted("Trade Unions")) goldFromTradeRoute += 2.0
if (civInfo.buildingUniques.contains("TradeRouteGoldIncrease")) goldFromTradeRoute *= 1.25 // Machu Pichu speciality
stats.gold += goldFromTradeRoute.toFloat()
}
return stats
}
private fun getStatsFromProduction(): Stats {
val stats = Stats()
if ("Gold" == cityInfo.cityConstructions.currentConstruction) stats.gold += stats.production / 4
if ("Science" == cityInfo.cityConstructions.currentConstruction) {
var scienceProduced = stats.production / 4
if (cityInfo.civInfo.buildingUniques.contains("ScienceConversionIncrease")) scienceProduced *= 1.33f
if (cityInfo.civInfo.policies.isAdopted("Rationalism")) scienceProduced *= 1.33f
stats.science += scienceProduced
}
return stats
}
private fun getStatPercentBonusesFromRailroad(): Stats {
val stats = Stats()
if (cityInfo.civInfo.tech.isResearched("Combustion") && (isCapital || isConnectedToCapital(RoadStatus.Railroad)))
stats.production += 25f
return stats
}
private fun getStatPercentBonusesFromMarble(): Stats {
val stats = Stats()
val construction = cityInfo.cityConstructions.getCurrentConstruction()
if (construction is Building
&& construction.isWonder
&& cityInfo.civInfo.getCivResources().containsKey(GameBasics.TileResources["Marble"]))
stats.production += 15f
return stats
}
private fun getStatPercentBonusesFromComputers(): Stats {
val stats = Stats()
if (cityInfo.civInfo.tech.isResearched("Computers")) {
stats.production += 10f
stats.science += 10f
}
return stats
}
private val statsFromProduction: Stats
get() {
val stats = Stats()
if ("Gold" == cityInfo.cityConstructions.currentConstruction) stats.gold += stats.production / 4
if ("Science" == cityInfo.cityConstructions.currentConstruction) {
var scienceProduced = stats.production / 4
if (cityInfo.civInfo.buildingUniques.contains("ScienceConversionIncrease")) scienceProduced *= 1.33f
if (cityInfo.civInfo.policies.isAdopted("Rationalism")) scienceProduced *= 1.33f
stats.science += scienceProduced
}
return stats
}
private val statPercentBonusesFromRailroad: Stats
get() {
val stats = Stats()
if (cityInfo.civInfo.tech.isResearched("Combustion") && (isCapital || isConnectedToCapital(RoadStatus.Railroad)))
stats.production += 25f
return stats
}
private val statPercentBonusesFromMarble: Stats
get() {
val stats = Stats()
val construction = cityInfo.cityConstructions.getCurrentConstruction()
if (construction is Building
&& construction.isWonder
&& cityInfo.civInfo.getCivResources().containsKey(GameBasics.TileResources["Marble"]))
stats.production += 15f
return stats
}
private val statPercentBonusesFromComputers: Stats
get() {
val stats = Stats()
if (cityInfo.civInfo.tech.isResearched("Computers")) {
stats.production += 10f
stats.science += 10f
}
return stats
}
private val growthBonusFromPolicies: Float
get() {
var bonus = 0f
if (cityInfo.civInfo.policies.isAdopted("Landed Elite") && isCapital)
bonus += 0.1f
if (cityInfo.civInfo.policies.isAdopted("Tradition Complete"))
bonus += 0.15f
return bonus
}
private fun getGrowthBonusFromPolicies(): Float {
var bonus = 0f
if (cityInfo.civInfo.policies.isAdopted("Landed Elite") && isCapital)
bonus += 0.1f
if (cityInfo.civInfo.policies.isAdopted("Tradition Complete"))
bonus += 0.15f
return bonus
}
// needs to be a separate function because we need to know the global happiness state
// in order to determine how much food is produced in a city!
@ -197,22 +190,22 @@ class CityStats {
stats.science += cityInfo.population.population.toFloat()
stats.production += cityInfo.population.freePopulation.toFloat()
stats.add(statsFromTiles)
stats.add(getStatsFromTiles())
stats.add(getStatsFromSpecialists(cityInfo.population.specialists, civInfo.policies.adoptedPolicies))
stats.add(statsFromTradeRoute)
stats.add(getStatsFromTradeRoute())
stats.add(cityInfo.cityConstructions.getStats())
stats.add(getStatsFromPolicies(civInfo.policies.adoptedPolicies))
val statPercentBonuses = cityInfo.cityConstructions.getStatPercentBonuses()
statPercentBonuses.add(getStatPercentBonusesFromGoldenAge(cityInfo.civInfo.goldenAges.isGoldenAge()))
statPercentBonuses.add(getStatPercentBonusesFromPolicies(civInfo.policies.adoptedPolicies, cityInfo.cityConstructions))
statPercentBonuses.add(statPercentBonusesFromRailroad)
statPercentBonuses.add(statPercentBonusesFromMarble)
statPercentBonuses.add(statPercentBonusesFromComputers)
statPercentBonuses.add(getStatPercentBonusesFromRailroad())
statPercentBonuses.add(getStatPercentBonusesFromMarble())
statPercentBonuses.add(getStatPercentBonusesFromComputers())
stats.production *= 1 + statPercentBonuses.production / 100 // So they get bonuses for production and gold/science
stats.add(statsFromProduction)
stats.add(getStatsFromProduction())
stats.gold *= 1 + statPercentBonuses.gold / 100
@ -226,7 +219,7 @@ class CityStats {
stats.food += cityInfo.population.numberOfSpecialists.toFloat()
if (isUnhappy) stats.food /= 4f // Reduce excess food to 1/4 per the same
stats.food *= 1 + growthBonusFromPolicies
stats.food *= 1 + getGrowthBonusFromPolicies()
stats.gold -= cityInfo.cityConstructions.getMaintenanceCosts().toFloat() // this is AFTER the bonus calculation!
this.currentCityStats = stats
@ -238,7 +231,7 @@ class CityStats {
val capitalTile = cityInfo.civInfo.capital.tile
val tilesReached = HashSet<TileInfo>()
var tilesToCheck : List<TileInfo> = listOf(cityInfo.tile)
while (tilesToCheck.any()) {
while (tilesToCheck.isNotEmpty()) {
val newTiles = tilesToCheck
.flatMap { cityInfo.tileMap.getTilesInDistance(it.position, 1) }.distinct()
.filter{ !tilesReached.contains(it) && !tilesToCheck.contains(it)

View file

@ -47,7 +47,9 @@ class RandomMapGenerator {
}
private fun getRandomResource(resources: List<TileResource>, resourceType: ResourceType): TileResource? {
return resources.filter { it.resourceType == resourceType }.getRandom()
val filtered = resources.filter { it.resourceType == resourceType }
if (filtered.isEmpty()) return null
else return filtered.getRandom()
}