You can now trade "gold per turn"

This commit is contained in:
Yair Morgenstern
2018-07-11 19:13:59 +03:00
parent d385ebd10b
commit a1d358e9c8
6 changed files with 96 additions and 58 deletions

View File

@ -1,12 +1,10 @@
package com.unciv.logic.automation
import com.badlogic.gdx.graphics.Color
import com.unciv.logic.HexMath
import com.unciv.logic.city.CityConstructions
import com.unciv.logic.city.CityInfo
import com.unciv.logic.civilization.CivilizationInfo
import com.unciv.logic.map.MapUnit
import com.unciv.logic.map.RoadStatus
import com.unciv.logic.map.TileInfo
import com.unciv.models.gamebasics.GameBasics
import com.unciv.models.gamebasics.unit.Unit
@ -122,7 +120,7 @@ class Automation {
when {
buildableNotWonders.isNotEmpty() // if the civ is in the gold red-zone, build markets or somesuch
&& cityInfo.civInfo.getStatsForNextTurn().values.map { it.gold }.sum()<0
&& cityInfo.civInfo.getStatsForNextTurn().gold <0
&& buildableNotWonders.any { it.gold>0 }
-> currentConstruction = buildableNotWonders.first { it.gold>0 }.name
buildableNotWonders.isNotEmpty() -> currentConstruction = buildableNotWonders.first().name

View File

@ -49,9 +49,11 @@ class CivilizationInfo {
fun isPlayerCivilization() = gameInfo.getPlayerCivilization()==this
fun isBarbarianCivilization() = gameInfo.getBarbarianCivilization()==this
fun getStatsForNextTurn():Stats{
return getStatMapForNextTurn().values.toList().reduce{a,b->a+b}
}
// negative gold hurts science
fun getStatsForNextTurn(): HashMap<String, Stats> {
fun getStatMapForNextTurn(): HashMap<String, Stats> {
val statMap = HashMap<String,Stats>()
for (city in cities){
for(entry in city.cityStats.baseStatList){
@ -75,6 +77,7 @@ class CivilizationInfo {
statMap["Policies"]!!.culture += statMap.values.map { it.happiness }.sum() / 2
}
// negative gold hurts science
// if we have - or 0, then the techs will never be complete and the tech button
// will show a negative number of turns and int.max, respectively
if (statMap.values.map { it.gold }.sum() < 0) {
@ -82,6 +85,9 @@ class CivilizationInfo {
1 - statMap.values.map { it.science }.sum())// Leave at least 1
statMap["Treasury deficit"] = Stats().apply { science = scienceDeficit }
}
val goldDifferenceFromTrade = diplomacy.values.sumBy { it.goldPerTurn() }
if(goldDifferenceFromTrade!=0)
statMap["Trade"] = Stats().apply { gold= goldDifferenceFromTrade.toFloat() }
return statMap
}
@ -182,8 +188,7 @@ class CivilizationInfo {
}
fun endTurn() {
val nextTurnStats = Stats()
for(stat in getStatsForNextTurn().values) nextTurnStats.add(stat)
val nextTurnStats = getStatsForNextTurn()
policies.endTurn(nextTurnStats.culture.toInt())
@ -297,6 +302,17 @@ class DiplomacyManager() {
// var status:DiplomaticStatus = DiplomaticStatus.War
var trades = ArrayList<Trade>()
fun goldPerTurn():Int{
var goldPerTurnForUs = 0
for(trade in trades) {
for (offer in trade.ourOffers.filter { it.type == TradeType.Gold_Per_Turn })
goldPerTurnForUs -= offer.amount
for (offer in trade.theirOffers.filter { it.type == TradeType.Gold_Per_Turn })
goldPerTurnForUs += offer.amount
}
return goldPerTurnForUs
}
fun resourcesFromTrade(): Counter<TileResource> {
val counter = Counter<TileResource>()
for(trade in trades){

View File

@ -36,8 +36,9 @@ class TechManager {
}
fun turnsToTech(TechName: String): Int {
return Math.ceil(((GameBasics.Technologies[TechName]!!.cost - researchOfTech(TechName))
/ civInfo.getStatsForNextTurn().values.sumByDouble { it.science.toDouble()})).toInt()
val remainingScience =GameBasics.Technologies[TechName]!!.cost - researchOfTech(TechName)
return Math.ceil( remainingScience.toDouble()
/ civInfo.getStatsForNextTurn().science).toInt()
}
fun isResearched(TechName: String): Boolean = techsResearched.contains(TechName)

View File

@ -109,7 +109,7 @@ class EmpireOverviewScreen : CameraStageBaseScreen(){
goldTable.defaults().pad(5f)
goldTable.add(Label("Gold", skin).setFont(24)).colspan(2).row()
var total=0f
for (entry in civInfo.getStatsForNextTurn()) {
for (entry in civInfo.getStatMapForNextTurn()) {
if(entry.value.gold==0f) continue
goldTable.add(entry.key)
goldTable.add(entry.value.gold.toString()).row()

View File

@ -61,6 +61,18 @@ class TradeScreen(val otherCivilization: CivilizationInfo) : CameraStageBaseScre
val tradeText = Label("What do you have in mind?".tr(),skin)
val offerButton = TextButton("Offer trade".tr(),skin)
val onChange = {
update()
offerButton.setText("Offer trade".tr())
tradeText.setText("What do you have in mind?".tr())
}
val ourAvailableOffersTable = OffersList(ourAvailableOffers, currentTrade.ourOffers) {onChange()}
val ourOffersTable = OffersList(currentTrade.ourOffers, ourAvailableOffers) {onChange()}
val theirOffersTable = OffersList(currentTrade.theirOffers, theirAvailableOffers) {onChange()}
val theirAvailableOffersTable = OffersList(theirAvailableOffers, currentTrade.theirOffers) {onChange()}
init {
val closeButton = TextButton("Close".tr(), skin)
closeButton.addClickListener { UnCivGame.Current.setWorldScreen() }
@ -98,9 +110,8 @@ class TradeScreen(val otherCivilization: CivilizationInfo) : CameraStageBaseScre
}
}
val newTradeScreen = TradeScreen(otherCivilization)
UnCivGame.Current.screen = newTradeScreen
newTradeScreen.tradeText.setText("Pleasure doing business with you!".tr())
update()
tradeText.setText("Pleasure doing business with you!".tr())
}
}
@ -112,15 +123,6 @@ class TradeScreen(val otherCivilization: CivilizationInfo) : CameraStageBaseScre
stage.addActor(lowerTable)
update()
}
fun update(){
table.clear()
val ourAvailableOffersTable = getTableOfOffers(ourAvailableOffers, currentTrade.ourOffers)
val ourOffersTable = getTableOfOffers(currentTrade.ourOffers, ourAvailableOffers)
val theirOffersTable = getTableOfOffers(currentTrade.theirOffers, theirAvailableOffers)
val theirAvailableOffersTable = getTableOfOffers(theirAvailableOffers, currentTrade.theirOffers)
table.add("Our items".tr())
table.add("Our trade offer".tr())
table.add("[${otherCivilization.civName}]'s trade offer".tr())
@ -131,30 +133,15 @@ class TradeScreen(val otherCivilization: CivilizationInfo) : CameraStageBaseScre
table.add(theirAvailableOffersTable).size(stage.width/4,stage.width/2)
table.pack()
table.center(stage)
update()
}
fun getTableOfOffers(offers: TradeOffersList, correspondingOffers: TradeOffersList): ScrollPane {
val table= Table(skin).apply { defaults().pad(5f) }
for(offer in offers) {
var buttonText = offer.name+" ("+offer.amount+")"
if(offer.duration>1) buttonText+="\n"+offer.duration+" {turns}".tr()
val tb = TextButton(buttonText,skin)
val amountPerClick =
if(offer.type==TradeType.Gold) 50
else 1
if(offer.amount>0)
tb.addClickListener {
val amountTransferred = min(amountPerClick, offer.amount)
offers += offer.copy(amount = -amountTransferred)
correspondingOffers += offer.copy(amount = amountTransferred)
offerButton.setText("Offer trade".tr())
tradeText.setText("What do you have in mind?".tr())
update()
}
else tb.disable() // for instance we have negative gold
table.add(tb).row()
}
return ScrollPane(table)
fun update(){
ourAvailableOffersTable.update()
ourOffersTable.update()
theirAvailableOffersTable.update()
theirOffersTable.update()
}
fun getAvailableOffers(civInfo: CivilizationInfo): TradeOffersList {
@ -165,6 +152,7 @@ class TradeScreen(val otherCivilization: CivilizationInfo) : CameraStageBaseScre
offers.add(TradeOffer(entry.key.name, resourceTradeType, 30, entry.value))
}
offers.add(TradeOffer("Gold".tr(),TradeType.Gold,0,civInfo.gold))
offers.add(TradeOffer("Gold per turn".tr(),TradeType.Gold_Per_Turn,30,civInfo.getStatsForNextTurn().gold.toInt()))
return offers
}
@ -175,18 +163,54 @@ class TradeScreen(val otherCivilization: CivilizationInfo) : CameraStageBaseScre
}
fun evaluateOffer(offer:TradeOffer, otherCivIsRecieving:Boolean): Int {
if(offer.type==TradeType.Gold) return offer.amount
if(offer.type == TradeType.Luxury_Resource){
var value = 100*offer.amount
if(!theirAvailableOffers.any { it.name==offer.name }) // We want to take away their last luxury or give them one they don't have
value += 250
else if(!otherCivIsRecieving && !ourAvailableOffers.any { it.name==offer.name })
value += 250 // they're giving us a luxury we don't have yet
return value // this is useful only as a barter trade to other civs
when(offer.type) {
TradeType.Gold -> return offer.amount
TradeType.Gold_Per_Turn -> return offer.amount*offer.duration
TradeType.Luxury_Resource -> {
var value = 100*offer.amount
if(!theirAvailableOffers.any { it.name==offer.name }) // We want to take away their last luxury or give them one they don't have
value += 250
else if(!otherCivIsRecieving && !ourAvailableOffers.any { it.name==offer.name })
value += 250 // they're giving us a luxury we don't have yet
return value // this is useful only as a barter trade to other civs
}
TradeType.Strategic_Resource -> return 50 * offer.amount
// Dunno what this is?
else -> return 1000
}
if(offer.type == TradeType.Strategic_Resource){
return 50 * offer.amount
}
return 1000 // Dunno what this is?
}
}
class OffersList(val offers: TradeOffersList, val correspondingOffers: TradeOffersList,
val onChange: () -> Unit) : ScrollPane(null) {
val table= Table(CameraStageBaseScreen.skin).apply { defaults().pad(5f) }
init {
update()
}
fun update() {
table.clear()
for(offer in offers.sortedBy { it.type }) {
var buttonText = offer.name+" ("+offer.amount+")"
if(offer.duration>1) buttonText+="\n"+offer.duration+" {turns}".tr()
val tb = TextButton(buttonText, CameraStageBaseScreen.skin)
val amountPerClick =
if(offer.type==TradeType.Gold) 50
else 1
if(offer.amount>0)
tb.addClickListener {
val amountTransferred = min(amountPerClick, offer.amount)
offers += offer.copy(amount = -amountTransferred)
correspondingOffers += offer.copy(amount = amountTransferred)
onChange()
update()
}
else tb.disable() // for instance we have negative gold
table.add(tb).row()
}
widget = table
}
}

View File

@ -130,8 +130,7 @@ class WorldScreenTopBar(val screen: WorldScreen) : Table() {
turnsLabel.setText("Turn".tr()+" " + civInfo.gameInfo.turns + " | "+ abs(year)+(if (year<0) " BCE" else " CE"))
val nextTurnStats = Stats()
for(stat in civInfo.getStatsForNextTurn().values) nextTurnStats.add(stat)
val nextTurnStats = civInfo.getStatsForNextTurn()
val goldPerTurn = "(" + (if (nextTurnStats.gold > 0) "+" else "") + Math.round(nextTurnStats.gold) + ")"
goldLabel.setText("" + Math.round(civInfo.gold.toFloat()) + goldPerTurn)