Skip to content

Commit

Permalink
Path Stream avoid negative nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
gabber235 committed Sep 22, 2024
1 parent 35a5312 commit 6b1b27b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import com.typewritermc.engine.paper.utils.msg
import com.typewritermc.engine.paper.utils.playSound
import lirand.api.extensions.events.unregister
import lirand.api.extensions.server.registerEvents
import lirand.api.extensions.world.clearAll
import org.bukkit.Material
import org.bukkit.entity.Player
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
Expand Down Expand Up @@ -128,17 +126,15 @@ class ContentEditor(
}
}

fun isInLastMode(): Boolean {
return stack.size == 1
}
fun isInLastMode(): Boolean = stack.size == 1

@EventHandler
fun onInventoryClick(event: InventoryClickEvent) {
if (event.whoClicked != player) return
if (event.clickedInventory != player.inventory) return
val item = items[event.slot] ?: return
item.action(
ItemInteraction(ItemInteractionType.INVENTORY_CLICK, event.slot)
ItemInteraction(ItemInteractionType.INVENTORY_CLICK, event.slot),
)
event.isCancelled = true
}
Expand Down Expand Up @@ -184,12 +180,13 @@ class ContentEditor(
}

private val Player.content: ContentEditor?
get() = with(KoinJavaComponent.get<InteractionHandler>(InteractionHandler::class.java)) {
interaction?.content
}
get() =
with(KoinJavaComponent.get<InteractionHandler>(InteractionHandler::class.java)) {
interaction?.content
}

val Player.isInContent: Boolean
get() = content != null

val Player.inLastContentMode: Boolean
get() = content?.isInLastMode() == true
get() = content?.isInLastMode() == true
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
package com.typewritermc.engine.paper.entry.roadnetwork.gps

import com.extollit.gaming.ai.path.HydrazinePathFinder
import com.extollit.gaming.ai.path.model.Passibility
import com.extollit.linalg.immutable.Vec3d
import com.github.retrooper.packetevents.protocol.particle.Particle
import com.github.retrooper.packetevents.protocol.particle.type.ParticleTypes
import com.github.retrooper.packetevents.util.Vector3f
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerParticle
import kotlinx.coroutines.Job
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.coroutineScope
import lirand.api.extensions.server.server
import com.typewritermc.core.entries.Ref
import com.typewritermc.engine.paper.entry.entity.toProperty
import com.typewritermc.engine.paper.entry.entries.AudienceDisplay
import com.typewritermc.engine.paper.entry.entries.RoadNetworkEntry
import com.typewritermc.engine.paper.entry.entries.TickableDisplay
import com.typewritermc.engine.paper.entry.entries.roadNetworkMaxDistance
import com.typewritermc.engine.paper.entry.roadnetwork.RoadNetworkManager
import com.typewritermc.engine.paper.entry.roadnetwork.pathfinding.PFEmptyEntity
import com.typewritermc.engine.paper.entry.roadnetwork.pathfinding.PFInstanceSpace
import com.typewritermc.engine.paper.extensions.packetevents.sendPacketTo
Expand All @@ -25,8 +22,15 @@ import com.typewritermc.engine.paper.snippets.snippet
import com.typewritermc.engine.paper.utils.ThreadType.DISPATCHERS_ASYNC
import com.typewritermc.engine.paper.utils.distanceSqrt
import com.typewritermc.engine.paper.utils.firstWalkableLocationBelow
import kotlinx.coroutines.Job
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.coroutineScope
import lirand.api.extensions.server.server
import org.bukkit.Location
import org.bukkit.entity.Player
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import java.util.*

private val pathStreamRefreshTime by snippet(
Expand Down Expand Up @@ -101,7 +105,9 @@ private class PlayerPathStreamDisplay(
private val player: Player,
private val startLocation: (Player) -> Location,
private val endLocation: (Player) -> Location,
) {
) : KoinComponent {
private val roadNetworkManager: RoadNetworkManager by inject()

private var gps = PointToPointGPS(ref, { startLocation(player) }, { endLocation(player) })
private var edges = emptyList<GPSEdge>()

Expand Down Expand Up @@ -168,13 +174,31 @@ private class PlayerPathStreamDisplay(
}
}

private fun findPath(
private suspend fun findPath(
start: Location,
end: Location,
): Iterable<Location> {
val roadNetwork = roadNetworkManager.getNetwork(gps.roadNetwork)

val interestingNegativeNodes = roadNetwork.negativeNodes.filter {
val distance = start.distanceSqrt(it.location) ?: 0.0
distance > it.radius * it.radius && distance < roadNetworkMaxDistance * roadNetworkMaxDistance
}

val entity = PFEmptyEntity(start.toProperty(), searchRange = roadNetworkMaxDistance.toFloat())
val instance = PFInstanceSpace(start.world)
val pathfinder = HydrazinePathFinder(entity, instance)

val additionalRadius = pathfinder.subject().width().toDouble()

// We want to avoid going through negative nodes
pathfinder.withGraphNodeFilter { node ->
if (node.isInRangeOf(interestingNegativeNodes, additionalRadius)) {
return@withGraphNodeFilter Passibility.dangerous
}
node.passibility()
}

val path = pathfinder.computePathTo(Vec3d(end.x, end.y, end.z)) ?: return emptyList()
return path.map {
val coordinate = it.coordinates()
Expand Down

0 comments on commit 6b1b27b

Please sign in to comment.