Skip to content

Instantly share code, notes, and snippets.

@ultraviolet-jordan
Created April 7, 2023 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ultraviolet-jordan/f3fb9e2885edbfaa969a26650c84b3fe to your computer and use it in GitHub Desktop.
Save ultraviolet-jordan/f3fb9e2885edbfaa969a26650c84b3fe to your computer and use it in GitHub Desktop.
@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.
lowDefinitionUpdates[actor.index] = actor.renderer.lowDefinitionRenderBlocks.buildLowDefinitionUpdates()
}
override fun clear() {
lowDefinitionUpdates.fill(null)
highDefinitionUpdates.fill(null)
}
private fun Array<HighDefinitionRenderBlock<*>?>.buildHighDefinitionUpdates(player: Player): ByteArray {
val mask = calculateMask(0x40)
val size = calculateSize(mask)
return RSByteBuffer(ByteBuffer.allocate(size)).also {
it.writeMask(mask)
for (block in this) {
if (block == null) continue
val start = it.position()
block.builder.build(it, block.renderType)
val end = it.position()
player.renderer.setLowDefinitionRenderingBlock(block, it.array().sliceArray(start until end))
}
}.array()
}
private fun Array<LowDefinitionRenderBlock<*>?>.buildLowDefinitionUpdates(): ByteArray {
val mask = calculateMask(0x40)
val size = calculateSize(mask)
return RSByteBuffer(ByteBuffer.allocate(size)).also {
it.writeMask(mask)
for (block in this) {
if (block == null) continue
it.writeBytes(block.bytes)
}
}.array()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment