Fix: starting units being able to spawn on Ancient Ruins/Barbarian encampments (#1691)

* Fix: starting units being able to spawn on Ancient Ruins/Barbarian encampments

* Added doc and commentary
This commit is contained in:
lyrjie 2020-01-16 13:06:43 +03:00 committed by Yair Morgenstern
parent 90abfda3a4
commit d3e88906d6
3 changed files with 20 additions and 7 deletions

View file

@ -111,13 +111,13 @@ class GameStarter{
for (civ in gameInfo.civilizations.filter { !it.isBarbarian() }) {
val startingLocation = startingLocations[civ]!!
civ.placeUnitNearTile(startingLocation.position, Constants.settler)
civ.placeUnitNearTile(startingLocation.position, getWarriorEquivalent(civ))
civ.placeUnitNearTile(startingLocation.position, Constants.settler, removeImprovement = true)
civ.placeUnitNearTile(startingLocation.position, getWarriorEquivalent(civ), removeImprovement = true)
if (!civ.isPlayerCivilization() && civ.isMajorCiv()) {
for (unit in gameInfo.getDifficulty().aiFreeUnits) {
val unitToAdd = if (unit == "Warrior") getWarriorEquivalent(civ) else unit
civ.placeUnitNearTile(startingLocation.position, unitToAdd)
civ.placeUnitNearTile(startingLocation.position, unitToAdd, removeImprovement = true)
}
}
}
@ -178,7 +178,6 @@ class GameStarter{
}
if(startingLocations.size < civs.size) continue // let's try again with less minimum distance!
for(tile in tilesWithStartingLocations) tile.improvement=null // get rid of the starting location improvements
return startingLocations
}
throw Exception("Didn't manage to get starting tiles even with distance of 1?")

View file

@ -462,8 +462,8 @@ class CivilizationInfo {
addNotification("A [$greatPerson] has been born in [${city.name}]!", city.location, Color.GOLD)
}
fun placeUnitNearTile(location: Vector2, unitName: String): MapUnit? {
return gameInfo.tileMap.placeUnitNearTile(location, unitName, this)
fun placeUnitNearTile(location: Vector2, unitName: String, removeImprovement: Boolean = false): MapUnit? {
return gameInfo.tileMap.placeUnitNearTile(location, unitName, this, removeImprovement = removeImprovement)
}
fun addCity(location: Vector2) {

View file

@ -109,7 +109,19 @@ class TileMap {
return tilesToReturn
}
fun placeUnitNearTile(position: Vector2, unitName: String, civInfo: CivilizationInfo): MapUnit? {
/** Tries to place the [unitName] into the [TileInfo] closest to the given the [position]
*
* @param civInfo civilization to assign unit to
* @param removeImprovement True if the improvement of [TileInfo] unit is placed into should be deleted
*
* @return created [MapUnit] or null if no suitable location was found
* */
fun placeUnitNearTile(
position: Vector2,
unitName: String,
civInfo: CivilizationInfo,
removeImprovement: Boolean = false
): MapUnit? {
val unit = gameInfo.ruleSet.units[unitName]!!.getMapUnit(gameInfo.ruleSet)
fun isTileMovePotential(tileInfo:TileInfo): Boolean {
@ -129,6 +141,8 @@ class TileMap {
val unitToPlaceTile = viableTilesToPlaceUnitIn.firstOrNull { unit.movement.canMoveTo(it) }
if(unitToPlaceTile!=null) {
// Remove the tile improvement, e.g. when placing the starter units (so they don't spawn on ruins/encampments)
if (removeImprovement) unitToPlaceTile.improvement = null
// only once we know the unit can be placed do we add it to the civ's unit list
unit.putInTile(unitToPlaceTile)
unit.currentMovement = unit.getMaxMovement().toFloat()