mirror of
https://github.com/2006-Scape/apollo.git
synced 2026-07-04 16:49:11 +00:00
Port the walkto and following plugins to Kotlin
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
name = "following"
|
||||
package = "org.apollo.game.plugin.entity"
|
||||
authors = [ "Gary Tierney" ]
|
||||
dependencies = [ "walkto", "command_utilities", "player_action"]
|
||||
|
||||
[config]
|
||||
srcDir = "src/"
|
||||
testDir = "test/"
|
||||
@@ -0,0 +1,52 @@
|
||||
package org.apollo.plugin.entity.following
|
||||
|
||||
import org.apollo.game.action.Action
|
||||
import org.apollo.game.model.Direction
|
||||
import org.apollo.game.model.Position
|
||||
import org.apollo.game.model.entity.Mob
|
||||
import org.apollo.game.model.entity.Player
|
||||
import org.apollo.net.message.Message
|
||||
import org.apollo.plugin.entity.walkto.walkBehind
|
||||
import org.apollo.plugin.entity.walkto.walkTo
|
||||
|
||||
class FollowAction(player: Player, val target: Player) : Action<Player>(0, true, player) {
|
||||
var lastPosition: Position? = null
|
||||
|
||||
companion object {
|
||||
fun start(player: Player, target: Player, message: Message? = null) {
|
||||
player.startAction(FollowAction(player, target))
|
||||
message?.terminate()
|
||||
}
|
||||
}
|
||||
|
||||
override fun execute() {
|
||||
if (!target.isActive) {
|
||||
stop()
|
||||
return
|
||||
}
|
||||
|
||||
mob.interactingMob = target
|
||||
|
||||
if (target.position == lastPosition) {
|
||||
return
|
||||
}
|
||||
|
||||
val distance = mob.position.getDistance(target.position)
|
||||
if (distance >= 15) {
|
||||
stop()
|
||||
return
|
||||
}
|
||||
|
||||
if (mob.position == target.position) {
|
||||
val directions = Direction.NESW
|
||||
val directionOffset = (Math.random() * directions.size).toInt()
|
||||
|
||||
mob.walkTo(target.position.step(1, directions[directionOffset]))
|
||||
return
|
||||
}
|
||||
|
||||
mob.walkBehind(target)
|
||||
lastPosition = target.position
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import com.google.common.primitives.Ints
|
||||
import org.apollo.game.message.impl.PlayerActionMessage
|
||||
import org.apollo.game.model.entity.setting.PrivilegeLevel
|
||||
import org.apollo.plugin.entity.following.FollowAction
|
||||
|
||||
on_player_event { PlayerActionEvent::class }
|
||||
.where { action == PlayerActionType.FOLLOW }
|
||||
.then {
|
||||
FollowAction.start(it, target)
|
||||
terminate()
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
name = "player_action"
|
||||
package = "org.apollo.game.plugin.entity"
|
||||
authors = [ "Gary Tierney" ]
|
||||
dependencies = []
|
||||
|
||||
[config]
|
||||
srcDir = "src/"
|
||||
testDir = "test/"
|
||||
@@ -0,0 +1,35 @@
|
||||
import org.apollo.game.message.impl.SetPlayerActionMessage
|
||||
import org.apollo.game.model.entity.Player
|
||||
import org.apollo.game.model.event.PlayerEvent
|
||||
import java.util.*
|
||||
|
||||
enum class PlayerActionType(val displayName: String, val slot: Int, val primary: Boolean = true) {
|
||||
ATTACK("Attack", 2),
|
||||
CHALLENGE("Challenge", 2),
|
||||
FOLLOW("Follow", 4),
|
||||
TRADE("Trade with", 5)
|
||||
}
|
||||
|
||||
class PlayerActionEvent(player: Player, val target: Player, val action: PlayerActionType) : PlayerEvent(player)
|
||||
|
||||
private val playerActionsMap = mutableMapOf<Player, EnumSet<PlayerActionType>>()
|
||||
private val Player.actions: EnumSet<PlayerActionType>
|
||||
get() = playerActionsMap.computeIfAbsent(this, { EnumSet.noneOf(PlayerActionType::class.java) })
|
||||
|
||||
fun Player.enableAction(action: PlayerActionType) {
|
||||
send(SetPlayerActionMessage(action.displayName, action.slot, action.primary))
|
||||
actions.add(action)
|
||||
}
|
||||
|
||||
fun Player.disableAction(action: PlayerActionType) {
|
||||
send(SetPlayerActionMessage("null", action.slot, action.primary))
|
||||
actions.remove(action)
|
||||
}
|
||||
|
||||
fun Player.actionEnabled(action: PlayerActionType): Boolean {
|
||||
return actions.contains(action)
|
||||
}
|
||||
|
||||
fun Player.actionAt(slot: Int): PlayerActionType? {
|
||||
return actions.find { it.slot == slot }
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import org.apollo.game.message.impl.PlayerActionMessage
|
||||
import org.apollo.game.model.event.impl.LoginEvent
|
||||
|
||||
on { PlayerActionMessage::class }
|
||||
.then {
|
||||
val action = it.actionAt(option)
|
||||
if (action != null) {
|
||||
it.world.submit(PlayerActionEvent(it, it.world.playerRepository[index], action))
|
||||
}
|
||||
|
||||
terminate()
|
||||
}
|
||||
|
||||
on_player_event { LoginEvent::class }
|
||||
.then {
|
||||
it.enableAction(PlayerActionType.FOLLOW)
|
||||
it.enableAction(PlayerActionType.TRADE)
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
name = "walkto"
|
||||
package = "org.apollo.plugin.entity.walkto"
|
||||
authors = ["Gary Tierney"]
|
||||
dependencies = []
|
||||
@@ -0,0 +1,59 @@
|
||||
package org.apollo.plugin.entity.walkto
|
||||
|
||||
import org.apollo.game.model.Direction
|
||||
import org.apollo.game.model.Position
|
||||
import org.apollo.game.model.entity.*
|
||||
import org.apollo.game.model.entity.obj.GameObject
|
||||
import org.apollo.game.model.entity.path.SimplePathfindingAlgorithm
|
||||
|
||||
private fun bounds(target: Entity): Pair<Int, Int> = when (target) {
|
||||
is GameObject -> {
|
||||
val orientation = Direction.WNES[target.orientation]
|
||||
val rotated = (orientation == Direction.WEST || orientation == Direction.EAST)
|
||||
|
||||
val width = if (rotated) target.definition.length else target.definition.width
|
||||
val height = if (rotated) target.definition.width else target.definition.length
|
||||
|
||||
Pair(width, height)
|
||||
}
|
||||
is Npc -> Pair(target.definition.size, target.definition.size)
|
||||
is Player -> Pair(1, 1)
|
||||
else -> error("Invalid entity type")
|
||||
}
|
||||
|
||||
fun Mob.walkTo(target: Entity, positioningDirection: Direction? = null) {
|
||||
val (sourceWidth, sourceHeight) = bounds(target)
|
||||
val (targetWidth, targetHeight) = bounds(target)
|
||||
|
||||
val direction = positioningDirection ?: Direction.between(position, target.position)
|
||||
val dx = direction.deltaX()
|
||||
val dy = direction.deltaY()
|
||||
|
||||
val targetX = if (dx <= 0) target.position.x else target.position.x + targetWidth - 1
|
||||
val targetY = if (dy <= 0) target.position.y else target.position.y + targetHeight - 1
|
||||
val offsetX = if (dx < 0) -sourceWidth else if (dx > 0) 1 else 0
|
||||
val offsetY = if (dy < 0) -sourceHeight else if (dy > 0) 1 else 0
|
||||
|
||||
walkTo(Position(targetX + offsetX, targetY + offsetY, position.height))
|
||||
}
|
||||
|
||||
fun Mob.walkBehind(target: Mob) {
|
||||
walkTo(target, target.lastDirection.opposite())
|
||||
}
|
||||
|
||||
fun Mob.walkTo(target: Position, positionPredicate: (Position) -> Boolean = { true }) {
|
||||
if (position == target) {
|
||||
return
|
||||
}
|
||||
|
||||
val pathfinder = SimplePathfindingAlgorithm(world.collisionManager)
|
||||
val path = pathfinder.find(position, target)
|
||||
|
||||
for (step in path) {
|
||||
if (!positionPredicate.invoke(step)) {
|
||||
return
|
||||
}
|
||||
|
||||
walkingQueue.addStep(step)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user