Skip to content

Commit

Permalink
Use string ids for client scripts closes #442
Browse files Browse the repository at this point in the history
  • Loading branch information
GregHib committed Feb 16, 2024
1 parent cf52fd6 commit d626628
Show file tree
Hide file tree
Showing 23 changed files with 46 additions and 32 deletions.
12 changes: 11 additions & 1 deletion data/definitions/scripts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,14 @@ quest_progress: 2194
activate_prayer: 2295
achievement_has_requirements: 3224
achievement_complete: 3994
close_dialogue: 571
clear_dialogues: 571
primary_options: 150
secondary_options: 695
update_bank_slots: 1465
quest_journal_refresh: 2165
quest_journal_length: 1207
trade_warning: 143
int_entry: 108
string_entry: 109
dialogue_item_zoom: 3449
items_kept_on_death: 118
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package world.gregs.voidps.engine.client

import net.pearx.kasechange.toSnakeCase
import world.gregs.voidps.engine.client.ui.chat.Colours
import world.gregs.voidps.engine.data.definition.ClientScriptDefinitions
import world.gregs.voidps.engine.entity.character.Character
import world.gregs.voidps.engine.entity.character.player.Player
import world.gregs.voidps.engine.entity.character.player.chat.ChatType
import world.gregs.voidps.engine.get
import world.gregs.voidps.network.encode.*
import world.gregs.voidps.type.Tile

Expand Down Expand Up @@ -104,9 +106,12 @@ fun Player.sendRunEnergy(energy: Int) = client?.sendRunEnergy(energy) ?: Unit
* @param params Additional parameters to run the script with (strings & integers only)
*/
fun Player.sendScript(
id: Int,
id: String,
vararg params: Any?
) = sendScript(id, params.toList())
) {
val definition = get<ClientScriptDefinitions>().get(id)
sendScript(definition.id, params.toList())
}

fun Player.sendScript(
id: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class InterfaceOptions(

fun send(id: String, component: String) {
val comp = definitions.getComponent(id, component) ?: return
val script = if (comp["primary", true]) 150 else 695
val script = if (comp["primary", true]) "primary_options" else "secondary_options"
val inventory = inventoryDefinitions.get(comp["inventory", ""])
if (inventory.id != -1) {
val combined = (comp["parent", -1] shl 16) or comp.id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ internal class InterfaceOptionsTest {
every { inventoryDefinitions.get(any<String>()) } returns InventoryDefinition(10, extras = mapOf("width" to 2, "height" to 3))
options.send(name, comp)
verify {
player.sendScript(695, (5 shl 16) or 0, 10, 2, 3, 0, -1, "", "", "", "", "", "", "", "", "")
player.sendScript("secondary_options", (5 shl 16) or 0, 10, 2, 3, 0, -1, "", "", "", "", "", "", "", "", "")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ objectOperate("Collect") {

interfaceClose("bank") { player: Player ->
player.close("bank_side")
player.sendScript(571)
player.sendScript("clear_dialogues")
}

interfaceOpen("bank") { player: Player ->
Expand All @@ -38,7 +38,7 @@ interfaceOpen("bank") { player: Player ->
player.sendVariable("bank_tab_$tab")
}
player.sendVariable("last_bank_amount")
player.sendScript(1465)
player.sendScript("update_bank_slots")
player.interfaceOptions.unlockAll("bank", "inventory", 0 until 516)
player.interfaceOptions.unlockAll("bank_side", "inventory", 0 until 28)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fun Player.quest(name: String): String = this[name, "unstarted"]
fun Player.questComplete(name: String): Boolean = quest(name) == "completed"

fun Player.refreshQuestJournal() {
sendScript(2165)
sendScript("quest_journal_refresh")
}

private const val QUEST_SCROLL_ID = "quest_scroll"
Expand All @@ -19,7 +19,7 @@ fun Player.sendQuestJournal(name: String, lines: List<String>) {
if (!interfaces.open(QUEST_SCROLL_ID)) {
return
}
sendScript(1207, lines.size + 1)
sendScript("quest_journal_length", lines.size + 1)
interfaces.sendText(QUEST_SCROLL_ID, "quest_name", name)
interfaces.sendText(QUEST_SCROLL_ID, "line0", "")
for (i in 0..301) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ interfaceOption("Make *", "make*", "make_mould*") {
}

interfaceClose("make_mould*") { player: Player ->
player.sendScript(571)
player.sendScript("clear_dialogues")
}

fun CharacterContext.make(component: String, amount: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ interfaceOption(component = "*_button", id = "silver_mould") {
}

interfaceClose("silver_mould") { player: Player ->
player.sendScript(571)
player.sendScript("clear_dialogues")
}

fun Player.make(item: Item, amount: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ itemOnObjectOperate("hammer", "anvil*", arrive = false) {
}

interfaceClose("smithing") { player: Player ->
player.sendScript(571)
player.sendScript("clear_dialogues")
}

suspend fun CharacterContext.smith(player: Player, metal: String, type: String, amount: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package world.gregs.voidps.world.command.debug
import world.gregs.voidps.engine.client.message
import world.gregs.voidps.engine.client.sendInterfaceSettings
import world.gregs.voidps.engine.client.sendInventoryItems
import world.gregs.voidps.engine.client.sendScript
import world.gregs.voidps.engine.client.ui.event.adminCommand
import world.gregs.voidps.engine.client.ui.menu.InterfaceOptionSettings.getHash
import world.gregs.voidps.engine.data.definition.InterfaceDefinitions
Expand Down Expand Up @@ -74,7 +73,7 @@ adminCommand("setting") {
adminCommand("script") {
val parts = content.split(" ")
val remainder = parts.subList(1, parts.size).map { it.toIntOrNull() ?: it }.toTypedArray()
player.sendScript(parts[0].toInt(), *remainder)
player.client?.sendScript(parts[0].toInt(), remainder.toList())
}

adminCommand("sendItems") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ interfaceOption("Set prefix", "name", "clan_chat_setup") {
}

interfaceClose("clan_chat_setup") { player: Player ->
player.sendScript(571)
player.sendScript("clear_dialogues")
}

interfaceOption("Disable", "name", "clan_chat_setup") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,5 @@ fun reset(player: Player, other: Player) {
player.loan.moveAll(player.inventory)
player.loan.clear()
player.otherLoan.clear()
player.sendScript(571)
player.sendScript("clear_dialogues")
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fun highlightRemovedSlots(player: Player, other: Player, update: ItemChanged) {
fun Player.warn(id: String, componentId: String, slot: Int) {
val component = interfaceDefinitions.getComponent(id, componentId) ?: return
val inventory = inventoryDefinitions.get(component["inventory", ""])
sendScript(143, (component["parent", -1] shl 16) or component.id, inventory["width", 0.0], inventory["height", 0.0], slot)
sendScript("trade_warning", (component["parent", -1] shl 16) or component.id, inventory["width", 0.0], inventory["height", 0.0], slot)
}

fun updateValue(player: Player, other: Player) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import world.gregs.voidps.engine.client.sendScript
import world.gregs.voidps.engine.entity.character.CharacterContext
import world.gregs.voidps.engine.suspend.dialogue.IntSuspension

private const val INTEGER_ENTRY_SCRIPT = 108

suspend fun CharacterContext.intEntry(text: String): Int {
player.sendScript(INTEGER_ENTRY_SCRIPT, text)
player.sendScript("int_entry", text)
return IntSuspension()
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ import world.gregs.voidps.engine.suspend.dialogue.ContinueSuspension

private const val ITEM_INTERFACE_ID = "dialogue_obj_box"
private const val DOUBLE_ITEM_INTERFACE_ID = "dialogue_double_obj_box"
private const val ITEM_SCRIPT_ID = 3449

suspend fun CharacterContext.item(item: String, zoom: Int, text: String, sprite: Int? = null) {
check(player.open(ITEM_INTERFACE_ID)) { "Unable to open item dialogue for $player" }
player.sendScript(ITEM_SCRIPT_ID, get<ItemDefinitions>().get(item).id, zoom)
player.sendScript("dialogue_item_zoom", get<ItemDefinitions>().get(item).id, zoom)
if (sprite != null) {
player.interfaces.sendSprite(ITEM_INTERFACE_ID, "sprite", sprite)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import world.gregs.voidps.engine.client.sendScript
import world.gregs.voidps.engine.entity.character.CharacterContext
import world.gregs.voidps.engine.suspend.dialogue.StringSuspension

private const val STRING_ENTRY_SCRIPT = 109

suspend fun CharacterContext.stringEntry(text: String): String {
player.sendScript(STRING_ENTRY_SCRIPT, text)
player.sendScript("string_entry", text)
return StringSuspension()
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ interfaceRefresh("items_kept_on_death") { player: Player ->
}

fun Player.updateItemsOnDeath(items: List<Item>, carriedWealth: Int, riskedWealth: Int, familiar: Boolean = false, gravestone: Boolean = false, skull: Boolean = false) {
sendScript(118,
sendScript("items_kept_on_death",
AreaType.Dangerous.ordinal,
items.size.coerceAtMost(4),
items.getOrNull(0)?.def?.id ?: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ interfaceOption("Remove-*", "items", "price_checker") {

interfaceClose("price_checker") { player ->
player.close("price_checker_side")
player.sendScript(571)
player.sendScript("clear_dialogues")
player.offer.moveAll(player.inventory)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ interfaceOption(component = "Tan *", id = "tanner") {
}

interfaceClose("tanner") { player: Player ->
player.sendScript(571)
player.sendScript("clear_dialogues")
}

fun tan(player: Player, type: String, amount: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.BeforeEach
import org.koin.test.mock.declareMock
import world.gregs.voidps.cache.definition.data.ClientScriptDefinition
import world.gregs.voidps.cache.definition.data.FontDefinition
import world.gregs.voidps.cache.definition.data.InterfaceComponentDefinition
import world.gregs.voidps.cache.definition.data.InterfaceDefinition
import world.gregs.voidps.engine.client.ui.Interfaces
import world.gregs.voidps.engine.client.ui.open
import world.gregs.voidps.engine.data.definition.ClientScriptDefinitions
import world.gregs.voidps.engine.data.definition.FontDefinitions
import world.gregs.voidps.engine.data.definition.InterfaceDefinitions
import world.gregs.voidps.engine.entity.character.Character
Expand All @@ -33,6 +35,7 @@ abstract class DialogueTest : KoinMock() {
lateinit var continuation: Continuation<Any>
lateinit var interfaceDefinitions: InterfaceDefinitions
lateinit var fontDefinitions: FontDefinitions
lateinit var clientScriptDefinitions: ClientScriptDefinitions

fun dialogueBlocking(block: suspend CharacterContext.() -> Unit) {
runTest {
Expand All @@ -54,6 +57,7 @@ abstract class DialogueTest : KoinMock() {
player.interfaces = interfaces
interfaceDefinitions = declareMock()
fontDefinitions = declareMock()
clientScriptDefinitions = declareMock()
continuation = object : Continuation<Any> {
override val context: CoroutineContext
get() = UnconfinedTestDispatcher()
Expand All @@ -65,6 +69,7 @@ abstract class DialogueTest : KoinMock() {
override val character: Character = this@DialogueTest.player
override var onCancel: (() -> Unit)? = null
})
every { clientScriptDefinitions.get("string_entry") } returns ClientScriptDefinition(id = 109)
every { player.open(any()) } returns true
every { interfaceDefinitions.get(any<String>()) } returns InterfaceDefinition()
every { interfaceDefinitions.getComponent(any<String>(), any<String>()) } returns InterfaceComponentDefinition()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal class IntEntryTest : DialogueTest() {
}
assertTrue(player.dialogueSuspension is IntSuspension)
verify {
player.sendScript(108, "text")
player.sendScript("int_entry", "text")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ internal class ItemBoxTest : DialogueTest() {
(player.dialogueSuspension as ContinueSuspension).resume()
verify {
player.open("dialogue_obj_box")
player.sendScript(3449, 9009, 650)
player.sendScript("dialogue_item_zoom", 9009, 650)
interfaces.sendSprite("dialogue_obj_box", "sprite", 10)
interfaces.sendText("dialogue_obj_box", "line1", "An item<br>description")
}
Expand Down Expand Up @@ -72,7 +72,7 @@ internal class ItemBoxTest : DialogueTest() {
}
}
coVerify(exactly = 0) {
player.sendScript(3449, 650, 9009)
player.sendScript("dialogue_item_zoom", 650, 9009)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal class StringEntryTest : DialogueTest() {
stringEntry("text")
}
verify {
player.sendScript(109, "text")
player.sendScript("string_entry", "text")
}
}

Expand Down

0 comments on commit d626628

Please sign in to comment.