2018-05-29 19:01:22 +00:00
package com.unciv
2018-07-20 12:58:03 +00:00
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
2018-08-24 09:28:27 +00:00
import com.unciv.ui.NewGameScreen
2018-05-29 19:01:22 +00:00
import com.unciv.ui.utils.getRandom
class GameStarter ( ) {
2018-08-24 09:28:27 +00:00
fun startNewGame ( newGameParameters : NewGameScreen . NewGameParameters ) : GameInfo {
2018-05-29 19:01:22 +00:00
val gameInfo = GameInfo ( )
2018-10-12 11:19:05 +00:00
gameInfo . tileMap = TileMap ( newGameParameters . mapRadius , newGameParameters . mapType )
2018-05-31 14:23:46 +00:00
gameInfo . tileMap . gameInfo = gameInfo // need to set this transient before placing units in the map
2018-07-18 15:45:37 +00:00
2018-05-29 19:01:22 +00:00
2018-07-20 12:58:03 +00:00
fun vectorIsWithinNTilesOfEdge ( vector : Vector2 , n : Int ) : Boolean {
2018-08-24 09:28:27 +00:00
return vector . x < newGameParameters . mapRadius - n
&& vector . x > n - newGameParameters . mapRadius
&& vector . y < newGameParameters . mapRadius - n
&& vector . y > n - newGameParameters . mapRadius
2018-07-20 12:58:03 +00:00
}
2018-07-26 20:16:56 +00:00
val distanceAroundStartingPointNoOneElseWillStartIn = 5
2018-10-11 20:05:49 +00:00
val freeTiles = gameInfo . tileMap . values
2018-10-28 12:33:42 +00:00
. filter { it . isLand ( ) && vectorIsWithinNTilesOfEdge ( it . position , 3 ) }
2018-10-11 20:05:49 +00:00
. toMutableList ( )
2018-07-20 12:58:03 +00:00
val playerPosition = freeTiles . getRandom ( ) . position
2018-11-17 19:02:42 +00:00
val playerCiv = CivilizationInfo ( newGameParameters . nation )
2018-08-24 09:28:27 +00:00
playerCiv . difficulty = newGameParameters . difficulty
2018-07-25 19:56:25 +00:00
gameInfo . civilizations . add ( playerCiv ) // first one is player civ
2018-07-18 15:45:37 +00:00
2018-07-27 07:03:44 +00:00
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
2018-08-24 09:28:27 +00:00
for ( nationName in GameBasics . Nations . keys . filterNot { it == " Barbarians " || it == newGameParameters . nation } . shuffled ( )
. take ( newGameParameters . numberOfEnemies ) ) {
2018-11-17 19:02:42 +00:00
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-08-22 10:30:37 +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!
2018-08-22 10:30:37 +00:00
for ( civ in gameInfo . civilizations . toList ( ) . filter { ! it . isBarbarianCivilization ( ) } ) {
2018-08-24 09:28:27 +00:00
if ( freeTiles . isEmpty ( ) ) {
gameInfo . civilizations . remove ( civ )
continue
} // we can't add any more civs.
2018-08-22 10:30:37 +00:00
val startingLocation = freeTiles . toList ( ) . getRandom ( ) . position
civ . placeUnitNearTile ( startingLocation , " Settler " )
2018-09-05 14:17:30 +00:00
civ . placeUnitNearTile ( startingLocation , " Warrior " )
2018-09-17 05:10:51 +00:00
civ . placeUnitNearTile ( startingLocation , " Scout " )
2018-08-22 10:30:37 +00:00
freeTiles . removeAll ( gameInfo . tileMap . getTilesInDistance ( startingLocation , distanceAroundStartingPointNoOneElseWillStartIn ) )
}
2018-05-29 19:01:22 +00:00
return gameInfo
}
}