Skip to content

Instantly share code, notes, and snippets.

@macabeus
Last active August 8, 2021 17:11
Show Gist options
  • Save macabeus/1f95ec45aad3c8b10c919bde02828ec1 to your computer and use it in GitHub Desktop.
Save macabeus/1f95ec45aad3c8b10c919bde02828ec1 to your computer and use it in GitHub Desktop.
const Jimp = require('jimp')
const { drop, splitEvery } = require('ramda')
const binary = require('binary') // fiquei em duvida entre ela e a qunpack
const fs = require('fs')
const data = drop(3, fs.readFileSync('dump/level-1/tilemap'))
// const data = drop(3, fs.readFileSync('dump/level-2/tilemap'))
const globalOam = splitEvery(44, fs.readFileSync('dump/level-1/global-oam'))
const objectsPos = []
let x = 0
for (let i = 0; i < globalOam.length; i += 1) {
const {
posX,
posY,
posXPart2,
posYPart2,
posXPart3,
posYPart3,
// posXPart4,
// posYPart4,
// posXPart5,
// posYPart5,
sprite,
kind,
} = binary.parse(globalOam[i])
.word16lu('posX')
.word16lu('posY')
.word8lu('meta1Part1') // para o bicho simples, é a direção inicial, para o alado, o quanto que ele desce
.word8lu('meta2Part1') // para o alado, 00 -> sobe e volta; 01 -> desce e volta; 02 -> desce e só vai
.skip(2)
.word16lu('posXPart2')
.word16lu('posYPart2')
.word8lu('meta1Part2')
.word8lu('meta2Part2')
.skip(2)
.word16lu('posXPart3')
.word16lu('posYPart3')
.word8lu('meta1Part3')
.word8lu('meta2Part3')
.skip(2)
.word16lu('posXPart4')
.word16lu('posYPart4')
.word8lu('meta1Part4')
.word8lu('meta2Part4')
.skip(2)
.word16lu('posXPart5')
.word16lu('posYPart5')
.word8lu('meta1Part5')
.word8lu('meta2Part5')
.skip(2)
.word8lu('sprite')
.word8lu('kind')
.vars
x += 44
objectsPos.push({
posX,
posY,
posXPart2,
posYPart2,
posXPart3,
posYPart3,
// posXPart4,
// posYPart4,
// posXPart5,
// posYPart5,
sprite,
kind,
buff: globalOam[i],
x,
})
}
const mapKindToColor = {
0x03: 0xFFE80DFF, // star
0x07: 0xFF6ED2FF, // coração
0x2C: 0xB3FF62FF, // dream stone
0x2D: 0x8CFFFAFF, // dream stone large
0x76: 0x438F36FF, // moo
0x77: 0x21722BFF, // flying moo horizontal
0x78: 0x21722BFF, // flying moo verical
}
const getPixelColor = (x, y, hexTile) => {
const xS = x * 8
const yS = y * 8
// OAM
if (objectsPos.filter(i => i.posX === xS).filter(i => i.posY === yS).length > 0) {
const { kind, buff } = objectsPos.filter(i => i.posX === xS).filter(i => i.posY === yS)[0]
const color = mapKindToColor[kind]
if (color) {
return color
}
return 0xD1120799
}
if (objectsPos.filter(i => i.posXPart2 === xS).filter(i => i.posYPart2 === yS).length > 0) {
const { kind, buff } = objectsPos.filter(i => i.posXPart2 === xS).filter(i => i.posYPart2 === yS)[0]
const color = mapKindToColor[kind]
if (color) {
return color
}
}
if (objectsPos.filter(i => i.posXPart3 === xS).filter(i => i.posYPart3 === yS).length > 0) {
const { kind, buff } = objectsPos.filter(i => i.posXPart3 === xS).filter(i => i.posYPart3 === yS)[0]
const color = mapKindToColor[kind]
if (color) {
return color
}
}
// blank
if (hexTile === 0) {
return 0x00000000
}
// blocks
if ([0x7D, 0x80, 0x81, 0x82, 0x8E, 0x8F, 0x90].includes(hexTile)) {
// grass
return 0x007D00FF
}
if ([0x54, 0x53, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5D, 0x60, 0x61, 0x62, 0x63, 0x64, 0x6B, 0x6D, 0x6E, 0x6F, 0x77, 0x7A, 0x7B, 0x7C, 0x7E, 0x7F, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x91, 0xAB, 0xB0, 0xB1, 0xB3, 0xBD].includes(hexTile)) {
// rock
return 0xA03C3FFF
}
if ([0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x48, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x51, 0x5B, 0x5E, 0x95, 0x96, 0x97, 0x9C, 0x9E, 0x9F, 0xA0, 0xA1, 0xA2, 0xA3, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xC2].includes(hexTile)) {
// dark rock
return 0x5A3C3FFF
}
if ([0x52, 0x94, 0x98, 0x99, 0x9A, 0x9B, 0x9D, 0xA4, 0xA5].includes(hexTile)) {
// wood
return 0xBC6F5DFF
}
if ([0x3D].includes(hexTile)) {
// spike
return 0xC81446FF
}
// non-blocks
if ([0x04, 0x05, 0x0B, 0x0C, 0x11, 0x13, 0x14, 0x21].includes(hexTile)) {
// bridgeRope
return 0xC8833F55
}
if ([0x10, 0x12, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x37, 0x38, 0x39, 0x3A].includes(hexTile)) {
// board
return 0xC7C7C755
}
return 0xFFFFFFFF
}
new Jimp(420, 600, (err, image) => {
// new Jimp(300, 600, (err, image) => {
// new Jimp(270, 120, (err, image) => {
let x = 0
let y = 0
for (let i = 0; i < data.length; i += 1) {
const value = data[i]
x += 1
if (x === 420) {
// if (x === 300) {
// if (x === 270) {
y += 1
x = 0
}
let color = getPixelColor(x, y, value)
image.setPixelColor(color, x, y)
}
image.write('image.png')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment