Ground item encoder + decoder

This commit is contained in:
woahscam 2021-10-29 17:05:31 -04:00
parent df29d908d7
commit 45fbb3df79
8 changed files with 140 additions and 34 deletions

View file

@ -6,6 +6,7 @@ import cacheops.cache.definition.decoder.*
import cacheops.cache.definition.encoder.NPCSpawnEncoder
import com.displee.cache.CacheLibrary
import com.formdev.flatlaf.FlatDarculaLaf
import const.Image.RED_DOT
import const.Image.YELLOW_DOT
import java.awt.*
import java.awt.event.KeyEvent
@ -18,8 +19,9 @@ import kotlin.system.exitProcess
object Rs2MapEditor {
val library = CacheLibrary.create("K:\\RSPSDev\\2-009Scape-578\\Server\\data\\cache")
var region = 12853
var region = 12850
var npcs = NPCSpawnParser.parseNPCSpawns(region)
var items = GroundItemSpawnParser.parseItemSpawns(region)
val npcRows = ArrayList<NPCSpawnPanel.NPCRow>()
lateinit var mapPanel: MapPane
lateinit var npcPanel: NPCSpawnPanel
@ -133,6 +135,11 @@ object Rs2MapEditor {
mapCell.layout = BorderLayout()
mapCell.add(JLabel(YELLOW_DOT))
}
val itemSpawnsHere = items.filter { it.x == column && it.y == flippedY && it.plane == 0 }.toList()
if (itemSpawnsHere.isNotEmpty()) {
mapCell.layout = BorderLayout()
mapCell.add(JLabel(RED_DOT))
}
componentPointMap[point] = mapCell
add(mapCell, gbc)
}

View file

@ -0,0 +1,53 @@
package cacheops.cache.definition.decoder
import Rs2MapEditor
import cacheops.cache.definition.data.ItemDefinition
import const.cache
import ext.unsignedShort
import java.nio.ByteBuffer
val itemDecoder = ItemDecoder(cache)
class GroundItemDecoder {
fun decode(data: ByteArray): ArrayList<Item> {
val buffer = ByteBuffer.wrap(data)
val list = ArrayList<Item>()
while (buffer.hasRemaining()) {
val coords = buffer.unsignedShort()
val lz = coords shr 14
val lx = (coords shr 7) and 0x3f
val ly = coords and 0x3f
val itemID = buffer.unsignedShort()
val respawnTime = buffer.unsignedShort()
val amount = buffer.int
list.add(Item(itemID, lx, ly, lz, respawnTime, amount))
}
return list
}
}
object GroundItemSpawnParser {
fun parseItemSpawns(regionId: Int): ArrayList<Item> {
val x = (regionId shr 8) and 0xFF
val y = regionId and 0xFF
val groundItemData = Rs2MapEditor.library.data(5, "i${x}_${y}")
if (groundItemData != null && groundItemData.isNotEmpty()) {
return GroundItemDecoder().decode(groundItemData)
}
return ArrayList()
}
}
class Item(val id: Int, val x: Int, val y: Int, val plane: Int, val respawnTime: Int, val amount: Int) {
val definition: ItemDefinition = itemDecoder.forId(id)
}

View file

@ -8,12 +8,27 @@ import const.Indices
class ItemDecoder(cache: Cache) : DefinitionDecoder<ItemDefinition>(cache, Indices.CONFIGURATION_ITEMS) {
companion object {
val DEFINITIONS = HashMap<Int, ItemDefinition>()
}
override fun create() = ItemDefinition()
override fun getFile(id: Int) = id and 0xff
override fun getArchive(id: Int) = id ushr 8
override fun readData(id: Int): ItemDefinition? {
return super.readData(id)
}
fun forId(id : Int): ItemDefinition {
if(DEFINITIONS[id] != null) return DEFINITIONS[id]!!
val def = readData(id)
DEFINITIONS[id] = def!!
return def
}
override fun ItemDefinition.read(opcode: Int, buffer: Reader) {
when (opcode) {
1 -> modelId = buffer.readShort()

View file

@ -2,7 +2,6 @@ package cacheops.cache.definition.decoder
import Rs2MapEditor
import cacheops.cache.definition.data.NPCDefinition
import const.Image
import const.cache
import ext.unsignedShort
import java.nio.ByteBuffer

View file

@ -0,0 +1,45 @@
package cacheops.cache.definition.encoder
import Rs2MapEditor
import cacheops.cache.definition.decoder.Item
import const.Indices
import java.nio.ByteBuffer
object GroundItemEncoder {
fun write(item: ArrayList<Item>) {
// Setup buffer | Determine capacity (ArrayList size * (short2 + short2 + short2 + int4))
val buffer = ByteBuffer.allocate(item.size * 10)
// Put data to buffer
item.forEach {
val formattedCoord = ((it.y or (it.x shl 7)) or (it.plane shl 14))
buffer.putShort(formattedCoord.toShort())
buffer.putShort(it.id.toShort())
buffer.putShort(it.respawnTime.toShort())
buffer.putInt(it.amount)
}
// Convert buffer to byte array
val dataArray = buffer.array()
// Get region information
val region = Rs2MapEditor.region
val x = (region shr 8) and 0xFF
val y = region and 0xFF
// Put region data
if (dataArray != null) {
Rs2MapEditor.library.put(Indices.LANDSCAPES, "i${x}_${y}", dataArray, intArrayOf(0,0,0,0))
Rs2MapEditor.library.update()
} else {
System.err.println("Panic! Something went VERY wrong!")
}
}
}
fun main() {
val itemList = arrayListOf<Item>(Item(946, 24, 2,0, 6, 1))
GroundItemEncoder.write(itemList)
}

View file

@ -1,11 +1,14 @@
package cacheops.cache.definition.encoder
import Rs2MapEditor
import cacheops.cache.definition.data.MapDefinition
import cacheops.cache.definition.decoder.MapTileParser
import const.Indices
import java.nio.ByteBuffer
class MapTileEncoder {
object MapTileEncoder {
fun write(definition: MapDefinition): ByteArray {
fun write(definition: MapDefinition) {
val buffer = ByteBuffer.allocate(80000)
for (plane in 0 until 4) {
for (localX in 0 until 64) {
@ -30,6 +33,17 @@ class MapTileEncoder {
}
}
}
return buffer.array().copyOf(buffer.position())
val data = buffer.array().copyOf(buffer.position())
val combinedData = data.plus(MapTileParser.atmosphereDataTail)
val x = (Rs2MapEditor.region shr 8) and 0xFF
val y = Rs2MapEditor.region and 0xFF
if (combinedData != null) {
Rs2MapEditor.library.put(Indices.LANDSCAPES, "m${x}_${y}", combinedData)
Rs2MapEditor.library.update()
} else {
System.err.println("Something went VERY wrong!")
}
}
}

View file

@ -1,25 +0,0 @@
package cacheops.cache.definition.encoder
import Rs2MapEditor
object MapTileWriter {
fun replaceValues() {
val region = Rs2MapEditor.region
val baseX = ((region shr 8) shl 6)
val baseY = ((region and 0xFF) shl 6)
val x = (region shr 8) and 0xFF
val y = region and 0xFF
// val data = MapTileEncoder().write(MapTileParser.definition)
// val combinedData = data.plus(MapTileParser.atmosphereDataTail)
//
// if (data != null) {
// Rs2MapEditor.library.put(Indices.LANDSCAPES, "m${x}_${y}", combinedData)
// Rs2MapEditor.library.update()
// } else {
// System.err.println("Something went VERY wrong!")
// }
}
}

View file

@ -8,7 +8,7 @@ import java.nio.ByteBuffer
object NPCSpawnEncoder {
fun write(npc: ArrayList<NPC>) {
val buffer = ByteBuffer.allocate(10000)
val buffer = ByteBuffer.allocate(npc.size * 4)
npc.forEach {
val formattedCoord = ((it.y or (it.x shl 7)) or (it.plane shl 14))
@ -16,11 +16,9 @@ object NPCSpawnEncoder {
buffer.putShort(it.id.toShort())
}
val dataArray = buffer.array().copyOf(buffer.position())
val dataArray = buffer.array()
val region = Rs2MapEditor.region
val baseX = ((region shr 8) shl 6)
val baseY = ((region and 0xFF) shl 6)
val x = (region shr 8) and 0xFF
val y = region and 0xFF