Improved NPC combat navigation

This commit is contained in:
Obique
2019-09-26 12:16:57 -04:00
parent 9c27612791
commit 39efffbdca

View File

@@ -24,6 +24,7 @@ import java.util.concurrent.ThreadLocalRandom
import java.util.concurrent.atomic.AtomicLong
import java.util.concurrent.atomic.AtomicReference
import kotlin.math.cos
import kotlin.math.max
import kotlin.math.sin
class NpcCombatMode(obj: AIObject) : NpcMode(obj) {
@@ -95,13 +96,23 @@ class NpcCombatMode(obj: AIObject) : NpcMode(obj) {
val lineOfSight = obj.isLineOfSight(target)
if (targetDistance > actionRange || !lineOfSight) {
val targetLocation = target.location
val targetHeading = target.location.yaw + ThreadLocalRandom.current().nextDouble(-45.0, 45.0)
val targetRange = actionRange / 2
val moveX = targetLocation.x + cos(targetHeading) * targetRange
val moveZ = targetLocation.z + sin(targetHeading) * targetRange
val moveHeading = targetLocation.getHeadingTo(Point3D(moveX, targetLocation.y, moveZ)) + 180
StartNpcMovementIntent.broadcast(obj, target.effectiveParent, Location.builder(targetLocation).setX(moveX).setZ(moveZ).setHeading(moveHeading).build(), npcRunSpeed)
if (targetDistance > actionRange * 2) {
val targetLocation = target.location
val location = obj.location
val moveHeading = location.getHeadingTo(targetLocation)
val moveDistance = max(0.0, targetDistance - actionRange / 2) / 3
val moveX = targetLocation.x + cos(moveHeading) * moveDistance
val moveZ = targetLocation.z + sin(moveHeading) * moveDistance
StartNpcMovementIntent.broadcast(obj, target.effectiveParent, Location.builder(targetLocation).setX(moveX).setZ(moveZ).setHeading(moveHeading).build(), npcRunSpeed)
} else {
val targetLocation = target.location
val targetHeading = target.location.yaw + ThreadLocalRandom.current().nextDouble(-20.0, 20.0)
val targetRange = actionRange / 2
val moveX = targetLocation.x + cos(targetHeading) * targetRange
val moveZ = targetLocation.z + sin(targetHeading) * targetRange
val moveHeading = targetLocation.getHeadingTo(Point3D(moveX, targetLocation.y, moveZ)) + 180
StartNpcMovementIntent.broadcast(obj, target.effectiveParent, Location.builder(targetLocation).setX(moveX).setZ(moveZ).setHeading(moveHeading).build(), npcRunSpeed)
}
}
if (lineOfSight && iteration.get() % 4 == 0L) {