Skip to content

Commit e38d242

Browse files
authored
Fix large npc rotation (#1010)
* Fix npc rotation closes #846 * Client connection tweaks to avoid update errors #829 * Fix rotation calc
1 parent aef4261 commit e38d242

6 files changed

Lines changed: 18 additions & 12 deletions

File tree

data/minigame/first_of_guthix/fist_of_guthix.npc-spawns.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ spawns = [
55
{ id = "getorix", x = 1693, y = 5598 },
66
{ id = "pontimer", x = 1696, y = 5600 },
77
{ id = "alran", x = 1699, y = 5599 },
8-
{ id = "fiara", x = 1714, y = 5602, direction = "SOUTH_EAST", members = true },
8+
{ id = "fiara", x = 1714, y = 5602, direction = "SOUTH", members = true },
99
]

engine/src/main/kotlin/world/gregs/voidps/engine/client/update/npc/NPCUpdateTask.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ class NPCUpdateTask(
3030
processAdditions(player, viewport, writer, updates, npcs)
3131
writer.stopBitAccess()
3232

33-
player.client?.updateNPCs(writer, updates)
34-
player.client?.flush()
33+
val client = player.client ?: return
34+
client.updateNPCs(writer, updates)
35+
client.flush()
3536
writer.position(0)
3637
updates.position(0)
3738
}
@@ -128,7 +129,9 @@ class NPCUpdateTask(
128129
sync.writeBits(1, visuals.tele)
129130
sync.writeBits(5, delta.y + if (delta.y < 15) 32 else 0)
130131
sync.writeBits(5, delta.x + if (delta.x < 15) 32 else 0)
131-
sync.writeBits(3, (visuals.face.direction shr 11) - 4)
132+
val roundedSector = ((visuals.face.direction + 1024) and 0x3fff) shr 11
133+
val bitsToWrite = (roundedSector - 4) and 0x7
134+
sync.writeBits(3, bitsToWrite)
132135
sync.writeBits(1, flag != 0)
133136
sync.writeBits(14, npc.def.id)
134137
encodeVisuals(updates, flag, visuals, client.index)

engine/src/main/kotlin/world/gregs/voidps/engine/client/update/player/PlayerUpdateTask.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ class PlayerUpdateTask {
5454
processGlobals(player, writer, updates, players, viewport, true)
5555
processGlobals(player, writer, updates, players, viewport, false)
5656

57-
player.client?.updatePlayers(writer, updates)
58-
player.client?.flush()
57+
val client = player.client ?: return
58+
client.updatePlayers(writer, updates)
59+
client.flush()
5960
writer.position(0)
6061
updates.position(0)
6162
}

engine/src/main/kotlin/world/gregs/voidps/engine/entity/character/Character.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,12 +197,14 @@ interface Character :
197197
* The direction the character is currently facing
198198
*/
199199
val direction: Direction
200-
get() = Direction.of(visuals.face.targetX - tile.x, visuals.face.targetY - tile.y)
200+
get() = Direction.of((visuals.face.targetX - tile.x).coerceIn(-1, 1), (visuals.face.targetY - tile.y).coerceIn(-1, 1))
201201

202202
/**
203203
* Turn to face a [direction]
204204
*/
205-
fun face(direction: Direction, update: Boolean = true) = face(direction.delta, update)
205+
fun face(direction: Direction, update: Boolean = true): Boolean {
206+
return face(Delta(direction.delta.x * 100, direction.delta.y * 100), update)
207+
}
206208

207209
/**
208210
* Turn to face a [tile]

network/src/main/kotlin/world/gregs/voidps/network/client/Client.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ open class Client(
7272
open fun send(opcode: Int, block: suspend ByteWriteChannel.() -> Unit) = send(opcode, -1, FIXED, block)
7373

7474
open fun send(opcode: Int, size: Int, type: Int, block: suspend ByteWriteChannel.() -> Unit) {
75-
if (disconnected) {
75+
if (disconnected || state != ClientState.Connected) {
7676
return
7777
}
7878
runBlocking(handler) {

network/src/main/kotlin/world/gregs/voidps/network/login/protocol/visual/encode/npc/NPCFaceEncoder.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import world.gregs.voidps.network.login.protocol.visual.NPCVisuals
55
import world.gregs.voidps.network.login.protocol.visual.VisualEncoder
66
import world.gregs.voidps.network.login.protocol.visual.VisualMask.NPC_FACE_MASK
77

8-
class NPCFaceEncoder : VisualEncoder<NPCVisuals>(NPC_FACE_MASK, initial = true) {
8+
class NPCFaceEncoder : VisualEncoder<NPCVisuals>(NPC_FACE_MASK) {
99

1010
override fun encode(writer: Writer, visuals: NPCVisuals) {
1111
val (targetX, targetY) = visuals.face
1212
writer.apply {
13-
writeShortAdd(targetX * 2 + 1)
14-
writeShortLittle(targetY * 2 + 1)
13+
writeShortAdd((targetX * 2) + 1)
14+
writeShortLittle((targetY * 2) + 1)
1515
}
1616
}
1717
}

0 commit comments

Comments
 (0)