Skip to content

Commit

Permalink
Removed redundant "else"s for less indentation and 'happy pathing'
Browse files Browse the repository at this point in the history
  • Loading branch information
yairm210 committed Jan 12, 2020
1 parent 725edc2 commit ca59dc4
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 66 deletions.
44 changes: 21 additions & 23 deletions core/src/com/unciv/logic/automation/NextTurnAutomation.kt
Expand Up @@ -19,32 +19,30 @@ class NextTurnAutomation{

/** Top-level AI turn tasklist */
fun automateCivMoves(civInfo: CivilizationInfo) {
if (civInfo.isBarbarian()) {
BarbarianAutomation(civInfo).automate()
} else {
respondToDemands(civInfo)
respondToTradeRequests(civInfo)

if(civInfo.isMajorCiv()) {
offerPeaceTreaty(civInfo)
exchangeTechs(civInfo)
exchangeLuxuries(civInfo)
issueRequests(civInfo)
adoptPolicy(civInfo)
} else {
getFreeTechForCityStates(civInfo)
}
if (civInfo.isBarbarian()) return BarbarianAutomation(civInfo).automate()

chooseTechToResearch(civInfo)
updateDiplomaticRelationship(civInfo)
declareWar(civInfo)
automateCityBombardment(civInfo)
useGold(civInfo)
automateUnits(civInfo)
reassignWorkedTiles(civInfo)
trainSettler(civInfo)
respondToDemands(civInfo)
respondToTradeRequests(civInfo)

if(civInfo.isMajorCiv()) {
offerPeaceTreaty(civInfo)
exchangeTechs(civInfo)
exchangeLuxuries(civInfo)
issueRequests(civInfo)
adoptPolicy(civInfo)
} else {
getFreeTechForCityStates(civInfo)
}

chooseTechToResearch(civInfo)
updateDiplomaticRelationship(civInfo)
declareWar(civInfo)
automateCityBombardment(civInfo)
useGold(civInfo)
automateUnits(civInfo)
reassignWorkedTiles(civInfo)
trainSettler(civInfo)

civInfo.popupAlerts.clear() // AIs don't care about popups.
}

Expand Down
4 changes: 2 additions & 2 deletions core/src/com/unciv/logic/map/TileInfo.kt
Expand Up @@ -7,8 +7,8 @@ import com.unciv.logic.city.CityInfo
import com.unciv.logic.civilization.CivilizationInfo
import com.unciv.models.ruleset.Ruleset
import com.unciv.models.ruleset.tile.*
import com.unciv.models.translations.tr
import com.unciv.models.stats.Stats
import com.unciv.models.translations.tr
import kotlin.math.abs

open class TileInfo {
Expand Down Expand Up @@ -262,7 +262,7 @@ open class TileInfo {
return resource != null && (getTileResource().revealedBy == null || civInfo.tech.isResearched(getTileResource().revealedBy!!))
}

fun getViewableTiles(distance:Int, ignoreCurrentTileHeight:Boolean = false): MutableList<TileInfo> {
fun getViewableTiles(distance:Int, ignoreCurrentTileHeight:Boolean = false): List<TileInfo> {
return tileMap.getViewableTiles(this.position,distance,ignoreCurrentTileHeight)
}

Expand Down
80 changes: 39 additions & 41 deletions core/src/com/unciv/logic/map/TileMap.kt
Expand Up @@ -150,50 +150,48 @@ class TileMap {
}


fun getViewableTiles(position: Vector2, sightDistance: Int, ignoreCurrentTileHeight: Boolean = false): MutableList<TileInfo> {
if (ignoreCurrentTileHeight) {
return getTilesInDistance(position, sightDistance).toMutableList()
} else {
val viewableTiles = getTilesInDistance(position, 1).toMutableList()
val currentTileHeight = get(position).getHeight()

for (i in 1..sightDistance) { // in each layer,
// This is so we don't use tiles in the same distance to "see over",
// that is to say, the "viewableTiles.contains(it) check will return false for neighbors from the same distance
val tilesToAddInDistanceI = ArrayList<TileInfo>()

for (tile in getTilesAtDistance(position, i)) { // for each tile in that layer,
val targetTileHeight = tile.getHeight()

/*
Okay so, if we're looking at a tile from a to c with b in the middle,
we have several scenarios:
1. a>b - - I can see everything, b does not hide c
2. a==b
2.1 a==b==0, all flat ground, no hiding
2.2 a>0, b>=c - b hides c from view (say I am in a forest/jungle and b is a forest/jungle, or hill)
2.3 a>0, c>b - c is tall enough I can see it over b!
3. a<b
3.1 b>=c - b hides c
3.2 b<c - c is tall enough I can see it over b!
This can all be summed up as "I can see c if a>b || c>b || b==0 "
*/

val containsViewableNeighborThatCanSeeOver = tile.neighbors.any {
val neighborHeight = it.getHeight()
viewableTiles.contains(it) && (
currentTileHeight > neighborHeight // a>b
|| targetTileHeight > neighborHeight // c>b
|| neighborHeight == 0) // b==0
}
if (containsViewableNeighborThatCanSeeOver) tilesToAddInDistanceI.add(tile)
fun getViewableTiles(position: Vector2, sightDistance: Int, ignoreCurrentTileHeight: Boolean = false): List<TileInfo> {
if (ignoreCurrentTileHeight) return getTilesInDistance(position, sightDistance)

val viewableTiles = getTilesInDistance(position, 1).toMutableList()
val currentTileHeight = get(position).getHeight()

for (i in 1..sightDistance) { // in each layer,
// This is so we don't use tiles in the same distance to "see over",
// that is to say, the "viewableTiles.contains(it) check will return false for neighbors from the same distance
val tilesToAddInDistanceI = ArrayList<TileInfo>()

for (tile in getTilesAtDistance(position, i)) { // for each tile in that layer,
val targetTileHeight = tile.getHeight()

/*
Okay so, if we're looking at a tile from a to c with b in the middle,
we have several scenarios:
1. a>b - - I can see everything, b does not hide c
2. a==b
2.1 a==b==0, all flat ground, no hiding
2.2 a>0, b>=c - b hides c from view (say I am in a forest/jungle and b is a forest/jungle, or hill)
2.3 a>0, c>b - c is tall enough I can see it over b!
3. a<b
3.1 b>=c - b hides c
3.2 b<c - c is tall enough I can see it over b!
This can all be summed up as "I can see c if a>b || c>b || b==0 "
*/

val containsViewableNeighborThatCanSeeOver = tile.neighbors.any {
val neighborHeight = it.getHeight()
viewableTiles.contains(it) && (
currentTileHeight > neighborHeight // a>b
|| targetTileHeight > neighborHeight // c>b
|| neighborHeight == 0) // b==0
}
viewableTiles.addAll(tilesToAddInDistanceI)
if (containsViewableNeighborThatCanSeeOver) tilesToAddInDistanceI.add(tile)
}

return viewableTiles
viewableTiles.addAll(tilesToAddInDistanceI)
}

return viewableTiles
}

fun setTransients(ruleset: Ruleset) {
Expand Down

0 comments on commit ca59dc4

Please sign in to comment.