Skip to content

Instantly share code, notes, and snippets.

View ultraviolet-jordan's full-sized avatar

Jordan ultraviolet-jordan

  • United States
View GitHub Profile
import java.nio.ByteBuffer
import kotlin.math.abs
fun main() {
val expectedInt = 420
val buffer = ByteBuffer.allocate(3)
buffer.putAsVl64(expectedInt)
buffer.flip()
val readInt = buffer.getAsVl64()
println("Matches? ${expectedInt == readInt}")
class RSByteBuffer(
private val buffer: ByteBuffer
) {
constructor(array: ByteArray) : this(ByteBuffer.wrap(array))
private var accessBitsIndex = -1
fun readByte(): Int {
checkAccessingBytes()
return buffer.get().toInt()
class PlayerAppearanceBlockBuilder : RenderBlockBuilder<Appearance>(
index = 1,
mask = 0x4
) {
private val builders = arrayOf(
HeadBuilder(),
BackBuilder(),
NeckBuilder(),
MainHandBuilder(),
TorsoBuilder(),
@Singleton
class PlayerInfoPacketBuilder @Inject constructor(
private val updateBlocks: PlayerUpdateBlocks
) : PacketBuilder<PlayerInfoPacket>(
opcode = 3,
size = -2
) {
override fun build(packet: PlayerInfoPacket, buffer: RSByteBuffer) {
val players = packet.players
val viewport = packet.viewport
abstract class UpdateBlocks<A : Actor> {
abstract fun buildPendingUpdatesBlocks(actor: A)
abstract fun clear()
fun Array<out RenderBlock<*>?>.calculateMask(comparator: Int): Int = fold(0) { mask, block ->
if (block == null) mask else mask or block.builder.mask
}.let { if (it > 0xFF) it or comparator else it }
fun Array<out RenderBlock<*>?>.calculateSize(mask: Int): Int = fold(0) { size, block ->
class ActorRenderer(
val lowDefinitionRenderBlocks: Array<LowDefinitionRenderBlock<*>?> = arrayOfNulls(13),
val highDefinitionRenderBlocks: Array<HighDefinitionRenderBlock<*>?> = arrayOfNulls(13)
) {
fun <T : RenderType> update(type: T): T {
val block = type.toBlock()
highDefinitionRenderBlocks[block.index] = HighDefinitionRenderBlock(type, block)
return type
}
class Viewport(
val player: Player
) {
val nsnFlags = IntArray(MAX_PLAYERS)
val locations = IntArray(MAX_PLAYERS)
val highDefinitions = IntArray(MAX_PLAYERS)
val lowDefinitions = IntArray(MAX_PLAYERS)
val highDefinitionUpdates = ArrayList<Int>()
val lowDefinitionUpdates = ArrayList<Int>()
val players = arrayOfNulls<Player?>(MAX_PLAYERS)
@Singleton
class PlayerUpdateBlocks(
val highDefinitionUpdates: Array<ByteArray?> = arrayOfNulls<ByteArray?>(World.MAX_PLAYERS),
val lowDefinitionUpdates: Array<ByteArray?> = arrayOfNulls<ByteArray?>(World.MAX_PLAYERS)
) : UpdateBlocks<Player>() {
override fun buildPendingUpdatesBlocks(actor: Player) {
if (actor.renderer.hasHighDefinitionUpdate()) {
highDefinitionUpdates[actor.index] = actor.renderer.highDefinitionRenderBlocks.buildHighDefinitionUpdates(actor)
}
// Low definitions are always built here for persisted blocks from previous game cycles. i.e Appearance.
fun main() {
val buffer = ByteBuffer.allocate(100)
buffer.withBitAccess {
pBit(2, 3)
pBit(1, 0)
}
}
inline fun ByteBuffer.withBitAccess(function: ByteBuffer.() -> Unit) {
position(position() * 8)
@JvmInline
value class MapSquareLandTile(
private val packed: Long
) {
constructor(
height: Int,
overlayId: Int,
overlayPath: Int,
overlayRotation: Int,
collision: Int,