Moved hasOpenBorders to a transient bool for "next turn" performance reasons

This commit is contained in:
Yair Morgenstern 2019-04-24 11:58:03 +03:00
parent 07a8b97ed2
commit 8a600ea07d
6 changed files with 180 additions and 160 deletions

File diff suppressed because it is too large Load diff

View file

@ -17,16 +17,17 @@ class NextTurnAutomation{
fun automateCivMoves(civInfo: CivilizationInfo) {
offerPeaceTreaty(civInfo)
exchangeTechs(civInfo)
exchangeLuxuries(civInfo)
chooseTechToResearch(civInfo)
adoptPolicy(civInfo)
exchangeLuxuries(civInfo)
declareWar(civInfo)
automateCityBombardment(civInfo)
buyBuildingOrUnit(civInfo)
automateUnits(civInfo)
reassignWorkedTiles(civInfo)
trainSettler(civInfo)
civInfo.popupAlerts.clear()
civInfo.popupAlerts.clear() // AIs don't care about popups.
}
private fun buyBuildingOrUnit(civInfo: CivilizationInfo) {

View file

@ -428,7 +428,7 @@ class CivilizationInfo {
fun canEnterTiles(otherCiv: CivilizationInfo): Boolean {
if(otherCiv==this) return true
if(isAtWarWith(otherCiv)) return true
if(getDiplomacyManager(otherCiv).hasOpenBorders()) return true
if(getDiplomacyManager(otherCiv).hasOpenBorders) return true
return false
}

View file

@ -18,6 +18,9 @@ enum class DiplomacyFlags{
class DiplomacyManager() {
@Transient lateinit var civInfo: CivilizationInfo
// since this needs to be checked a lot during travel, putting it in a transient is a good performance booster
@Transient var hasOpenBorders=false
lateinit var otherCivName:String
var trades = ArrayList<Trade>()
var diplomaticStatus = DiplomaticStatus.War
@ -29,12 +32,15 @@ class DiplomacyManager() {
toReturn.otherCivName=otherCivName
toReturn.diplomaticStatus=diplomaticStatus
toReturn.trades.addAll(trades.map { it.clone() })
toReturn.flagsCountdown.putAll(flagsCountdown)
toReturn.hasOpenBorders=hasOpenBorders
return toReturn
}
constructor(civilizationInfo: CivilizationInfo, OtherCivName:String) : this() {
civInfo=civilizationInfo
otherCivName=OtherCivName
updateHasOpenBorders()
}
//region pure functions
@ -45,13 +51,6 @@ class DiplomacyManager() {
return 0
}
fun hasOpenBorders(): Boolean {
for(trade in trades)
for(offer in trade.theirOffers)
if(offer.name=="Open Borders" && offer.duration > 0) return true
return false
}
fun canDeclareWar() = (turnsToPeaceTreaty()==0 && diplomaticStatus != DiplomaticStatus.War)
fun otherCiv() = civInfo.gameInfo.getCivilization(otherCivName)
@ -103,6 +102,17 @@ class DiplomacyManager() {
}
}
// for performance reasons we don't want to call this every time we want to see if a unit can move through a tile
fun updateHasOpenBorders(){
hasOpenBorders=false
for(trade in trades)
for(offer in trade.theirOffers)
if(offer.name=="Open Borders" && offer.duration > 0){
hasOpenBorders=true
return
}
}
fun nextTurn(){
for(trade in trades.toList()){
for(offer in trade.ourOffers.union(trade.theirOffers).filter { it.duration>0 })
@ -114,6 +124,7 @@ class DiplomacyManager() {
}
}
removeUntenebleTrades()
updateHasOpenBorders()
for(flag in flagsCountdown.keys.toList()) {
flagsCountdown[flag] = flagsCountdown[flag]!! - 1

View file

@ -17,7 +17,7 @@ class TradeLogic(val ourCivilization:CivilizationInfo, val otherCivilization: Ci
if(civInfo.isAtWarWith(otherCivilization))
offers.add(TradeOffer("Peace Treaty", TradeType.Treaty, 20))
if(!otherCivilization.getDiplomacyManager(civInfo).hasOpenBorders()
if(!otherCivilization.getDiplomacyManager(civInfo).hasOpenBorders
&& civInfo.tech.getTechUniques().contains("Enables Open Borders agreements")
&& otherCivilization.tech.getTechUniques().contains("Enables Open Borders agreements"))
offers.add(TradeOffer("Open Borders", TradeType.Agreement, 30))
@ -57,8 +57,14 @@ class TradeLogic(val ourCivilization:CivilizationInfo, val otherCivilization: Ci
}
fun acceptTrade() {
ourCivilization.getDiplomacyManager(otherCivilization).trades.add(currentTrade)
otherCivilization.getDiplomacyManager(ourCivilization).trades.add(currentTrade.reverse())
ourCivilization.getDiplomacyManager(otherCivilization).apply {
trades.add(currentTrade)
updateHasOpenBorders()
}
otherCivilization.getDiplomacyManager(ourCivilization).apply {
trades.add(currentTrade.reverse())
updateHasOpenBorders()
}
// instant transfers
fun transferTrade(to: CivilizationInfo, from: CivilizationInfo, trade: Trade) {
@ -96,6 +102,7 @@ class TradeLogic(val ourCivilization:CivilizationInfo, val otherCivilization: Ci
transferTrade(ourCivilization,otherCivilization,currentTrade)
transferTrade(otherCivilization,ourCivilization,currentTrade.reverse())
}
}

View file

@ -25,13 +25,12 @@ class OffersListScroll(val onOfferClicked: (TradeOffer) -> Unit) : ScrollPane(nu
for (offertype in values()) {
val labelName = when(offertype){
Gold, Gold_Per_Turn, Treaty,Introduction -> ""
Gold, Gold_Per_Turn, Treaty,Agreement,Introduction -> ""
Luxury_Resource -> "Luxury resources"
Strategic_Resource -> "Strategic resources"
Technology -> "Technologies"
WarDeclaration -> "Declarations of war"
City -> "Cities"
Agreement -> "Agreements"
}
val offersOfType = offersToDisplay.filter { it.type == offertype }
if (labelName!="" && offersOfType.any()) {