Created
September 3, 2024 16:14
-
-
Save PhoenixIllusion/8fcae384dd40c2ff87df74a135fca4ea to your computer and use it in GitHub Desktop.
Godot: Parse the packed data of a GridMap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const data = new Uint32Array([65531, 65528, 0, 65531, 65529, 0, 65531, 65530, 0, 65531, 65531, 0, 65531, 65532, 0, 65531, 65533, 0, 65531, 65534, 0, 65531, 65535, 0, 65531, 0, 0, 65531, 1, 0, 65531, 2, 0, 65531, 3, 0, 65531, 4, 0, 65531, 5, 0, 65531, 6, 0, 65532, 65528, 0, 65532, 65529, 0, 65532, 65530, 0, 65532, 65531, 0, 65532, 65532, 0, 65532, 65533, 0, 65532, 65534, 0, 65532, 65535, 0, 65532, 0, 0, 65532, 1, 0, 65532, 2, 0, 65532, 3, 0, 65532, 4, 0, 65532, 5, 0, 65532, 6, 0, 65533, 65528, 0, 65533, 65529, 0, 65533, 65530, 0, 65533, 65531, 0, 65533, 65532, 0, 65533, 65533, 0, 65533, 65534, 0, 65533, 65535, 0, 65533, 0, 0, 65533, 1, 0, 65533, 2, 0, 65533, 3, 0, 65533, 4, 0, 65533, 5, 0, 65533, 6, 0, 65534, 65528, 0, 65534, 65529, 0, 65534, 65530, 0, 65534, 65531, 0, 65534, 65532, 0, 65534, 65533, 0, 65534, 65534, 0, 65534, 65535, 0, 65534, 0, 0, 65534, 1, 0, 65534, 2, 0, 65534, 3, 0, 65534, 4, 0, 65534, 5, 0, 65534, 6, 0, 65535, 65528, 0, 65535, 65529, 0, 65535, 65530, 0, 65535, 65531, 0, 65535, 65532, 0, 65535, 65533, 0, 65535, 65534, 0, 65535, 65535, 0, 65535, 0, 0, 65535, 1, 0, 65535, 2, 0, 65535, 3, 0, 65535, 4, 0, 65535, 5, 0, 65535, 6, 0, 0, 65528, 0, 0, 65529, 0, 0, 65530, 0, 0, 65531, 0, 0, 65532, 0, 0, 65533, 0, 0, 65534, 0, 0, 65535, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 3, 0, 0, 4, 0, 0, 5, 0, 0, 6, 0, 1, 65528, 0, 1, 65529, 0, 1, 65530, 0, 1, 65531, 0, 1, 65532, 0, 1, 65533, 0, 1, 65534, 0, 1, 65535, 0, 1, 0, 0, 1, 1, 0, 1, 2, 0, 1, 3, 0, 1, 4, 0, 1, 5, 0, 1, 6, 0, 2, 65528, 0, 2, 65529, 0, 2, 65530, 0, 2, 65531, 0, 2, 65532, 0, 2, 65533, 0, 2, 65534, 0, 2, 65535, 0, 2, 0, 0, 2, 1, 0, 2, 2, 0, 2, 3, 0, 2, 4, 0, 2, 5, 0, 2, 6, 0, 3, 65528, 0, 3, 65529, 0, 3, 65530, 0, 3, 65531, 0, 3, 65532, 0, 3, 65533, 0, 3, 65534, 0, 3, 65535, 0, 3, 0, 0, 3, 1, 0, 3, 2, 0, 3, 3, 0, 3, 4, 0, 3, 5, 0, 3, 6, 0, 4, 65528, 0, 4, 65529, 0, 4, 65530, 0, 4, 65531, 0, 4, 65532, 0, 4, 65533, 0, 4, 65534, 0, 4, 65535, 0, 4, 0, 0, 4, 1, 0, 4, 2, 0, 4, 3, 0, 4, 4, 0, 4, 5, 0, 4, 6, 0, 5, 65528, 0, 5, 65529, 0, 5, 65530, 0, 5, 65531, 0, 5, 65532, 0, 5, 65533, 0, 5, 65534, 0, 5, 65535, 0, 5, 0, 0, 5, 1, 0, 5, 2, 0, 5, 3, 0, 5, 4, 0, 5, 5, 0, 5, 6, 0, 6, 65528, 0, 6, 65529, 0, 6, 65530, 0, 6, 65531, 0, 6, 65532, 0, 6, 65533, 0, 6, 65534, 0, 6, 65535, 0, 6, 0, 0, 6, 1, 0, 6, 2, 0, 6, 3, 0, 6, 4, 0, 6, 5, 0, 6, 6, 0, 7, 65528, 1441796, 7, 65529, 1441796, 7, 65530, 1441796, 7, 65531, 1441796, 7, 65532, 1441796, 7, 65533, 1441796, 7, 65534, 1441796, 7, 65535, 1441796, 7, 0, 1441796, 7, 1, 1441796, 7, 2, 1441796, 7, 3, 1441796, 7, 4, 1441796, 7, 5, 1441796, 7, 6, 1441796, 65532, 7, 4, 65533, 7, 4, 65534, 7, 4, 65535, 7, 4, 0, 7, 4, 1, 7, 4, 2, 7, 4, 3, 7, 4, 4, 7, 4, 5, 7, 4, 6, 7, 4, 65530, 65528, 1441796, 65530, 65529, 1441796, 65530, 65530, 1441796, 65530, 65531, 1441796, 65530, 65532, 1441796, 65530, 65533, 1441796, 65530, 65534, 1441796, 65530, 65535, 1441796, 65530, 0, 1441796, 65530, 1, 1441796, 65530, 2, 1441796, 65530, 3, 1441796, 65530, 4, 1441796, 65530, 5, 1441796, 65530, 6, 1441796, 65531, 7, 4, 65531, 65527, 4, 65532, 65527, 4, 65533, 65527, 4, 65534, 65527, 4, 65535, 65527, 4, 0, 65527, 4, 1, 65527, 4, 2, 65527, 4, 3, 65527, 4, 4, 65527, 4, 5, 65527, 4, 6, 65527, 4, 7, 7, 1048579, 65530, 7, 3, 7, 65527, 655363, 65530, 65527, 1441795, 131079, 2, 655363, 65543, 5, 655363]); | |
const dataView = new DataView(data.buffer); | |
/** | |
Parses the Key of a GridView element | |
@param {number} idx | |
@param {DataView} dataView | |
*/ | |
function parseKey(idx, dataView) { | |
return { | |
x: dataView.getInt16(idx, true), | |
y: dataView.getInt16(idx + 2, true), | |
z: dataView.getInt16(idx + 4, true), | |
} | |
} | |
/** | |
Parses the Key of a GridView element | |
@param {number} idx | |
@param {DataView} dataView | |
*/ | |
function parseCell(idx, dataView) { | |
const part = dataView.getUint16(idx + 2, true); | |
return { | |
item: dataView.getUint16(idx, true), | |
rot: part & 0x1F, | |
layer: part >> 5 | |
} | |
} | |
for(let i=0; i< dataView.byteLength / 12; i+= 1) { | |
const key = parseKey(i * 12, dataView); | |
const cell = parseCell(i * 12 + 8, dataView); | |
console.log({key, cell}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment