WorldScreen now accepts player as parameter - important for multiplayer so people could see their own map even when it's someone else's turn

This commit is contained in:
Yair Morgenstern 2019-08-04 22:47:49 +03:00
parent f3ba4b5486
commit 7b37e426a9
8 changed files with 78 additions and 68 deletions

View file

@ -2,6 +2,7 @@ package com.unciv
import com.badlogic.gdx.Game
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.Input
import com.unciv.logic.GameInfo
import com.unciv.logic.GameSaver
import com.unciv.models.gamebasics.GameBasics
@ -26,7 +27,7 @@ class UnCivGame(val version: String) : Game() {
override fun create() {
Current = this
Gdx.input.isCatchBackKey=true
Gdx.input.setCatchKey(Input.Keys.BACK, true)
GameBasics.run { } // just to initialize the GameBasics
settings = GameSaver().getGeneralSettings()
if (GameSaver().getSave("Autosave").exists()) {
@ -41,7 +42,7 @@ class UnCivGame(val version: String) : Game() {
fun loadGame(gameInfo:GameInfo){
this.gameInfo = gameInfo
worldScreen = WorldScreen()
worldScreen = WorldScreen(gameInfo.currentPlayerCiv)
setWorldScreen()
}
@ -53,7 +54,7 @@ class UnCivGame(val version: String) : Game() {
val newGame = GameStarter().startNewGame(GameParameters().apply { difficulty="Chieftain" })
gameInfo = newGame
worldScreen = WorldScreen()
worldScreen = WorldScreen(gameInfo.currentPlayerCiv)
setWorldScreen()
}
@ -76,7 +77,7 @@ class UnCivGame(val version: String) : Game() {
return create()
if(::worldScreen.isInitialized) worldScreen.dispose() // I hope this will solve some of the many OuOfMemory exceptions...
worldScreen = WorldScreen()
worldScreen = WorldScreen(gameInfo.currentPlayerCiv)
setWorldScreen()
}

View file

@ -4,6 +4,7 @@ import com.badlogic.gdx.math.Vector2
import java.util.*
import kotlin.math.abs
import kotlin.math.max
import kotlin.math.sqrt
class HexMath {
@ -24,8 +25,8 @@ class HexMath {
fun hex2WorldCoords(hexCoord: Vector2): Vector2 {
// Distance between cells = 2* normal of triangle = 2* (sqrt(3)/2) = sqrt(3)
val xVector = getVectorByClockHour(10).scl(Math.sqrt(3.0).toFloat())
val yVector = getVectorByClockHour(2).scl(Math.sqrt(3.0).toFloat())
val xVector = getVectorByClockHour(10).scl(sqrt(3.0).toFloat())
val yVector = getVectorByClockHour(2).scl(sqrt(3.0).toFloat())
return xVector.scl(hexCoord.x).add(yVector.scl(hexCoord.y))
}

View file

@ -16,13 +16,19 @@ import com.unciv.models.stats.Stats
class CityStats {
@Transient var baseStatList = LinkedHashMap<String, Stats>()
@Transient var statPercentBonusList = LinkedHashMap<String, Stats>()
@Transient var happinessList = LinkedHashMap<String, Float>()
@Transient var foodEaten=0f
@Transient
var baseStatList = LinkedHashMap<String, Stats>()
@Transient
var statPercentBonusList = LinkedHashMap<String, Stats>()
@Transient
var happinessList = LinkedHashMap<String, Float>()
@Transient
var foodEaten = 0f
@Transient 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
@Transient
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
//region pure fuctions
private fun getStatsFromTiles(): Stats {
@ -38,7 +44,7 @@ class CityStats {
if (!cityInfo.isCapital() && cityInfo.isConnectedToCapital()) {
val civInfo = cityInfo.civInfo
var goldFromTradeRoute = civInfo.getCapital().population.population * 0.15 + cityInfo.population.population * 1.1 - 1 // Calculated by http://civilization.wikia.com/wiki/Trade_route_(Civ5)
if(civInfo.getNation().unique=="+1 Gold from each Trade Route, Oil resources provide double quantity") goldFromTradeRoute += 1
if (civInfo.getNation().unique == "+1 Gold from each Trade Route, Oil resources provide double quantity") goldFromTradeRoute += 1
if (civInfo.policies.isAdopted("Trade Unions")) goldFromTradeRoute += 2
if (civInfo.containsBuildingUnique("Gold from all trade routes +25%")) goldFromTradeRoute *= 1.25 // Machu Pichu speciality
stats.gold += goldFromTradeRoute.toFloat()
@ -116,7 +122,7 @@ class CityStats {
val stats = Stats()
val civUnique = cityInfo.civInfo.getNation().unique
if(civUnique == "+2 Culture per turn from cities before discovering Steam Power")
if (civUnique == "+2 Culture per turn from cities before discovering Steam Power")
stats.culture += 2
return stats
@ -141,15 +147,15 @@ class CityStats {
val civUnique = cityInfo.civInfo.getNation().unique
val currentConstruction = cityInfo.cityConstructions.getCurrentConstruction()
if(civUnique=="+25% Production towards any buildings that already exist in the Capital"
&& currentConstruction is Building
&& cityInfo.civInfo.getCapital().cityConstructions.builtBuildings
if (civUnique == "+25% Production towards any buildings that already exist in the Capital"
&& currentConstruction is Building
&& cityInfo.civInfo.getCapital().cityConstructions.builtBuildings
.contains(currentConstruction.name))
stats.production+=25f
stats.production += 25f
if(civUnique=="+20% production towards Wonder construction"
&& currentConstruction is Building && currentConstruction.isWonder)
stats.production+=20
if (civUnique == "+20% production towards Wonder construction"
&& currentConstruction is Building && currentConstruction.isWonder)
stats.production += 20
return stats
}
@ -167,18 +173,18 @@ class CityStats {
// 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!
// -3 happiness per city
fun updateCityHappiness(){
fun updateCityHappiness() {
val civInfo = cityInfo.civInfo
val newHappinessList = LinkedHashMap<String,Float>()
val newHappinessList = LinkedHashMap<String, Float>()
var unhappinessModifier = civInfo.getDifficulty().unhappinessModifier
if(!civInfo.isPlayerCivilization())
if (!civInfo.isPlayerCivilization())
unhappinessModifier *= civInfo.gameInfo.getDifficulty().aiUnhappinessModifier
var unhappinessFromCity=-3f
if (civInfo.getNation().unique=="Unhappiness from number of Cities doubled, Unhappiness from number of Citizens halved.")
unhappinessFromCity*=2f//doubled for the Indian
newHappinessList ["Cities"] = unhappinessFromCity * unhappinessModifier
var unhappinessFromCity = -3f
if (civInfo.getNation().unique == "Unhappiness from number of Cities doubled, Unhappiness from number of Citizens halved.")
unhappinessFromCity *= 2f//doubled for the Indian
newHappinessList["Cities"] = unhappinessFromCity * unhappinessModifier
var unhappinessFromCitizens = cityInfo.population.population.toFloat()
if (civInfo.policies.isAdopted("Democracy"))
@ -187,7 +193,7 @@ class CityStats {
unhappinessFromCitizens *= 0.9f
if (civInfo.policies.isAdopted("Meritocracy"))
unhappinessFromCitizens *= 0.95f
if (civInfo.getNation().unique=="Unhappiness from number of Cities doubled, Unhappiness from number of Citizens halved.")
if (civInfo.getNation().unique == "Unhappiness from number of Cities doubled, Unhappiness from number of Citizens halved.")
unhappinessFromCitizens *= 0.5f //halved for the Indian
newHappinessList["Population"] = -unhappinessFromCitizens * unhappinessModifier
@ -199,38 +205,38 @@ class CityStats {
happinessFromPolicies += (cityInfo.population.population / 2).toFloat()
if (civInfo.policies.isAdopted("Meritocracy") && cityInfo.isConnectedToCapital())
happinessFromPolicies += 1f
if(civInfo.policies.isAdopted("Military Caste") && cityInfo.getCenterTile().militaryUnit!=null)
happinessFromPolicies+=1
if (civInfo.policies.isAdopted("Military Caste") && cityInfo.getCenterTile().militaryUnit != null)
happinessFromPolicies += 1
newHappinessList ["Policies"] = happinessFromPolicies
newHappinessList["Policies"] = happinessFromPolicies
val happinessFromBuildings = cityInfo.cityConstructions.getStats().happiness.toInt().toFloat()
newHappinessList ["Buildings"] = happinessFromBuildings
newHappinessList["Buildings"] = happinessFromBuildings
if(civInfo.containsBuildingUnique("+1 happiness in each city"))
if (civInfo.containsBuildingUnique("+1 happiness in each city"))
newHappinessList["Wonders"] = 1f
// we don't want to modify the existing happiness list because that leads
// to concurrency problems if we iterate on it while changing
happinessList=newHappinessList
happinessList = newHappinessList
}
fun getStatsOfSpecialist(stat:Stat, policies: HashSet<String>): Stats {
fun getStatsOfSpecialist(stat: Stat, policies: HashSet<String>): Stats {
val stats = Stats()
if(stat==Stat.Culture||stat==Stat.Science) stats.add(stat,3f)
else stats.add(stat,2f) // science and gold specialists
if (stat == Stat.Culture || stat == Stat.Science) stats.add(stat, 3f)
else stats.add(stat, 2f) // science and gold specialists
if (policies.contains("Commerce Complete")) stats.gold += 1
if (policies.contains("Secularism")) stats.science += 2
if(cityInfo.containsBuildingUnique("+1 Production from specialists"))
if (cityInfo.containsBuildingUnique("+1 Production from specialists"))
stats.production += 1
return stats
}
private fun getStatsFromSpecialists(specialists: Stats, policies: HashSet<String>): Stats {
val stats = Stats()
for(entry in specialists.toHashMap().filter { it.value>0 })
stats.add(getStatsOfSpecialist(entry.key,policies)*entry.value)
for (entry in specialists.toHashMap().filter { it.value > 0 })
stats.add(getStatsOfSpecialist(entry.key, policies) * entry.value)
return stats
}
@ -248,7 +254,7 @@ class CityStats {
stats.culture += 1f
if (adoptedPolicies.contains("Republic"))
stats.production += 1f
if(adoptedPolicies.contains("Military Caste") && cityInfo.getCenterTile().militaryUnit!=null)
if (adoptedPolicies.contains("Military Caste") && cityInfo.getCenterTile().militaryUnit != null)
stats.culture += 2
if (adoptedPolicies.contains("Universal Suffrage"))
stats.production += (cityInfo.population.population / 5).toFloat()
@ -272,15 +278,15 @@ class CityStats {
if (cityInfo.civInfo.containsBuildingUnique("Culture in all cities increased by 25%")) stats.culture += 25f
val currentConstruction = cityInfo.cityConstructions.getCurrentConstruction()
if(currentConstruction is Building && currentConstruction.uniques.contains("Spaceship part")){
if(cityInfo.civInfo.containsBuildingUnique("Increases production of spaceship parts by 25%"))
if (currentConstruction is Building && currentConstruction.uniques.contains("Spaceship part")) {
if (cityInfo.civInfo.containsBuildingUnique("Increases production of spaceship parts by 25%"))
stats.production += 25
if(cityInfo.containsBuildingUnique("Increases production of spaceship parts by 50%"))
if (cityInfo.containsBuildingUnique("Increases production of spaceship parts by 50%"))
stats.production += 50
}
if(currentConstruction is BaseUnit && currentConstruction.unitType==UnitType.Mounted
&& cityInfo.containsBuildingUnique("+15% Production when building Mounted Units in this city"))
if (currentConstruction is BaseUnit && currentConstruction.unitType == UnitType.Mounted
&& cityInfo.containsBuildingUnique("+15% Production when building Mounted Units in this city"))
stats.production += 15
return stats
@ -295,10 +301,10 @@ class CityStats {
stats.production += 50f
if (policies.contains("Republic") && currentConstruction is Building)
stats.production += 5f
if(policies.contains("Warrior Code") && currentConstruction is BaseUnit && currentConstruction.unitType.isMelee())
if (policies.contains("Warrior Code") && currentConstruction is BaseUnit && currentConstruction.unitType.isMelee())
stats.production += 20
if (policies.contains("Piety")
&& listOf("Monument", "Temple", "Opera House", "Museum", "Broadcast Tower").contains(currentConstruction.name))
&& listOf("Monument", "Temple", "Opera House", "Museum", "Broadcast Tower").contains(currentConstruction.name))
stats.production += 15f
if (policies.contains("Reformation") && cityConstructions.getBuiltBuildings().any { it.isWonder })
stats.culture += 33f
@ -306,7 +312,7 @@ class CityStats {
stats.gold += 25f
if (policies.contains("Sovereignty") && cityInfo.civInfo.getHappiness() >= 0)
stats.science += 15f
if (policies.contains("Total War") && currentConstruction is BaseUnit && !currentConstruction.unitType.isCivilian() )
if (policies.contains("Total War") && currentConstruction is BaseUnit && !currentConstruction.unitType.isCivilian())
stats.production += 15f
if (policies.contains("Aristocracy")
&& currentConstruction is Building
@ -319,10 +325,10 @@ class CityStats {
fun isConnectedToCapital(roadType: RoadStatus): Boolean {
if (cityInfo.civInfo.cities.count() < 2) return false// first city!
if(roadType==RoadStatus.Road) return cityInfo.isConnectedToCapital() // this transient is not applicable to connection via railroad.
if (roadType == RoadStatus.Road) return cityInfo.isConnectedToCapital() // this transient is not applicable to connection via railroad.
val capitalTile = cityInfo.civInfo.getCapital().getCenterTile()
val bfs = BFS(capitalTile){it.roadStatus == roadType}
val bfs = BFS(capitalTile) { it.roadStatus == roadType }
val cityTile = cityInfo.getCenterTile()
bfs.stepUntilDestination(cityTile)
@ -330,12 +336,14 @@ class CityStats {
}
//endregion
fun updateBaseStatList(){
fun updateBaseStatList() {
val newBaseStatList = LinkedHashMap<String, Stats>() // we don't edit the existing baseStatList directly, in order to avoid concurrency exceptions
val civInfo = cityInfo.civInfo
newBaseStatList["Population"] = Stats().add(Stat.Science, cityInfo.population.population.toFloat())
.add(Stat.Production, cityInfo.population.getFreePopulation().toFloat())
newBaseStatList["Population"] = Stats().apply {
science = cityInfo.population.population.toFloat()
production = cityInfo.population.getFreePopulation().toFloat()
}
newBaseStatList["Tile yields"] = getStatsFromTiles()
newBaseStatList["Specialists"] = getStatsFromSpecialists(cityInfo.population.specialists, civInfo.policies.adoptedPolicies)
newBaseStatList["Trade routes"] = getStatsFromTradeRoute()
@ -347,6 +355,7 @@ class CityStats {
baseStatList = newBaseStatList
}
fun updateStatPercentBonusList(){
val newStatPercentBonusList = LinkedHashMap<String,Stats>()
newStatPercentBonusList["Golden Age"]=getStatPercentBonusesFromGoldenAge(cityInfo.civInfo.goldenAges.isGoldenAge())

View file

@ -308,7 +308,7 @@ class NewGameScreen: PickerScreen(){
override fun render(delta: Float) {
if(newGame!=null){
game.gameInfo=newGame!!
game.worldScreen = WorldScreen()
game.worldScreen = WorldScreen(newGame!!.currentPlayerCiv)
game.setWorldScreen()
}
super.render(delta)

View file

@ -271,14 +271,14 @@ open class TileGroup(var tileInfo: TileInfo, var tileSetStrings:TileSetStrings)
val civColor = tileInfo.getOwner()!!.getNation().getColor()
for (neighbor in tileInfo.neighbors) {
val neigborOwner = neighbor.getOwner()
if (neigborOwner == tileOwner && borderImages.containsKey(neighbor)) // the neighbor used to not belong to us, but now it's ours
val neighborOwner = neighbor.getOwner()
if (neighborOwner == tileOwner && borderImages.containsKey(neighbor)) // the neighbor used to not belong to us, but now it's ours
{
for (image in borderImages[neighbor]!!)
image.remove()
borderImages.remove(neighbor)
}
if (neigborOwner != tileOwner && !borderImages.containsKey(neighbor)) { // there should be a border here but there isn't
if (neighborOwner != tileOwner && !borderImages.containsKey(neighbor)) { // there should be a border here but there isn't
val relativeHexPosition = tileInfo.position.cpy().sub(neighbor.position)
val relativeWorldPosition = HexMath().hex2WorldCoords(relativeHexPosition)

View file

@ -16,7 +16,7 @@ class PlayerReadyScreen(currentPlayerCiv: CivilizationInfo) : CameraStageBaseScr
.setFontColor(currentPlayerCiv.getNation().getSecondaryColor()))
table.onClick {
UnCivGame.Current.worldScreen = WorldScreen().apply {
UnCivGame.Current.worldScreen = WorldScreen(currentPlayerCiv).apply {
shouldUpdate = true
}
UnCivGame.Current.setWorldScreen()

View file

@ -27,9 +27,8 @@ import com.unciv.ui.worldscreen.bottombar.BattleTable
import com.unciv.ui.worldscreen.bottombar.WorldScreenBottomBar
import com.unciv.ui.worldscreen.unit.UnitActionsTable
class WorldScreen : CameraStageBaseScreen() {
class WorldScreen(val currentPlayerCiv:CivilizationInfo) : CameraStageBaseScreen() {
val gameInfo = game.gameInfo
internal val currentPlayerCiv: CivilizationInfo = gameInfo.getCurrentPlayerCivilization()
val tileMapHolder: TileMapHolder = TileMapHolder(this, gameInfo.tileMap)
val minimapWrapper = MinimapHolder(tileMapHolder)
@ -319,7 +318,7 @@ class WorldScreen : CameraStageBaseScreen() {
override fun resize(width: Int, height: Int) {
if (stage.viewport.screenWidth != width || stage.viewport.screenHeight != height) {
super.resize(width, height)
game.worldScreen = WorldScreen() // start over.
game.worldScreen = WorldScreen(currentPlayerCiv) // start over.
game.setWorldScreen()
}
}

View file

@ -28,7 +28,7 @@ class Language(val language:String){
}
}
class WorldScreenOptionsTable(screen:WorldScreen) : PopupTable(screen){
class WorldScreenOptionsTable(val worldScreen:WorldScreen) : PopupTable(worldScreen){
var selectedLanguage: String = "English"
init {
@ -142,7 +142,7 @@ class WorldScreenOptionsTable(screen:WorldScreen) : PopupTable(screen){
override fun changed(event: ChangeEvent?, actor: Actor?) {
UnCivGame.Current.settings.resolution = resolutionSelectBox.selected
UnCivGame.Current.settings.save()
UnCivGame.Current.worldScreen = WorldScreen()
UnCivGame.Current.worldScreen = WorldScreen(worldScreen.currentPlayerCiv)
UnCivGame.Current.setWorldScreen()
WorldScreenOptionsTable(UnCivGame.Current.worldScreen)
}
@ -165,7 +165,7 @@ class WorldScreenOptionsTable(screen:WorldScreen) : PopupTable(screen){
override fun changed(event: ChangeEvent?, actor: Actor?) {
UnCivGame.Current.settings.tileSet = tileSetSelectBox.selected
UnCivGame.Current.settings.save()
UnCivGame.Current.worldScreen = WorldScreen()
UnCivGame.Current.worldScreen = WorldScreen(worldScreen.currentPlayerCiv)
UnCivGame.Current.setWorldScreen()
WorldScreenOptionsTable(UnCivGame.Current.worldScreen)
}
@ -248,7 +248,7 @@ class WorldScreenOptionsTable(screen:WorldScreen) : PopupTable(screen){
CameraStageBaseScreen.resetFonts()
UnCivGame.Current.worldScreen = WorldScreen()
UnCivGame.Current.worldScreen = WorldScreen(worldScreen.currentPlayerCiv)
UnCivGame.Current.setWorldScreen()
WorldScreenOptionsTable(UnCivGame.Current.worldScreen)
}