From d11cb4779b31b23bef28c20798e919d137e453de Mon Sep 17 00:00:00 2001 From: Duan Tao Date: Mon, 21 Jan 2019 19:36:27 +0800 Subject: [PATCH] AIs trade techs. Also modified AI tech choose strategy. --- core/src/com/unciv/logic/GameInfo.kt | 10 ++--- .../logic/automation/NextTurnAutomation.kt | 41 +++++++++++++++++-- 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/core/src/com/unciv/logic/GameInfo.kt b/core/src/com/unciv/logic/GameInfo.kt index 0a55b340d3..e8c8e18796 100644 --- a/core/src/com/unciv/logic/GameInfo.kt +++ b/core/src/com/unciv/logic/GameInfo.kt @@ -58,11 +58,11 @@ class GameInfo { while(thisPlayer.playerType==PlayerType.AI){ NextTurnAutomation().automateCivMoves(thisPlayer) - if (thisPlayer.tech.techsToResearch.isEmpty()) { // should belong in automation? yes/no? - val researchableTechs = GameBasics.Technologies.values - .filter { !thisPlayer.tech.isResearched(it.name) && thisPlayer.tech.canBeResearched(it.name) } - thisPlayer.tech.techsToResearch.add(researchableTechs.minBy { it.cost }!!.name) - } + // if (thisPlayer.tech.techsToResearch.isEmpty()) { // should belong in automation? yes/no? + // val researchableTechs = GameBasics.Technologies.values + // .filter { !thisPlayer.tech.isResearched(it.name) && thisPlayer.tech.canBeResearched(it.name) } + // thisPlayer.tech.techsToResearch.add(researchableTechs.minBy { it.cost }!!.name) + // } switchTurn() } diff --git a/core/src/com/unciv/logic/automation/NextTurnAutomation.kt b/core/src/com/unciv/logic/automation/NextTurnAutomation.kt index a8beadae50..26ce5b11ef 100644 --- a/core/src/com/unciv/logic/automation/NextTurnAutomation.kt +++ b/core/src/com/unciv/logic/automation/NextTurnAutomation.kt @@ -2,16 +2,19 @@ package com.unciv.logic.automation import com.unciv.logic.city.CityInfo import com.unciv.logic.civilization.CivilizationInfo +import com.unciv.logic.civilization.PlayerType import com.unciv.logic.map.MapUnit import com.unciv.logic.trade.TradeLogic import com.unciv.logic.trade.TradeType import com.unciv.models.gamebasics.GameBasics +import com.unciv.models.gamebasics.tech.Technology import com.unciv.ui.utils.getRandom import kotlin.math.min class NextTurnAutomation{ fun automateCivMoves(civInfo: CivilizationInfo) { + exchangeTechs(civInfo) chooseTechToResearch(civInfo) adoptPolicy(civInfo) exchangeLuxuries(civInfo) @@ -22,11 +25,43 @@ class NextTurnAutomation{ trainSettler(civInfo) } + private fun exchangeTechs(civInfo: CivilizationInfo) { + for (otherCiv in civInfo.diplomacy.values.map { it.otherCiv() }.filter { it.playerType == PlayerType.AI }.sortedBy { it.tech.techsResearched.size }) { + val tradeLogic = TradeLogic(civInfo, otherCiv) + val ourTradableTechs = tradeLogic.ourAvailableOffers + .filter { it.type == TradeType.Technology } + val theirTradableTechs = tradeLogic.theirAvailableOffers + .filter { it.type == TradeType.Technology } + + for (ourOffer in ourTradableTechs) { + val theirOfferList = theirTradableTechs.filter{ + tradeLogic.evaluateOffer(ourOffer, false) >= tradeLogic.evaluateOffer(it, false) + && !tradeLogic.currentTrade.theirOffers.contains(it) } + if (theirOfferList.isNotEmpty()) { + tradeLogic.currentTrade.ourOffers.add(ourOffer) + tradeLogic.currentTrade.theirOffers.add(theirOfferList.getRandom()) + } + } + + if (tradeLogic.currentTrade.theirOffers.isNotEmpty()) { + tradeLogic.acceptTrade() + } + } + } + private fun chooseTechToResearch(civInfo: CivilizationInfo) { if (civInfo.tech.techsToResearch.isEmpty()) { - val researchableTechs = GameBasics.Technologies.values.filter { civInfo.tech.canBeResearched(it.name) } - val techToResearch = researchableTechs.groupBy { it.cost }.minBy { it.key }!!.value.getRandom() - civInfo.tech.techsResearched.add(techToResearch.name) + val researchableTechs = GameBasics.Technologies.values.filter { !civInfo.tech.isResearched(it.name) && civInfo.tech.canBeResearched(it.name) } + val techsGroups = researchableTechs.groupBy { it.cost } + val costs = techsGroups.keys.sorted() + + val tech: Technology + if (techsGroups[costs[0]]!!.size == 1 || costs.size == 1) { + tech = techsGroups[costs[0]]!!.getRandom() + } else { + tech = (techsGroups[costs[0]]!! + techsGroups[costs[1]]!!).getRandom() + } + civInfo.tech.techsToResearch.add(tech.name) } }