From 39efffbdca016c4bb4b75b2ad8dda1b759b9fc16 Mon Sep 17 00:00:00 2001 From: Obique Date: Thu, 26 Sep 2019 12:16:57 -0400 Subject: [PATCH] Improved NPC combat navigation --- .../resources/support/npc/ai/NpcCombatMode.kt | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/projectswg/holocore/resources/support/npc/ai/NpcCombatMode.kt b/src/main/java/com/projectswg/holocore/resources/support/npc/ai/NpcCombatMode.kt index c10eb29bd..4f3519f8b 100644 --- a/src/main/java/com/projectswg/holocore/resources/support/npc/ai/NpcCombatMode.kt +++ b/src/main/java/com/projectswg/holocore/resources/support/npc/ai/NpcCombatMode.kt @@ -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) {