mirror of
https://gitlab.com/cable-mc/cobblemon.git
synced 2026-11-29 11:38:52 -05:00
fix: 1-block tall pokemon have no self-preservation in pastures
This commit is contained in:
1 parent
6d0c516bae
commit
bfb9b28d65
3 files changed
+128
-121
No files matched your search
+35
-25
@@ -20,13 +20,14 @@ import com.cobblemon.mod.common.api.ai.biggerbrain.fixation.EntityFixation
|
||||
import com.cobblemon.mod.common.api.ai.biggerbrain.fixation.LookAndMoveFixation
|
||||
import com.cobblemon.mod.common.api.ai.config.objective.ObjectiveConfig
|
||||
import com.cobblemon.mod.common.api.ai.movement.MovementIntent
|
||||
import com.cobblemon.mod.common.api.ai.movement.PathValidation
|
||||
import com.cobblemon.mod.common.api.ai.movement.TraversalPolicy
|
||||
import com.cobblemon.mod.common.api.molang.MoLangFunctions.asMostSpecificMoLangValue
|
||||
import com.cobblemon.mod.common.util.closestPosition
|
||||
import com.cobblemon.mod.common.entity.ai.OmniPathNavigation
|
||||
import com.cobblemon.mod.common.util.distanceTo
|
||||
import com.cobblemon.mod.common.util.getIsSubmerged
|
||||
import com.cobblemon.mod.common.util.resolveFloat
|
||||
import com.cobblemon.mod.common.util.withQueryValue
|
||||
import net.minecraft.core.BlockPos
|
||||
import net.minecraft.core.Direction
|
||||
import net.minecraft.util.Mth
|
||||
import net.minecraft.world.entity.LivingEntity
|
||||
import net.minecraft.world.entity.PathfinderMob
|
||||
@@ -64,6 +65,7 @@ class FindAir(
|
||||
return ObjectiveStatus.BLOCKED
|
||||
}
|
||||
|
||||
val level = entity.level()
|
||||
val iterable = BlockPos.betweenClosed(
|
||||
Mth.floor(entity.x - 4.0),
|
||||
entity.blockY - 1,
|
||||
@@ -73,19 +75,27 @@ class FindAir(
|
||||
Mth.floor(entity.z + 4.0)
|
||||
)
|
||||
|
||||
val blockPos = entity.closestPosition(iterable) { isAirPos(entity.level(), it) }
|
||||
val policy = TraversalPolicy.forIntent(entity, MovementIntent.URGENT)
|
||||
val escape = iterable.asSequence()
|
||||
.map { it.immutable() }
|
||||
.filter { isAirPos(level, it) }
|
||||
.map { it.above() }
|
||||
.sortedBy { entity.distanceTo(it) }
|
||||
.take(MAX_CANDIDATES)
|
||||
.mapNotNull { target ->
|
||||
val path = when (val navigation = entity.navigation) {
|
||||
is OmniPathNavigation -> navigation.planPath(target, 0, policy)
|
||||
else -> navigation.createPath(target, 0)
|
||||
}
|
||||
if (path != null && path.canReach() && path.target == target &&
|
||||
PathValidation.isTraversable(level, entity, path)
|
||||
) target to path else null
|
||||
}
|
||||
.minByOrNull { it.second.nodeCount }
|
||||
?: return ObjectiveStatus.INACTIVE
|
||||
|
||||
val walkTarget = WalkTarget(blockPos, speedMultiplier, 0)
|
||||
|
||||
val tracker = RegularMovementTracker.attempt(
|
||||
entity = entity,
|
||||
walkTarget = walkTarget,
|
||||
policy = TraversalPolicy.forIntent(
|
||||
entity = entity,
|
||||
intent = MovementIntent.URGENT,
|
||||
),
|
||||
)
|
||||
val walkTarget = WalkTarget(escape.first, speedMultiplier, 0)
|
||||
val tracker = RegularMovementTracker.following(entity, walkTarget, escape.second, policy)
|
||||
|
||||
if (tracker != null && brain.distract(distraction)) {
|
||||
brain.fixate(
|
||||
@@ -102,17 +112,21 @@ class FindAir(
|
||||
}
|
||||
|
||||
private fun isAirPos(world: LevelReader, pos: BlockPos): Boolean {
|
||||
val blockState = world.getBlockState(pos)
|
||||
val aboveState = world.getBlockState(pos.above())
|
||||
val notFluid = world.getFluidState(pos.above()).isEmpty || blockState.`is`(Blocks.BUBBLE_COLUMN)
|
||||
val canPathfindThroughAbove = aboveState.isPathfindable(PathComputationType.LAND)
|
||||
val solidBelow = !blockState.isPathfindable(PathComputationType.LAND)
|
||||
val above = pos.above()
|
||||
val state = world.getBlockState(pos)
|
||||
val aboveState = world.getBlockState(above)
|
||||
val notFluid = world.getFluidState(above).isEmpty || state.`is`(Blocks.BUBBLE_COLUMN)
|
||||
val shape = state.getCollisionShape(world, pos)
|
||||
|
||||
return notFluid && canPathfindThroughAbove && solidBelow
|
||||
return notFluid &&
|
||||
aboveState.isPathfindable(PathComputationType.LAND) &&
|
||||
!shape.isEmpty && shape.max(Direction.Axis.Y) <= 1.0 &&
|
||||
aboveState.getCollisionShape(world, above).isEmpty
|
||||
}
|
||||
|
||||
companion object {
|
||||
val distraction = Distraction(DistractionType.DAMAGE)
|
||||
private const val MAX_CANDIDATES = 8
|
||||
}
|
||||
|
||||
class Config : ObjectiveConfig {
|
||||
@@ -122,10 +136,6 @@ class FindAir(
|
||||
override fun create(
|
||||
entity: LivingEntity,
|
||||
ctx: BehaviourConfigurationContext
|
||||
): Objective {
|
||||
return FindAir(
|
||||
speedMultiplier = speedMultiplier.asSimplifiedExpression(entity),
|
||||
)
|
||||
}
|
||||
): Objective = FindAir(speedMultiplier = speedMultiplier.asSimplifiedExpression(entity))
|
||||
}
|
||||
}
|
||||
+72
-96
@@ -17,18 +17,22 @@ import com.cobblemon.mod.common.api.ai.biggerbrain.distraction.comfortDistractio
|
||||
import com.cobblemon.mod.common.api.ai.biggerbrain.fixation.EntityFixation
|
||||
import com.cobblemon.mod.common.api.ai.biggerbrain.fixation.MovementTrackerFixation
|
||||
import com.cobblemon.mod.common.api.ai.config.objective.ObjectiveConfig
|
||||
import com.cobblemon.mod.common.entity.ai.CobblemonWalkTarget
|
||||
import com.cobblemon.mod.common.api.ai.movement.MovementIntent
|
||||
import com.cobblemon.mod.common.api.ai.movement.PathValidation
|
||||
import com.cobblemon.mod.common.api.ai.movement.TraversalPolicy
|
||||
import com.cobblemon.mod.common.entity.ai.OmniPathNavigation
|
||||
import com.cobblemon.mod.common.util.*
|
||||
import net.minecraft.core.BlockPos
|
||||
import net.minecraft.world.entity.LivingEntity
|
||||
import net.minecraft.world.entity.PathfinderMob
|
||||
import net.minecraft.world.entity.TamableAnimal
|
||||
import net.minecraft.world.entity.ai.memory.WalkTarget
|
||||
import net.minecraft.world.entity.ai.util.LandRandomPos
|
||||
import net.minecraft.world.entity.ai.util.RandomPos
|
||||
import net.minecraft.world.level.pathfinder.Path
|
||||
import net.minecraft.world.level.pathfinder.PathType
|
||||
import net.minecraft.world.phys.Vec3
|
||||
|
||||
|
||||
/**
|
||||
* Tries to path out of water if the entity usually water to avoid water.
|
||||
* Will pathfind through water even if the entity usually filters out water path nodes.
|
||||
@@ -62,11 +66,9 @@ class MoveOutOfWater(
|
||||
|
||||
entity as TamableAnimal
|
||||
|
||||
if (!entity.isInWater || entity.isUnderWater) {
|
||||
// Focus on not drowning first
|
||||
// TODO: 100% boned if in an underwater cave
|
||||
return ObjectiveStatus.INACTIVE
|
||||
}
|
||||
// Focus on not drowning first
|
||||
// TODO: 100% boned if in an underwater cave
|
||||
if (!entity.isInWater || entity.isUnderWater) return ObjectiveStatus.INACTIVE
|
||||
|
||||
remainingCooldownTicks = ticksBetweenAttempts
|
||||
|
||||
@@ -79,118 +81,88 @@ class MoveOutOfWater(
|
||||
val allowAir = wanderControl?.allowAir
|
||||
?: molang.resolveBoolean("q.entity.movement.can_fly && !q.entity.movement.can_walk".asExpression())
|
||||
|
||||
val types = mutableSetOf<WanderType>()
|
||||
if ((allowLand || allowAir)) {
|
||||
types.add(WanderType.LAND)
|
||||
if (!allowLand && !allowAir) return ObjectiveStatus.INACTIVE
|
||||
if (!brain.distract(comfortDistraction)) return ObjectiveStatus.ACTIVE
|
||||
|
||||
// Sample land targets; keep the one with the shortest physically valid path
|
||||
val policy = TraversalPolicy.forIntent(entity, MovementIntent.URGENT)
|
||||
val best = (0 until MAX_LAND_SAMPLES).asSequence()
|
||||
.mapNotNull { LandRandomPos.getPos(entity, maxDistance, maxDistance) }
|
||||
.map { BlockPos.containing(it.x, it.y, it.z) }
|
||||
.mapNotNull { pos -> validPath(entity, pos, policy)?.let { pos to it } }
|
||||
.minByOrNull { it.second.nodeCount }
|
||||
|
||||
var tracker = best?.let {
|
||||
RegularMovementTracker.following(entity, WalkTarget(it.first, speedMultiplier, 0), it.second, policy)
|
||||
}
|
||||
|
||||
if (types.isEmpty()) {
|
||||
return ObjectiveStatus.INACTIVE
|
||||
}
|
||||
|
||||
if (brain.distract(comfortDistraction)) {
|
||||
var walkTarget: CobblemonWalkTarget? = null
|
||||
val targetVec3 = LandRandomPos.getPos(entity, maxDistance, maxDistance)
|
||||
if (targetVec3 != null) {
|
||||
walkTarget = CobblemonWalkTarget(
|
||||
BlockPos.containing(targetVec3.x, targetVec3.y, targetVec3.z),
|
||||
speedMultiplier,
|
||||
0
|
||||
)
|
||||
if (tracker == null) {
|
||||
if (failCount < 5) {
|
||||
failCount++
|
||||
return ObjectiveStatus.ACTIVE
|
||||
}
|
||||
|
||||
if (walkTarget == null) {
|
||||
if (failCount >= 5) {
|
||||
// No out of water target black with a malus of 0 could be found.
|
||||
// Need to choose a direction and move as far as possible, hoping we randomly find land
|
||||
// Totally possible that a shoreline is *just* outside of the entity's wander range and
|
||||
// we choose a direction that's opposite of the shoreline
|
||||
// To counteract this, the pokemon moves in a (very roughly) spiral pattern outward from the origin
|
||||
var wanderPosition: BlockPos? = null
|
||||
if (wanderDirection == null) {
|
||||
val wanderOffset = RandomPos.generateRandomDirection(entity.level().random, maxDistance / 2, 0)
|
||||
wanderDirection = wanderOffset.toVec3d().normalize()
|
||||
isWanderTurnDirectionClockwise = entity.random.nextInt(2) > 0
|
||||
wanderTurnChance = BASE_TURN_CHANCE
|
||||
wanderPosition = entity.blockPosition().offset(wanderOffset.x, wanderOffset.y, wanderOffset.z)
|
||||
} else {
|
||||
|
||||
if (entity.level().random.nextFloat() < wanderTurnChance) {
|
||||
// Turn 30 degrees
|
||||
wanderDirection =
|
||||
wanderDirection!!.yRot(Math.PI.toFloat() / 3.0f * if (isWanderTurnDirectionClockwise) -1f else 1f)
|
||||
wanderTurnChance *= 0.7
|
||||
}
|
||||
val wanderVec = entity.position().add(wanderDirection!!.scale(maxDistance.toDouble()))
|
||||
wanderPosition = BlockPos.containing(wanderVec)
|
||||
|
||||
}
|
||||
val wanderTarget = generateWaterSafePosToward(entity, maxDistance, wanderPosition)
|
||||
|
||||
if (wanderTarget != null) {
|
||||
walkTarget = CobblemonWalkTarget(
|
||||
wanderPosition,
|
||||
speedMultiplier,
|
||||
0,
|
||||
nodeTypeFilter = { nodeType ->
|
||||
true
|
||||
}
|
||||
)
|
||||
} else {
|
||||
return ObjectiveStatus.ACTIVE
|
||||
}
|
||||
} else {
|
||||
failCount++
|
||||
// No land found. Move as far as possible in a (very roughly) outward spiral, hoping to find a shoreline
|
||||
val wanderPosition = if (wanderDirection == null) {
|
||||
val offset = RandomPos.generateRandomDirection(entity.level().random, maxDistance / 2, 0)
|
||||
wanderDirection = offset.toVec3d().normalize()
|
||||
isWanderTurnDirectionClockwise = entity.random.nextInt(2) > 0
|
||||
wanderTurnChance = BASE_TURN_CHANCE
|
||||
entity.blockPosition().offset(offset.x, offset.y, offset.z)
|
||||
} else {
|
||||
if (entity.level().random.nextFloat() < wanderTurnChance) {
|
||||
wanderDirection = wanderDirection!!.yRot(Math.PI.toFloat() / 3.0f * if (isWanderTurnDirectionClockwise) -1f else 1f)
|
||||
wanderTurnChance *= 0.7
|
||||
}
|
||||
|
||||
BlockPos.containing(entity.position().add(wanderDirection!!.scale(maxDistance.toDouble())))
|
||||
}
|
||||
if (walkTarget != null) {
|
||||
failCount = 0
|
||||
val movementTracker = RegularMovementTracker.attempt(entity, walkTarget)
|
||||
?: return ObjectiveStatus.ACTIVE
|
||||
movementTask = brain.fixate(
|
||||
fixation = StayOutOfWaterFixation(
|
||||
movementTracker = movementTracker
|
||||
)
|
||||
)
|
||||
val wanderTarget = generateWaterSafePosToward(entity, maxDistance, wanderPosition)
|
||||
val wanderPath = wanderTarget?.let { validPath(entity, it, policy) }
|
||||
if (wanderTarget == null || wanderPath == null) {
|
||||
wanderDirection = null // blocked (e.g. fenced in): pick a fresh direction next time instead of pushing into it
|
||||
return ObjectiveStatus.ACTIVE
|
||||
}
|
||||
tracker = RegularMovementTracker.following(entity, WalkTarget(wanderTarget, speedMultiplier, 0), wanderPath, policy)
|
||||
?: return ObjectiveStatus.ACTIVE
|
||||
}
|
||||
|
||||
failCount = 0
|
||||
movementTask = brain.fixate(StayOutOfWaterFixation(movementTracker = tracker))
|
||||
return ObjectiveStatus.ACTIVE
|
||||
}
|
||||
|
||||
private fun validPath(entity: PathfinderMob, pos: BlockPos, policy: TraversalPolicy): Path? {
|
||||
val path = when (val nav = entity.navigation) {
|
||||
is OmniPathNavigation -> nav.planPath(pos, 0, policy)
|
||||
else -> nav.createPath(pos, 0)
|
||||
}
|
||||
return path?.takeIf { it.canReach() && PathValidation.isTraversable(entity.level(), entity, it) }
|
||||
}
|
||||
|
||||
fun generateWaterSafePosToward(
|
||||
mob: PathfinderMob,
|
||||
range: Int,
|
||||
targetPos: BlockPos
|
||||
): BlockPos? {
|
||||
|
||||
for (i in 0..4) {
|
||||
val candidate: BlockPos? = RandomPos.generateRandomPosTowardDirection(
|
||||
mob,
|
||||
range,
|
||||
mob.level().random,
|
||||
targetPos
|
||||
)
|
||||
|
||||
if (candidate == null) continue
|
||||
|
||||
repeat(5) {
|
||||
val candidate = RandomPos.generateRandomPosTowardDirection(mob, range, mob.level().random, targetPos) ?: return@repeat
|
||||
val pathType = mob.navigation.nodeEvaluator.getPathType(mob, candidate)
|
||||
// Don't actually care if it can be reached, only if it's not hazardous
|
||||
if (pathType == PathType.WATER || mob.getPathfindingMalus(pathType) <= 0)
|
||||
return candidate
|
||||
|
||||
if (pathType == PathType.WATER || mob.getPathfindingMalus(pathType) <= 0) return candidate
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val BASE_TURN_CHANCE = 0.8
|
||||
const val MAX_LAND_SAMPLES = 8
|
||||
}
|
||||
|
||||
class StayOutOfWaterFixation(
|
||||
override val movementTracker: MovementTracker,
|
||||
) : EntityFixation, MovementTrackerFixation {
|
||||
var ticksOutOfWater = 0
|
||||
private var lastCheckPos: Vec3? = null
|
||||
private var ticksSinceCheck = 0
|
||||
override fun getDescription(entity: LivingEntity) = "Moving out of water"
|
||||
override fun canDistract(distraction: Distraction, entity: LivingEntity): Boolean {
|
||||
// Easily distracted once we are no longer touching water.
|
||||
@@ -204,14 +176,18 @@ class MoveOutOfWater(
|
||||
override fun tick(entity: LivingEntity, brain: BiggerBrain): Boolean {
|
||||
val movementStatus = tickMovementFixation()
|
||||
if (movementStatus == MovementStatus.COMPLETED) return false
|
||||
|
||||
if (++ticksSinceCheck >= 40) {
|
||||
if (lastCheckPos?.let { entity.position().distanceToSqr(it) < 0.25 } == true) return false
|
||||
lastCheckPos = entity.position()
|
||||
ticksSinceCheck = 0
|
||||
}
|
||||
|
||||
if (entity.isInWater) {
|
||||
ticksOutOfWater = 0
|
||||
} else {
|
||||
ticksOutOfWater++
|
||||
if (ticksOutOfWater > 20) {
|
||||
// Made it out of water, job done
|
||||
return false
|
||||
}
|
||||
} else if (++ticksOutOfWater > 20) {
|
||||
// Made it out of water, job done
|
||||
return false
|
||||
}
|
||||
return movementStatus != MovementStatus.FAILED
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.cobblemon.mod.common.api.ai.movement
|
||||
|
||||
import net.minecraft.world.entity.PathfinderMob
|
||||
import net.minecraft.world.level.LevelReader
|
||||
import net.minecraft.world.level.pathfinder.Path
|
||||
import net.minecraft.world.phys.AABB
|
||||
|
||||
object PathValidation {
|
||||
fun isTraversable(level: LevelReader, entity: PathfinderMob, path: Path): Boolean {
|
||||
val dims = entity.getDimensions(entity.pose)
|
||||
var previous: AABB? = null
|
||||
for (i in 0 until path.nodeCount) {
|
||||
val node = path.getNode(i)
|
||||
val box = dims.makeBoundingBox(node.x + 0.5, node.y.toDouble(), node.z + 0.5).deflate(1.0E-3)
|
||||
val swept = previous?.minmax(box) ?: box
|
||||
if (!level.noCollision(entity, swept)) return false
|
||||
previous = box
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user