Skip to content

Commit

Permalink
Combat following path improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
GregHib committed Jun 26, 2021
1 parent 5437cc5 commit 8872b9c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ data class CombatTargetStrategy(
* @param walls ranged, magic or halberds
*/
fun isWithinAttackDistance(x: Int, y: Int, plane: Int, target: Character, attackDistance: Int, walls: Boolean): Boolean {
// under
if (x >= target.tile.x && x < target.tile.x + target.size.width && y >= target.tile.y && y < target.tile.y + target.size.height) {
return false
}
val targetX = getNearest(target.tile.x, target.size.width, x)
val targetY = getNearest(target.tile.y, target.size.height, y)
if (Distance.chebyshev(x, y, targetX, targetY) > attackDistance) {
Expand All @@ -39,6 +43,21 @@ data class CombatTargetStrategy(
if (!get<BresenhamsLine>().withinSight(x, y, plane, targetX, targetY, target.tile.plane, walls)) {
return false
}
// diagonal
if (attackDistance <= 1) {
if (x > target.tile.x && y > target.tile.y) {// ne
return false
}
if (x < target.tile.x && y > target.tile.y) {// nw
return false
}
if (x > target.tile.x && y < target.tile.y) {// se
return false
}
if (x < target.tile.x && y < target.tile.y) {// sw
return false
}
}
return true
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import world.gregs.voidps.engine.action.ActionType
import world.gregs.voidps.engine.action.Suspension
import world.gregs.voidps.engine.action.action
import world.gregs.voidps.engine.client.ui.awaitDialogues
import world.gregs.voidps.engine.entity.*
Expand All @@ -12,7 +11,6 @@ import world.gregs.voidps.engine.entity.character.npc.NPCClick
import world.gregs.voidps.engine.entity.character.player.Player
import world.gregs.voidps.engine.entity.character.update.visual.watch
import world.gregs.voidps.engine.event.on
import world.gregs.voidps.engine.path.PathResult
import world.gregs.voidps.engine.path.strat.CombatTargetStrategy
import world.gregs.voidps.engine.path.strat.CombatTargetStrategy.Companion.isWithinAttackDistance
import world.gregs.voidps.network.encode.message
Expand All @@ -37,11 +35,13 @@ fun attack(player: Player, target: Character) {
try {
player.watch(target)
while (isActive && player.awaitDialogues()) {
if (player.hasEffect("skilling_delay")) {
delay(player.remaining("skilling_delay").toInt())
}
if (!withinRange(player, target)) {
await<Unit>(Suspension.Movement)
delay()
continue
}
if (player.remaining("skilling_delay") > 0L) {
delay()
continue
}
if (!canAttack(player, target)) {
break
Expand All @@ -66,16 +66,16 @@ fun attack(player: Player, target: Character) {
fun withinRange(player: Player, target: Character): Boolean {
val maxDistance = (player["attack_range", 1] + if (player.attackStyle == "long_range") 2 else 0).coerceAtMost(10)
val closeCombat = maxDistance == 1
val strategy = CombatTargetStrategy(target, maxDistance, closeCombat)
if (!isWithinAttackDistance(player.tile.x, player.tile.y, player.tile.plane, target, maxDistance, closeCombat)) {
if (!isWithinAttackDistance(player.tile.x, player.tile.y, player.tile.plane, target, maxDistance + if (player.movement.moving) 1 else 0, closeCombat)) {
if (player.movement.steps.isNotEmpty()) {
return false
}
val strategy = CombatTargetStrategy(target, maxDistance, closeCombat)
player.dialogues.clear()
player.movement.set(strategy) {
player.movement.strategy = strategy
player.movement.action = {
if (player.cantReach(strategy) || player.movement.result == null) {
player.message("You can't reach that.")
} else if (player.movement.result is PathResult.Success) {
if (player.action.suspension == Suspension.Movement) {
player.action.resume()
}
}
}
return false
Expand Down

0 comments on commit 8872b9c

Please sign in to comment.