Resolved #1114 - submarines cannot attack from coast to lake and vice versa

This commit is contained in:
Yair Morgenstern 2019-09-24 17:26:19 +03:00
parent f6009c43ca
commit 40c76b7752
4 changed files with 100 additions and 92 deletions

View File

@ -12,6 +12,7 @@ class Constants{
const val hill = "Hill"
const val coast = "Coast"
const val plains = "Plains"
const val lakes = "Lakes"
const val barbarianEncampment = "Barbarian encampment"
const val ancientRuins = "Ancient ruins"

View File

@ -167,7 +167,13 @@ class UnitAutomation{
if (tile.isWater) return false // can't attack water units while embarked, only land
if (combatant.isRanged()) return false
}
if (tile.isLand && combatant.unit.hasUnique("Can only attack water")) return false
if (combatant.unit.hasUnique("Can only attack water")) {
if (tile.isLand) return false
// trying to attack lake-to-coast or vice versa
if ((tile.baseTerrain == Constants.lakes) != (combatant.getTile().baseTerrain == Constants.lakes))
return false
}
}
val tileCombatant = Battle(combatant.getCivInfo().gameInfo).getMapCombatantOfTile(tile)

View File

@ -33,7 +33,7 @@ class Battle(val gameInfo:GameInfo) {
val attackedTile = defender.getTile()
if(attacker is MapUnitCombatant && attacker.getUnitType().isAirUnit()){
intercept(attacker,defender)
tryInterceptAirAttack(attacker,defender)
if(attacker.isDefeated()) return
}
@ -133,8 +133,7 @@ class Battle(val gameInfo:GameInfo) {
// and if it's an air unit, it only has 1 movement anyway, so...
if (!attacker.getUnitType().isAirUnit() && !(attacker.getUnitType().isMelee() && defender.isDefeated()))
unit.useMovementPoints(1f)
}
else unit.currentMovement = 0f
} else unit.currentMovement = 0f
unit.attacksThisTurn += 1
if (unit.isFortified() || unit.action == Constants.unitActionSleep)
attacker.unit.action = null // but not, for instance, if it's Set Up - then it should definitely keep the action!
@ -155,22 +154,13 @@ class Battle(val gameInfo:GameInfo) {
addXp(attacker, 5, defender)
addXp(defender, 4, attacker)
}
}
else{ // ranged attack
} else { // ranged attack
addXp(attacker, 2, defender)
addXp(defender, 2, attacker)
}
// Add culture when defeating a barbarian when Honor policy is adopted (can be either attacker or defender!)
fun tryGetCultureFromHonor(civUnit:ICombatant, barbarianUnit:ICombatant){
if(barbarianUnit.isDefeated() && barbarianUnit is MapUnitCombatant
&& barbarianUnit.getCivInfo().isBarbarian()
&& civUnit.getCivInfo().policies.isAdopted("Honor"))
civUnit.getCivInfo().policies.storedCulture +=
max(barbarianUnit.unit.baseUnit.strength,barbarianUnit.unit.baseUnit.rangedStrength)
}
tryGetCultureFromHonor(attacker, defender)
tryGetCultureFromHonor(defender, attacker)
@ -178,12 +168,21 @@ class Battle(val gameInfo:GameInfo) {
&& attacker.getCivInfo().policies.isAdopted("Honor Complete"))
attacker.getCivInfo().gold += defender.unit.baseUnit.getProductionCost(attacker.getCivInfo()) / 10
if(attacker is MapUnitCombatant && attacker.unit.action!=null && attacker.unit.action!!.startsWith("moveTo"))
if (attacker is MapUnitCombatant && attacker.unit.action != null
&& attacker.unit.action!!.startsWith("moveTo"))
attacker.unit.action = null
}
private fun tryGetCultureFromHonor(civUnit:ICombatant, barbarianUnit:ICombatant){
if(barbarianUnit.isDefeated() && barbarianUnit is MapUnitCombatant
&& barbarianUnit.getCivInfo().isBarbarian()
&& civUnit.getCivInfo().policies.isAdopted("Honor"))
civUnit.getCivInfo().policies.storedCulture +=
max(barbarianUnit.unit.baseUnit.strength,barbarianUnit.unit.baseUnit.rangedStrength)
}
// XP!
fun addXp(thisCombatant:ICombatant, amount:Int, otherCombatant:ICombatant){
private fun addXp(thisCombatant:ICombatant, amount:Int, otherCombatant:ICombatant){
if(thisCombatant !is MapUnitCombatant) return
if(thisCombatant.unit.promotions.totalXpProduced() >= 30 && otherCombatant.getCivInfo().isBarbarian())
return
@ -211,7 +210,6 @@ class Battle(val gameInfo:GameInfo) {
for(airUnit in airUnits.toList()) airUnit.destroy()
}
if (attacker.getCivInfo().isPlayerCivilization())
attackerCiv.popupAlerts.add(PopupAlert(AlertType.CityConquered, city.name))
else city.puppetCity(attacker.getCivInfo())
@ -224,12 +222,13 @@ class Battle(val gameInfo:GameInfo) {
return null
}
fun captureCivilianUnit(attacker: ICombatant, defender: ICombatant){
private fun captureCivilianUnit(attacker: ICombatant, defender: ICombatant){
// barbarians don't capture civilians, City-states don't capture settlers
if(attacker.getCivInfo().isBarbarian()
|| (attacker.getCivInfo().isCityState() && defender.getName()==Constants.settler)){
defender.takeDamage(100)
return
} // barbarians don't capture civilians!
}
if (defender.getCivInfo().isDefeated()) {//Last settler captured
defender.getCivInfo().destroy()
@ -245,7 +244,7 @@ class Battle(val gameInfo:GameInfo) {
capturedUnit.updateViewableTiles()
}
fun intercept(attacker:MapUnitCombatant, defender: ICombatant){
private fun tryInterceptAirAttack(attacker:MapUnitCombatant, defender: ICombatant) {
val attackedTile = defender.getTile()
for (interceptor in defender.getCivInfo().getCivUnits().filter { it.canIntercept(attackedTile) }) {
if (Random().nextFloat() > 100f / interceptor.interceptChance()) continue
@ -261,15 +260,18 @@ class Battle(val gameInfo:GameInfo) {
val interceptorName = interceptor.name
if (attacker.isDefeated()) {
attacker.getCivInfo().addNotification("Our [$attackerName] was destroyed by an intercepting [$interceptorName]",
attacker.getCivInfo()
.addNotification("Our [$attackerName] was destroyed by an intercepting [$interceptorName]",
Color.RED)
defender.getCivInfo().addNotification("Our [$interceptorName] intercepted and destroyed an enemy [$attackerName]",
defender.getCivInfo()
.addNotification("Our [$interceptorName] intercepted and destroyed an enemy [$attackerName]",
interceptor.currentTile.position, Color.RED)
}
else{
attacker.getCivInfo().addNotification("Our [$attackerName] was attacked by an intercepting [$interceptorName]",
} else {
attacker.getCivInfo()
.addNotification("Our [$attackerName] was attacked by an intercepting [$interceptorName]",
Color.RED)
defender.getCivInfo().addNotification("Our [$interceptorName] intercepted and attacked an enemy [$attackerName]",
defender.getCivInfo()
.addNotification("Our [$interceptorName] intercepted and attacked an enemy [$attackerName]",
interceptor.currentTile.position, Color.RED)
}
return

View File

@ -2,8 +2,6 @@ package com.unciv.logic.map
import com.badlogic.gdx.math.Vector2
import com.unciv.Constants
import com.unciv.Constants.Companion.mountain
import com.unciv.Constants.Companion.ocean
import com.unciv.logic.HexMath
import com.unciv.models.gamebasics.GameBasics
import com.unciv.models.gamebasics.tile.ResourceType
@ -123,7 +121,7 @@ class CelluarAutomataRandomMapGenerator(): SeedRandomMapGenerator() {
val tile=TileInfo()
tile.position=vector
if (type == TerrainType.Land) tile.baseTerrain = ""
else tile.baseTerrain = ocean
else tile.baseTerrain = Constants.ocean
return tile
}
@ -152,7 +150,7 @@ class CelluarAutomataRandomMapGenerator(): SeedRandomMapGenerator() {
if (tilesInArea.size <= 10) {
for (vector in tilesInArea) {
val tile = map[vector.toString()]!!
tile.baseTerrain = "Lakes"
tile.baseTerrain = Constants.lakes
tile.setTransients()
}
}
@ -160,7 +158,7 @@ class CelluarAutomataRandomMapGenerator(): SeedRandomMapGenerator() {
}
//Coasts
for (tile in map.values.filter { it.baseTerrain == ocean }) {
for (tile in map.values.filter { it.baseTerrain == Constants.ocean }) {
if (HexMath().getVectorsInDistance(tile.position,2).any { hasLandTile(map,it) }) {
tile.baseTerrain = Constants.coast
tile.setTransients()
@ -170,7 +168,7 @@ class CelluarAutomataRandomMapGenerator(): SeedRandomMapGenerator() {
override fun randomizeTile(tileInfo: TileInfo, map: HashMap<String, TileInfo>){
if(tileInfo.getBaseTerrain().type==TerrainType.Land && Math.random()<0.05f){
tileInfo.baseTerrain = mountain
tileInfo.baseTerrain = Constants.mountain
tileInfo.setTransients()
}
addRandomTerrainFeature(tileInfo)
@ -185,8 +183,8 @@ class CelluarAutomataRandomMapGenerator(): SeedRandomMapGenerator() {
fun divideIntoAreas2(averageTilesPerArea: Int, waterPercent: Float, distance: Int, map: HashMap<Vector2, TileInfo>) {
val areas = ArrayList<Area>()
val terrains = GameBasics.Terrains.values.filter { it.type === TerrainType.Land && it.name != "Lakes"
&& it.name != mountain}
val terrains = GameBasics.Terrains.values.filter { it.type === TerrainType.Land && it.name != Constants.lakes
&& it.name != Constants.mountain}
while(map.values.any { it.baseTerrain=="" }) // the world could be split into lots off tiny islands, and every island deserves land types
{
@ -196,7 +194,7 @@ class CelluarAutomataRandomMapGenerator(): SeedRandomMapGenerator() {
for (i in 0 until numberOfSeeds) {
var terrain = if (Math.random() > waterPercent) terrains.random().name
else ocean
else Constants.ocean
val tile = emptyTiles.random()
//change grassland to desert or tundra based on y
@ -204,7 +202,7 @@ class CelluarAutomataRandomMapGenerator(): SeedRandomMapGenerator() {
if (terrain == "Grassland" || terrain == "Tundra")
terrain = "Desert"
} else if (abs(getLatitude(tile.position)) > maxLatitude * 0.7) {
if (terrain == "Grassland" || terrain == Constants.plains || terrain == "Desert" || terrain == ocean) {
if (terrain == "Grassland" || terrain == Constants.plains || terrain == "Desert" || terrain == Constants.ocean) {
terrain = "Tundra"
}
} else {
@ -263,9 +261,9 @@ class PerlinNoiseRandomMapGenerator:SeedRandomMapGenerator(){
+ Perlin.noise(vector.x*ratio*2,vector.y*ratio*2,mapRandomSeed)/2
+ Perlin.noise(vector.x*ratio*4,vector.y*ratio*4,mapRandomSeed)/4
when {
height>0.8 -> tile.baseTerrain = mountain
height>0.8 -> tile.baseTerrain = Constants.mountain
height>0 -> tile.baseTerrain = "" // we'll leave this to the area division
else -> tile.baseTerrain = ocean
else -> tile.baseTerrain = Constants.ocean
}
return tile
}
@ -297,12 +295,12 @@ class AlexanderRandomMapGenerator:RandomMapGenerator(){
if(map[currentSpark]!!.baseTerrain==grassland){
for(tile in emptyTilesAroundSpark){
if(Math.random()<landExpansionChance) map[tile]=TileInfo().apply { baseTerrain=grassland }
else map[tile]=TileInfo().apply { baseTerrain=ocean }
else map[tile]=TileInfo().apply { baseTerrain=Constants.ocean }
}
}
else{
for(tile in emptyTilesAroundSpark)
map[tile]=TileInfo().apply { baseTerrain=ocean }
map[tile]=TileInfo().apply { baseTerrain=Constants.ocean }
}
sparkList.remove(currentSpark)
sparkList.addAll(emptyTilesAroundSpark)
@ -311,7 +309,7 @@ class AlexanderRandomMapGenerator:RandomMapGenerator(){
val newmap = HashMap<String,TileInfo>()
for(entry in map){
entry.value!!.position = entry.key
if(entry.value!!.baseTerrain==ocean
if(entry.value!!.baseTerrain==Constants.ocean
&& HexMath().getAdjacentVectors(entry.key).all { !map.containsKey(it) || map[it]!!.baseTerrain==grassland })
entry.value!!.baseTerrain=grassland
@ -364,7 +362,8 @@ open class SeedRandomMapGenerator : RandomMapGenerator() {
open fun divideIntoAreas(averageTilesPerArea: Int, waterPercent: Float, map: HashMap<Vector2, TileInfo>) {
val areas = ArrayList<Area>()
val terrains = GameBasics.Terrains.values.filter { it.type === TerrainType.Land && it.name != "Lakes" && it.name != mountain }
val terrains = GameBasics.Terrains.values
.filter { it.type === TerrainType.Land && it.name != Constants.lakes && it.name != Constants.mountain }
while(map.values.any { it.baseTerrain=="" }) // the world could be split into lots off tiny islands, and every island deserves land types
{
@ -373,7 +372,7 @@ open class SeedRandomMapGenerator : RandomMapGenerator() {
for (i in 0 until numberOfSeeds) {
val terrain = if (Math.random() > waterPercent) terrains.random().name
else ocean
else Constants.ocean
val area = Area(terrain)
val tile = emptyTiles.random()
emptyTiles -= tile
@ -386,10 +385,10 @@ open class SeedRandomMapGenerator : RandomMapGenerator() {
}
for (area in areas.filter { it.terrain == ocean && it.locations.size <= 10 }) {
for (area in areas.filter { it.terrain == Constants.ocean && it.locations.size <= 10 }) {
// areas with 10 or less tiles are lakes.
for (location in area.locations)
map[location]!!.baseTerrain = "Lakes"
map[location]!!.baseTerrain = Constants.lakes
}
}
@ -439,7 +438,7 @@ open class RandomMapGenerator {
tileInfo.position = position
val terrains = GameBasics.Terrains.values
val baseTerrain = terrains.filter { it.type === TerrainType.Land && it.name != "Lakes" }.random()
val baseTerrain = terrains.filter { it.type === TerrainType.Land }.random()
tileInfo.baseTerrain = baseTerrain.name
addRandomTerrainFeature(tileInfo)
@ -498,7 +497,7 @@ open class RandomMapGenerator {
}
open fun setWaterTiles(map: HashMap<String, TileInfo>) {
for (tile in map.values.filter { it.baseTerrain == ocean }) {
for (tile in map.values.filter { it.baseTerrain == Constants.ocean }) {
if (HexMath().getVectorsInDistance(tile.position,2).any { hasLandTile(map,it) }) {
tile.baseTerrain = Constants.coast
tile.setTransients()
@ -508,12 +507,12 @@ open class RandomMapGenerator {
open fun randomizeTile(tileInfo: TileInfo, map: HashMap<String, TileInfo>){
if(tileInfo.getBaseTerrain().type==TerrainType.Land && Math.random()<0.05f){
tileInfo.baseTerrain = mountain
tileInfo.baseTerrain = Constants.mountain
tileInfo.setTransients()
}
if(tileInfo.getBaseTerrain().type==TerrainType.Land && Math.random()<0.05f
&& HexMath().getVectorsInDistance(tileInfo.position,1).all { hasLandTile(map,it) }){
tileInfo.baseTerrain = "Lakes"
tileInfo.baseTerrain = Constants.lakes
tileInfo.setTransients()
}
addRandomTerrainFeature(tileInfo)