Can now play with multiple human players!

Need to find a way for all human players to choose their civs in a simple manner
This commit is contained in:
Yair Morgenstern
2019-01-11 09:58:32 +02:00
parent 8bf84393fc
commit 401aa942eb
3 changed files with 45 additions and 29 deletions

View File

@ -31,20 +31,23 @@ class GameStarter{
val startingLocations = getStartingLocations( val startingLocations = getStartingLocations(
newGameParameters.numberOfEnemies+newGameParameters.humanPlayers, gameInfo.tileMap) newGameParameters.numberOfEnemies+newGameParameters.humanPlayers, gameInfo.tileMap)
val availableCivNames = Stack<String>()
availableCivNames.addAll(GameBasics.Nations.keys.shuffled())
availableCivNames.remove(newGameParameters.nation)
for(i in 1..newGameParameters.humanPlayers) { for(i in 1..newGameParameters.humanPlayers) {
val playerCiv = CivilizationInfo(newGameParameters.nation) val playerCiv =
if(i==1) CivilizationInfo(newGameParameters.nation)
else CivilizationInfo(availableCivNames.pop())
gameInfo.difficulty = newGameParameters.difficulty gameInfo.difficulty = newGameParameters.difficulty
playerCiv.playerType = PlayerType.Human playerCiv.playerType = PlayerType.Human
gameInfo.civilizations.add(playerCiv) gameInfo.civilizations.add(playerCiv)
} }
val barbarianCivilization = CivilizationInfo() val barbarianCivilization = CivilizationInfo("Barbarians")
barbarianCivilization.civName = "Barbarians"
gameInfo.civilizations.add(barbarianCivilization)// second is barbarian civ gameInfo.civilizations.add(barbarianCivilization)// second is barbarian civ
for (nationName in GameBasics.Nations.keys.filterNot { it=="Barbarians" || it==newGameParameters.nation }.shuffled() for (nationName in availableCivNames.take(newGameParameters.numberOfEnemies)) {
.take(newGameParameters.numberOfEnemies)) {
val civ = CivilizationInfo(nationName) val civ = CivilizationInfo(nationName)
gameInfo.civilizations.add(civ) gameInfo.civilizations.add(civ)
} }

View File

@ -37,46 +37,52 @@ class GameInfo {
//endregion //endregion
fun nextTurn() { fun nextTurn() {
val currentPlayer = getCurrentPlayerCivilization() val previousHumanPlayer = getCurrentPlayerCivilization()
var thisPlayer = previousHumanPlayer // not calling is currentPlayer because that's alreay taken and I can't think of a better name
var currentPlayerIndex = civilizations.indexOf(thisPlayer)
for (civInfo in civilizations) { fun switchTurn(){
if (civInfo.tech.techsToResearch.isEmpty()) { // should belong in automation? yes/no? thisPlayer.endTurn()
val researchableTechs = GameBasics.Technologies.values currentPlayerIndex = (currentPlayerIndex+1) % civilizations.size
.filter { !civInfo.tech.isResearched(it.name) && civInfo.tech.canBeResearched(it.name) } if(currentPlayerIndex==0){
civInfo.tech.techsToResearch.add(researchableTechs.minBy { it.cost }!!.name) turns++
if (turns % 10 == 0) { // every 10 turns add a barbarian in a random place
placeBarbarianUnit(null)
}
} }
civInfo.endTurn() thisPlayer = civilizations[currentPlayerIndex]
thisPlayer.startTurn()
} }
// We need to update the stats after ALL the cities are done updating because switchTurn()
// maybe one of them has a wonder that affects the stats of all the rest of the cities
for (civInfo in civilizations.filterNot { it == currentPlayer || (it.isDefeated() && !it.isBarbarianCivilization()) }) { while(thisPlayer.playerType==PlayerType.AI){
civInfo.startTurn() NextTurnAutomation().automateCivMoves(thisPlayer)
NextTurnAutomation().automateCivMoves(civInfo) 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()
} }
if (turns % 10 == 0) { // every 10 turns add a barbarian in a random place currentPlayer=thisPlayer.civName
placeBarbarianUnit(null)
}
// Start our turn immediately before the player can made decisions - affects whether our units can commit automated actions and then be attacked immediately etc. // Start our turn immediately before the player can made decisions - affects whether our units can commit automated actions and then be attacked immediately etc.
currentPlayer.startTurn() val enemyUnitsCloseToTerritory = thisPlayer.viewableTiles
val enemyUnitsCloseToTerritory = currentPlayer.viewableTiles
.filter { .filter {
it.militaryUnit != null && it.militaryUnit!!.civInfo != currentPlayer it.militaryUnit != null && it.militaryUnit!!.civInfo != thisPlayer
&& currentPlayer.isAtWarWith(it.militaryUnit!!.civInfo) && thisPlayer.isAtWarWith(it.militaryUnit!!.civInfo)
&& (it.getOwner() == currentPlayer || it.neighbors.any { neighbor -> neighbor.getOwner() == currentPlayer }) && (it.getOwner() == thisPlayer || it.neighbors.any { neighbor -> neighbor.getOwner() == thisPlayer })
} }
for (enemyUnitTile in enemyUnitsCloseToTerritory) { for (enemyUnitTile in enemyUnitsCloseToTerritory) {
val inOrNear = if (enemyUnitTile.getOwner() == currentPlayer) "in" else "near" val inOrNear = if (enemyUnitTile.getOwner() == thisPlayer) "in" else "near"
val unitName = enemyUnitTile.militaryUnit!!.name val unitName = enemyUnitTile.militaryUnit!!.name
currentPlayer.addNotification("An enemy [$unitName] was spotted $inOrNear our territory", enemyUnitTile.position, Color.RED) thisPlayer.addNotification("An enemy [$unitName] was spotted $inOrNear our territory", enemyUnitTile.position, Color.RED)
} }
turns++
} }
fun placeBarbarianUnit(tileToPlace: TileInfo?) { fun placeBarbarianUnit(tileToPlace: TileInfo?) {

View File

@ -264,6 +264,13 @@ class WorldScreen : CameraStageBaseScreen() {
var shouldUpdate=false var shouldUpdate=false
override fun render(delta: Float) { override fun render(delta: Float) {
if(shouldUpdate){ // This is so that updates happen in the MAIN THREAD, where there is a GL Context, if(shouldUpdate){ // This is so that updates happen in the MAIN THREAD, where there is a GL Context,
if(currentPlayerCiv!=gameInfo.getCurrentPlayerCivilization()){
UnCivGame.Current.worldScreen= WorldScreen().apply {
shouldUpdate=true
}
UnCivGame.Current.setWorldScreen()
}
// otherwise images will not load properly! // otherwise images will not load properly!
update() update()