Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert pauses and queues with interaction delays #590

Merged
merged 16 commits into from
Jan 21, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class FloorItemOptionHandler(
private val logger = InlineLogger()

override fun validate(player: Player, instruction: InteractFloorItem) {
if (player.contains("delay") || player.hasClock("input_delay")) {
if (player.contains("delay")) {
return
}
val (id, x, y, optionIndex) = instruction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class NPCOptionHandler(
private val logger = InlineLogger()

override fun validate(player: Player, instruction: InteractNPC) {
if (player.contains("delay") || player.hasClock("input_delay")) {
if (player.contains("delay")) {
return
}
val npc = npcs.indexed(instruction.npcIndex) ?: return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ObjectOptionHandler(
private val logger = InlineLogger()

override fun validate(player: Player, instruction: InteractObject) {
if (player.contains("delay") || player.hasClock("input_delay")) {
if (player.contains("delay")) {
return
}
val (objectId, x, y, option) = instruction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class PlayerOptionHandler(
private val logger = InlineLogger()

override fun validate(player: Player, instruction: InteractPlayer) {
if (player.contains("delay") || player.hasClock("input_delay")) {
if (player.contains("delay")) {
return
}
val target = players.indexed(instruction.playerIndex) ?: return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import world.gregs.voidps.network.client.instruction.Walk
class WalkHandler : InstructionHandler<Walk>() {

override fun validate(player: Player, instruction: Walk) {
if (player.contains("delay") || player.hasClock("input_delay")) {
if (player.contains("delay")) {
return
}
player.closeInterfaces()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ abstract class Interaction<C : Character> : CancellableEvent(), SuspendableEvent
var approach = false
val operate: Boolean
get() = !approach
override var onCancel: (() -> Unit)? = null
var launched = false

abstract fun copy(approach: Boolean): Interaction<C>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ data class AreaEntered<C : Character>(
val area: Area
) : SuspendableEvent, SuspendableContext<C> {

override var onCancel: (() -> Unit)? = null

override val size = 5

override suspend fun pause(ticks: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ data class AreaExited<C : Character>(
val tags: Set<String>,
val area: Area
) : SuspendableEvent, SuspendableContext<C> {
override var onCancel: (() -> Unit)? = null

override val size = 5

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ data class Moved<C : Character>(
val from: Tile,
val to: Tile
) : CancellableEvent(), SuspendableContext<C>, SuspendableEvent {
override var onCancel: (() -> Unit)? = null

override val size = 4

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import world.gregs.voidps.engine.entity.character.player.Player
*/
interface Context<C : Character> {
val character: C
var onCancel: (() -> Unit)?
val Context<Player>.player: Player
get() = character
val Context<NPC>.npc: NPC
Expand Down
26 changes: 25 additions & 1 deletion engine/src/main/kotlin/world/gregs/voidps/engine/queue/Action.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Action<C : Character>(
val priority: ActionPriority,
delay: Int = 0,
val behaviour: LogoutBehaviour = LogoutBehaviour.Discard,
override var onCancel: (() -> Unit)? = { character.clearAnimation() },
var onCancel: (() -> Unit)? = { character.clearAnimation() },
var action: suspend Action<*>.() -> Unit = {}
) : SuspendableContext<C> {
var suspension: CancellableContinuation<Unit>? = null
Expand Down Expand Up @@ -52,6 +52,30 @@ class Action<C : Character>(
}
}

/**
* Queue calls shouldn't be nested and should be replaced with suspensions
*/

@Suppress("UNUSED_PARAMETER", "UnusedReceiverParameter")
@Deprecated("Replace nested queues with pause", ReplaceWith("pause(initialDelay)"))
fun Character.queue(name: String, initialDelay: Int = 0, behaviour: LogoutBehaviour = LogoutBehaviour.Discard, onCancel: (() -> Unit)? = null, block: (suspend Action<C>.() -> Unit)?) {
}

@Suppress("UNUSED_PARAMETER", "UnusedReceiverParameter")
@Deprecated("Replace nested queues with pause", ReplaceWith("pause(initialDelay)"))
fun Character.softQueue(name: String, initialDelay: Int = 0, behaviour: LogoutBehaviour = LogoutBehaviour.Discard, onCancel: (() -> Unit)? = null, block: (suspend Action<C>.() -> Unit)?) {
}

@Suppress("UNUSED_PARAMETER", "UnusedReceiverParameter")
@Deprecated("Replace nested queues with pause", ReplaceWith("pause(initialDelay)"))
fun Character.weakQueue(name: String, initialDelay: Int = 0, behaviour: LogoutBehaviour = LogoutBehaviour.Discard, onCancel: (() -> Unit)? = null, block: (suspend Action<C>.() -> Unit)?) {
}

@Suppress("UNUSED_PARAMETER", "UnusedReceiverParameter")
@Deprecated("Replace nested queues with pause", ReplaceWith("pause(initialDelay)"))
fun Character.strongQueue(name: String, initialDelay: Int = 0, behaviour: LogoutBehaviour = LogoutBehaviour.Discard, onCancel: (() -> Unit)? = null, block: (suspend Action<C>.() -> Unit)?) {
}

override fun toString(): String {
return "${name}_${count}_${priority.name.toSnakeCase()}_${behaviour.name.toSnakeCase()}"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package world.gregs.voidps.engine.suspend

import kotlinx.coroutines.suspendCancellableCoroutine
import world.gregs.voidps.engine.entity.character.Character
import world.gregs.voidps.engine.entity.character.mode.interact.Interaction
import world.gregs.voidps.engine.event.Context

interface SuspendableContext<C : Character> : Context<C> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ internal class ActionQueueTest {
}

@Test
fun `Queues can be suspended and resume`() {
fun `Queues can be suspended and resumed`() {
var resumed = false
val action = action {
pause(4)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import world.gregs.voidps.engine.client.ui.chat.Colours
import world.gregs.voidps.engine.client.ui.chat.plural
import world.gregs.voidps.engine.client.ui.chat.toTag
import world.gregs.voidps.engine.client.ui.event.adminCommand
import world.gregs.voidps.engine.client.variable.start
import world.gregs.voidps.engine.data.Settings
import world.gregs.voidps.engine.data.definition.data.Rock
import world.gregs.voidps.engine.data.settingsReload
Expand All @@ -31,7 +30,6 @@ import world.gregs.voidps.engine.inv.add
import world.gregs.voidps.engine.inv.inventory
import world.gregs.voidps.engine.inv.remove
import world.gregs.voidps.engine.map.collision.blocked
import world.gregs.voidps.engine.queue.softQueue
import world.gregs.voidps.engine.timer.timerStart
import world.gregs.voidps.engine.timer.timerStop
import world.gregs.voidps.engine.timer.timerTick
Expand Down Expand Up @@ -224,15 +222,13 @@ objectApproach("Prospect", "crashed_star_tier_#") {
arriveDelay()
val starPayout = target.def["collect_for_next_layer", -1]
player.message("You examine the crashed star...")
player.start("movement_delay", 4)
player.softQueue("prospect", 4) {
val star = def.getOrNull<Rock>("mining")?.ores?.firstOrNull()
if (star == null) {
player.message("Star has been mined...")
} else if (starPayout != -1) {
val percentageCollected = getLayerPercentage(totalCollected, starPayout)
player.message("There is $percentageCollected% left of this layer.")
}
delay(4)
val star = def.getOrNull<Rock>("mining")?.ores?.firstOrNull()
if (star == null) {
player.message("Star has been mined...")
} else if (starPayout != -1) {
val percentageCollected = getLayerPercentage(totalCollected, starPayout)
player.message("There is $percentageCollected% left of this layer.")
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
package world.gregs.voidps.world.activity.quest.mini

import world.gregs.voidps.engine.client.message
import world.gregs.voidps.engine.entity.character.mode.interact.TargetInteraction
import world.gregs.voidps.engine.event.TargetContext
import world.gregs.voidps.engine.entity.character.npc.NPC
import world.gregs.voidps.engine.entity.character.player.Player
import world.gregs.voidps.engine.inv.inventory
import world.gregs.voidps.engine.inv.remove
import world.gregs.voidps.engine.queue.queue
import world.gregs.voidps.engine.suspend.SuspendableContext
import world.gregs.voidps.world.activity.quest.quest
import world.gregs.voidps.world.interact.dialogue.Sad
import world.gregs.voidps.world.interact.dialogue.Talk
import world.gregs.voidps.world.interact.dialogue.type.npc
import world.gregs.voidps.world.interact.dialogue.type.player

suspend fun <T> T.barCrawlDrink(
suspend fun <T : TargetInteraction<Player, NPC>> T.barCrawlDrink(
start: (suspend T.() -> Unit)? = null,
effects: suspend T.() -> Unit = {},
) where T : TargetContext<Player, NPC>, T : SuspendableContext<Player> {
) {
player<Talk>("I'm doing Alfred Grimhand's Barcrawl.")
val info: Map<String, Any> = target.def.getOrNull("bar_crawl") ?: return
start?.invoke(this) ?: npc<Talk>(info["start"] as String)
Expand All @@ -27,15 +26,14 @@ suspend fun <T> T.barCrawlDrink(
return
}
player.message(info["give"] as String)
player.queue("barcrawl_$id", 4) {
player.message(info["drink"] as String)
pause(4)
player.message(info["effect"] as String)
pause(4)
(info["sign"] as? String)?.let { player.message(it) }
player.addVarbit("barcrawl_signatures", id)
effects()
}
delay(4)
player.message(info["drink"] as String)
delay(4)
player.message(info["effect"] as String)
delay(4)
(info["sign"] as? String)?.let { player.message(it) }
player.addVarbit("barcrawl_signatures", id)
effects()
}

val barCrawlFilter: TargetContext<Player, NPC>.() -> Boolean = filter@{
Expand Down
Loading
Loading