Unciv/core/src/com/unciv/GameStarter.kt

70 lines
3.1 KiB
Kotlin
Raw Normal View History

2018-05-29 19:01:22 +00:00
package com.unciv
import com.badlogic.gdx.math.Vector2
2018-05-29 19:01:22 +00:00
import com.unciv.logic.GameInfo
import com.unciv.logic.civilization.CivilizationInfo
import com.unciv.logic.map.TileMap
import com.unciv.models.gamebasics.GameBasics
import com.unciv.ui.NewGameScreen
2018-05-29 19:01:22 +00:00
import com.unciv.ui.utils.getRandom
class GameStarter(){
fun startNewGame(newGameParameters: NewGameScreen.NewGameParameters): GameInfo {
2018-05-29 19:01:22 +00:00
val gameInfo = GameInfo()
gameInfo.tileMap = TileMap(newGameParameters.mapRadius, newGameParameters.mapType)
gameInfo.tileMap.gameInfo = gameInfo // need to set this transient before placing units in the map
2018-05-29 19:01:22 +00:00
fun vectorIsWithinNTilesOfEdge(vector: Vector2,n:Int): Boolean {
return vector.x < newGameParameters.mapRadius-n
&& vector.x > n-newGameParameters.mapRadius
&& vector.y < newGameParameters.mapRadius-n
&& vector.y > n-newGameParameters.mapRadius
}
val distanceAroundStartingPointNoOneElseWillStartIn = 5
val freeTiles = gameInfo.tileMap.values
2018-10-28 12:33:42 +00:00
.filter { it.isLand() && vectorIsWithinNTilesOfEdge(it.position,3)}
.toMutableList()
val playerPosition = freeTiles.getRandom().position
val playerCiv = CivilizationInfo(newGameParameters.nation)
playerCiv.difficulty=newGameParameters.difficulty
2018-07-25 19:56:25 +00:00
gameInfo.civilizations.add(playerCiv) // first one is player civ
freeTiles.removeAll(gameInfo.tileMap.getTilesInDistance(playerPosition, distanceAroundStartingPointNoOneElseWillStartIn ))
2018-05-29 19:01:22 +00:00
val barbarianCivilization = CivilizationInfo()
gameInfo.civilizations.add(barbarianCivilization)// second is barbarian civ
for (nationName in GameBasics.Nations.keys.filterNot { it=="Barbarians" || it==newGameParameters.nation }.shuffled()
.take(newGameParameters.numberOfEnemies)) {
val civ = CivilizationInfo(nationName)
2018-07-25 19:56:25 +00:00
civ.tech.techsResearched.addAll(playerCiv.getDifficulty().aiFreeTechs)
gameInfo.civilizations.add(civ)
2018-05-29 19:01:22 +00:00
}
2018-05-29 19:01:22 +00:00
barbarianCivilization.civName = "Barbarians"
gameInfo.setTransients() // needs to be before placeBarbarianUnit because it depends on the tilemap having its gameinfo set
2018-08-23 05:43:14 +00:00
// and only now do we add units for everyone, because otherwise both the gameInfo.setTransients() and the placeUnit will both add the unit to the civ's unit list!
for (civ in gameInfo.civilizations.toList().filter { !it.isBarbarianCivilization() }) {
if(freeTiles.isEmpty()){
gameInfo.civilizations.remove(civ)
continue
} // we can't add any more civs.
val startingLocation = freeTiles.toList().getRandom().position
civ.placeUnitNearTile(startingLocation, "Settler")
2018-09-05 14:17:30 +00:00
civ.placeUnitNearTile(startingLocation, "Warrior")
civ.placeUnitNearTile(startingLocation, "Scout")
freeTiles.removeAll(gameInfo.tileMap.getTilesInDistance(startingLocation, distanceAroundStartingPointNoOneElseWillStartIn ))
}
2018-05-29 19:01:22 +00:00
return gameInfo
}
}