Better trade cancellation when all cities are offered (also includes player-to-player cases, not only AI)

This commit is contained in:
Yair Morgenstern
2020-09-25 15:33:12 +03:00
parent 009c59b77c
commit 43bef4d88d
2 changed files with 13 additions and 11 deletions

View File

@ -12,11 +12,18 @@ import kotlin.math.sqrt
class TradeEvaluation{ class TradeEvaluation{
fun isTradeValid(trade:Trade, offerer:CivilizationInfo, tradePartner: CivilizationInfo): Boolean { fun isTradeValid(trade:Trade, offerer:CivilizationInfo, tradePartner: CivilizationInfo): Boolean {
for(offer in trade.ourOffers)
if(!isOfferValid(offer,offerer)) // Edge case time! Guess what happens if you offer a peace agreement to the AI for all their cities except for the capital,
// and then capture their capital THAT SAME TURN? It can agree, leading to the civilization getting instantly destroyed!
if (trade.ourOffers.count { it.type == TradeType.City } == offerer.cities.size
|| trade.theirOffers.count { it.type == TradeType.City } == tradePartner.cities.size)
return false
for (offer in trade.ourOffers)
if (!isOfferValid(offer, offerer))
return false return false
for(offer in trade.theirOffers) for (offer in trade.theirOffers)
if(!isOfferValid(offer,tradePartner)) if (!isOfferValid(offer, tradePartner))
return false return false
return true return true
} }
@ -43,11 +50,6 @@ class TradeEvaluation{
} }
fun isTradeAcceptable(trade: Trade, evaluator: CivilizationInfo, tradePartner: CivilizationInfo): Boolean { fun isTradeAcceptable(trade: Trade, evaluator: CivilizationInfo, tradePartner: CivilizationInfo): Boolean {
// Edge case time! Guess what happens if you offer a peace agreement to the AI for all their cities except for the capital,
// and then capture their capital THAT SAME TURN? It can agree, leading to the civilization getting instantly destroyed!
if (trade.ourOffers.count { it.type == TradeType.City } == evaluator.cities.size)
return false
var sumOfTheirOffers = trade.theirOffers.asSequence() var sumOfTheirOffers = trade.theirOffers.asSequence()
.filter { it.type != TradeType.Treaty } // since treaties should only be evaluated once for 2 sides .filter { it.type != TradeType.Treaty } // since treaties should only be evaluated once for 2 sides
.map { evaluateBuyCost(it, evaluator, tradePartner) }.sum() .map { evaluateBuyCost(it, evaluator, tradePartner) }.sum()

View File

@ -49,8 +49,8 @@ object ImageGetter {
fun reload(){ fun reload(){
textureRegionDrawables.clear() textureRegionDrawables.clear()
// These are the drawables from the base game // These are the drawables from the base game
for(region in atlas.regions){ for(region in atlas.regions) {
val drawable =TextureRegionDrawable(region) val drawable = TextureRegionDrawable(region)
textureRegionDrawables[region.name] = drawable textureRegionDrawables[region.name] = drawable
} }