From 09f7e8ac7040df12ca10a94aa79f6d1d3da1f983 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Fri, 6 Sep 2024 19:55:01 +0700 Subject: [PATCH] Misc fixes regarding NPC behavior tree, but they still dont function properly (except ship pet) --- .../java/org/classdump/luna/runtime/ResumeInfo.java | 2 +- .../defs/actor/behavior/CompositeNodeType.kt | 5 ++--- .../world/entities/ActorMovementController.kt | 3 ++- .../kstarbound/world/entities/MovementController.kt | 7 ++----- .../kstarbound/world/entities/PathController.kt | 11 +++++++++-- .../world/entities/behavior/AbstractBehaviorNode.kt | 3 +++ .../world/entities/behavior/ActionNode.kt | 3 ++- .../world/entities/behavior/DecoratorNode.kt | 3 ++- .../world/entities/behavior/DynamicNode.kt | 3 ++- .../world/entities/behavior/ParallelNode.kt | 3 ++- .../world/entities/behavior/RandomizeNode.kt | 6 ++++-- .../world/entities/behavior/SequenceNode.kt | 13 +++++++++---- 12 files changed, 40 insertions(+), 22 deletions(-) diff --git a/luna/src/main/java/org/classdump/luna/runtime/ResumeInfo.java b/luna/src/main/java/org/classdump/luna/runtime/ResumeInfo.java index b660d7da..e4548432 100644 --- a/luna/src/main/java/org/classdump/luna/runtime/ResumeInfo.java +++ b/luna/src/main/java/org/classdump/luna/runtime/ResumeInfo.java @@ -40,7 +40,7 @@ class ResumeInfo { if (resumable instanceof ProtectedResumable) { // top is protected, can handle the error ProtectedResumable pr = (ProtectedResumable) resumable; - pr.resumeError(context, savedState, Conversions.toErrorObject(error)); + pr.resumeError(context, savedState, error); Dispatch.evaluateTailCalls(context); return true; } else { diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/actor/behavior/CompositeNodeType.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/actor/behavior/CompositeNodeType.kt index a2360165..9f2fd8eb 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/actor/behavior/CompositeNodeType.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/actor/behavior/CompositeNodeType.kt @@ -11,9 +11,8 @@ import ru.dbotthepony.kstarbound.world.entities.behavior.RandomizeNode import ru.dbotthepony.kstarbound.world.entities.behavior.SequenceNode enum class CompositeNodeType(override val jsonName: String, val factory: (Map, ImmutableList) -> AbstractBehaviorNode) : IStringSerializable { - SEQUENCE("Sequence", { _, c -> SequenceNode(c) }), - // in original engine, selector and sequence nodes are identical code-wise - SELECTOR("Selector", { _, c -> SequenceNode(c) }), + SEQUENCE("Sequence", { _, c -> SequenceNode(c, isSelector = false) }), + SELECTOR("Selector", { _, c -> SequenceNode(c, isSelector = true) }), PARALLEL("Parallel", { p, c -> ParallelNode(p, c) }), DYNAMIC("Dynamic", { _, c -> DynamicNode(c) }), RANDOMIZE("Randomize", { _, c -> RandomizeNode(c) }); diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/ActorMovementController.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/ActorMovementController.kt index b80c38d3..701dfbd3 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/ActorMovementController.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/ActorMovementController.kt @@ -239,7 +239,7 @@ class ActorMovementController() : MovementController() { updateParameters(calculateMovementParameters(actorMovementParameters)) } - // TODO: run is unsed + // TODO: run is unused fun pathMove(position: Vector2d, run: Boolean, parameters: PathFinder.Parameters? = null): Pair? { // FIXME: code flow is stupid @@ -281,6 +281,7 @@ class ActorMovementController() : MovementController() { controlMove = null controlRun = false controlCrouch = false + controlDown = false controlJump = false controlJumpAnyway = false controlFly = null diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/MovementController.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/MovementController.kt index 048b4bff..c50e6949 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/MovementController.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/MovementController.kt @@ -197,9 +197,6 @@ open class MovementController { var movementParameters: MovementParameters = Globals.movementParameters protected set - var gravityMultiplier = 1.0 - var isGravityDisabled = false - open fun isAtWorldLimit(bottomOnly: Boolean = false): Boolean { return false // TODO } @@ -212,7 +209,7 @@ open class MovementController { } fun determineGravity(): Vector2d { - if (isGravityDisabled) + if (movementParameters.gravityEnabled == false) return Vector2d.ZERO return world.chunkMap.gravityAt(position) @@ -279,7 +276,7 @@ open class MovementController { */ // TODO: Ghost collisions occur, where objects trip on edges open fun move(delta: Double) { - isZeroGravity = isGravityDisabled || gravityMultiplier == 0.0 || determineGravity().lengthSquared == 0.0 + isZeroGravity = movementParameters.gravityEnabled == false || (movementParameters.gravityMultiplier ?: 1.0) == 0.0 || determineGravity().lengthSquared == 0.0 val movementParameters = movementParameters diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/PathController.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/PathController.kt index 7e762160..4c811a85 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/PathController.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/PathController.kt @@ -102,6 +102,9 @@ class PathController(val controller: ActorMovementController, var edgeTimer: Dou this.path.addAll(newPath) } } + } else if (this.path.isEmpty() && newPath.isNotEmpty()) { + merged = true + this.path.addAll(newPath) } if (!merged && controller.position != this.startPosition) { @@ -130,7 +133,11 @@ class PathController(val controller: ActorMovementController, var edgeTimer: Dou } this.edgeTimer = newEdgeTimer - return this.path.isEmpty() + + if (this.path.isEmpty()) + return true + else + return null } else { return null } @@ -139,7 +146,7 @@ class PathController(val controller: ActorMovementController, var edgeTimer: Dou fun findPath(target: Vector2d): Boolean? { // reached the end of the last path and we have a new target position to move toward - if (path.isEmpty() && controller.world.geometry.diff(this.target!!, target).lengthSquared > 0.1) { + if (this.target != null && path.isEmpty() && controller.world.geometry.diff(this.target!!, target).lengthSquared > 0.1) { reset() this.target = target } diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/behavior/AbstractBehaviorNode.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/behavior/AbstractBehaviorNode.kt index 339079f6..c8562cc2 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/behavior/AbstractBehaviorNode.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/behavior/AbstractBehaviorNode.kt @@ -23,6 +23,9 @@ abstract class AbstractBehaviorNode { RUNNING(null) } + var calls = 0 + protected set + abstract fun run(delta: Double, state: BehaviorState): Status abstract fun reset() diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/behavior/ActionNode.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/behavior/ActionNode.kt index de7691b6..0d1510c7 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/behavior/ActionNode.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/behavior/ActionNode.kt @@ -14,6 +14,7 @@ class ActionNode(val name: String, val parameters: Map, v private val nodeID = BehaviorState.NODE_GARBAGE_INDEX.getAndIncrement() override fun run(delta: Double, state: BehaviorState): Status { + calls++ var coroutine = coroutine var firstTime = false @@ -67,7 +68,7 @@ class ActionNode(val name: String, val parameters: Map, v } override fun toString(): String { - return "ActionNode[$name, parameters=$parameters, outputs=$outputs, coroutine=$coroutine]" + return "ActionNode[$calls / $name, parameters=$parameters, outputs=$outputs, coroutine=$coroutine]" } companion object { diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/behavior/DecoratorNode.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/behavior/DecoratorNode.kt index 170640ec..0942115a 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/behavior/DecoratorNode.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/behavior/DecoratorNode.kt @@ -15,6 +15,7 @@ class DecoratorNode(val name: String, val parameters: Map private val nodeID = BehaviorState.NODE_GARBAGE_INDEX.getAndIncrement() override fun run(delta: Double, state: BehaviorState): Status { + calls++ var coroutine = coroutine if (coroutine == null) { @@ -100,7 +101,7 @@ class DecoratorNode(val name: String, val parameters: Map } override fun toString(): String { - return "DecoratorNode[$name, coroutine=$coroutine, parameters=$parameters, children=$child]" + return "DecoratorNode[$calls / $name, coroutine=$coroutine, parameters=$parameters, children=$child]" } companion object { diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/behavior/DynamicNode.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/behavior/DynamicNode.kt index 7318253e..7e7e8b2d 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/behavior/DynamicNode.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/behavior/DynamicNode.kt @@ -8,6 +8,7 @@ class DynamicNode(val children: ImmutableList) : AbstractB private var index = 0 override fun run(delta: Double, state: BehaviorState): Status { + calls++ for ((i, node) in children.withIndex()) { val status = node.runAndReset(delta, state) @@ -27,7 +28,7 @@ class DynamicNode(val children: ImmutableList) : AbstractB } override fun toString(): String { - return "DynamicNode[${children.size}, index=$index, current=${children.getOrNull(index)}]" + return "DynamicNode[$calls / ${children.size}, index=$index, current=${children.getOrNull(index)}]" } override fun reset() { diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/behavior/ParallelNode.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/behavior/ParallelNode.kt index 441dcd5f..b82f6ef9 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/behavior/ParallelNode.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/behavior/ParallelNode.kt @@ -34,6 +34,7 @@ class ParallelNode(parameters: Map, val children: Immutab private var lastSucceeded = -1 override fun run(delta: Double, state: BehaviorState): Status { + calls++ var failed = 0 var succeeded = 0 @@ -58,7 +59,7 @@ class ParallelNode(parameters: Map, val children: Immutab } override fun toString(): String { - return "ParallelNode[children=${children.size}, successLimit=$successLimit, failLimit=$failLimit, lastFailed=$lastFailed, lastSucceeded=$lastSucceeded]" + return "ParallelNode[$calls / children=${children.size}, successLimit=$successLimit, failLimit=$failLimit, lastFailed=$lastFailed, lastSucceeded=$lastSucceeded]" } override fun reset() { diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/behavior/RandomizeNode.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/behavior/RandomizeNode.kt index 96856559..f0b1a511 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/behavior/RandomizeNode.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/behavior/RandomizeNode.kt @@ -8,6 +8,8 @@ class RandomizeNode(val children: ImmutableList) : Abstrac var index = -1 override fun run(delta: Double, state: BehaviorState): Status { + calls++ + if (index == -1 && children.isNotEmpty()) index = state.lua.random.nextInt(children.size) @@ -19,9 +21,9 @@ class RandomizeNode(val children: ImmutableList) : Abstrac override fun toString(): String { if (index == -1) - return "RandomizeNode[${children.size}, not chosen]" + return "RandomizeNode[$calls / ${children.size}, not chosen]" else - return "RandomizeNode[${children.size}, ${children[index]}]" + return "RandomizeNode[$calls / ${children.size}, ${children[index]}]" } override fun reset() { diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/behavior/SequenceNode.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/behavior/SequenceNode.kt index 220d9b6d..e4ef48ac 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/behavior/SequenceNode.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/behavior/SequenceNode.kt @@ -4,25 +4,30 @@ import com.google.common.collect.ImmutableList import org.classdump.luna.runtime.ExecutionContext import ru.dbotthepony.kstarbound.lua.userdata.BehaviorState -class SequenceNode(val children: ImmutableList) : AbstractBehaviorNode() { +class SequenceNode(val children: ImmutableList, val isSelector: Boolean) : AbstractBehaviorNode() { private var index = 0 override fun run(delta: Double, state: BehaviorState): Status { + calls++ + while (index < children.size) { val child = children[index] val status = child.runAndReset(delta, state) - if (status == Status.FAILURE || status == Status.RUNNING) + if ((isSelector && status == Status.SUCCESS || !isSelector && status == Status.FAILURE) || status == Status.RUNNING) return status index++ } - return Status.SUCCESS + return if (isSelector) Status.FAILURE else Status.SUCCESS } override fun toString(): String { - return "SequenceNode[${children.size}, index=$index, current=${children.getOrNull(index)}]" + if (isSelector) + return "SelectorNode[$calls / ${children.size}, index=$index, current=${children.getOrNull(index)}]" + else + return "SequenceNode[$calls / ${children.size}, index=$index, current=${children.getOrNull(index)}]" } override fun reset() {