Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "os"
- "replayReader"
- "fmt"
- "image/color"
- "image"
- "image/png"
- )
- var basePalette = []color.Color{
- color.RGBA{0, 0, 0, 0},
- color.RGBA{127, 178, 56, 255},
- color.RGBA{247, 233, 163, 255},
- color.RGBA{199, 199, 199, 255},
- color.RGBA{255, 0, 0, 255},
- color.RGBA{160, 160, 255, 255},
- color.RGBA{167, 167, 167, 255},
- color.RGBA{0, 124, 0, 255},
- color.RGBA{255, 255, 255, 255},
- color.RGBA{164, 168, 184, 255},
- color.RGBA{151, 109, 77, 255},
- color.RGBA{112, 112, 112, 255},
- color.RGBA{64, 64, 255, 255},
- color.RGBA{143, 119, 72, 255},
- color.RGBA{255, 252, 245, 255},
- color.RGBA{216, 127, 51, 255},
- color.RGBA{178, 76, 216, 255},
- color.RGBA{102, 153, 216, 255},
- color.RGBA{229, 229, 51, 255},
- color.RGBA{127, 204, 25, 255},
- color.RGBA{242, 127, 165, 255},
- color.RGBA{76, 76, 76, 255},
- color.RGBA{153, 153, 153, 255},
- color.RGBA{76, 127, 153, 255},
- color.RGBA{127, 63, 178, 255},
- color.RGBA{51, 76, 178, 255},
- color.RGBA{102, 76, 51, 255},
- color.RGBA{102, 127, 51, 255},
- color.RGBA{153, 51, 51, 255},
- color.RGBA{25, 25, 25, 255},
- color.RGBA{250, 238, 77, 255},
- color.RGBA{92, 219, 213, 255},
- color.RGBA{74, 128, 255, 255},
- color.RGBA{0, 217, 58, 255},
- color.RGBA{129, 86, 49, 255},
- color.RGBA{112, 2, 0, 255},
- color.RGBA{209, 177, 161, 255},
- color.RGBA{159, 82, 36, 255},
- color.RGBA{149, 87, 108, 255},
- color.RGBA{112, 108, 138, 255},
- color.RGBA{186, 133, 36, 255},
- color.RGBA{103, 117, 53, 255},
- color.RGBA{160, 77, 78, 255},
- color.RGBA{57, 41, 35, 255},
- color.RGBA{135, 107, 98, 255},
- color.RGBA{87, 92, 92, 255},
- color.RGBA{122, 73, 88, 255},
- color.RGBA{76, 62, 92, 255},
- color.RGBA{76, 50, 35, 255},
- color.RGBA{76, 82, 42, 255},
- color.RGBA{142, 60, 46, 255},
- color.RGBA{37, 22, 16, 255},
- }
- var shadePalette = []float32{
- 180,
- 220,
- 255,
- 135,
- }
- func paletteIDtoColor(mapColor byte)(color.Color){
- basePaletteID := mapColor/4
- shadePaletteID := mapColor%4
- basePaletteColor := basePalette[basePaletteID]
- shadePaletteFactor := shadePalette[shadePaletteID]
- r, g, b, a := basePaletteColor.RGBA()
- rFloat := float32(uint8(r))
- gFloat := float32(uint8(g))
- bFloat := float32(uint8(b))
- rFloat = rFloat * shadePaletteFactor / 255
- gFloat = gFloat * shadePaletteFactor / 255
- bFloat = bFloat * shadePaletteFactor / 255
- return color.RGBA{uint8(rFloat),uint8(gFloat),uint8(bFloat),uint8(a)}
- }
- func main() {
- //Open output image file
- imageFile, err := os.Create("output.png")
- if err != nil {
- panic(err)
- }
- //Create output image
- bounds := image.Rect(0, 0, 128, 128)
- outputImage := image.NewNRGBA(bounds)
- //Open TMCPR file
- f, err := os.Open("recording.tmcpr")
- if err != nil {
- panic(err)
- }
- replay := replayReader.NewReplay(f)
- packet := &replayReader.Packet{}
- for replay.Next(packet) {
- //First byte of the packet is (probably) always the Packet ID
- packetType, err := packet.ReaduByte()
- if err != nil {
- fmt.Printf("Couldn't read packetType. Reason: %s. Reading next packet.\n", err)
- continue
- }
- // Packet #0x24 is "Map"
- // You can find the structure of the "Map" packet at http://wiki.vg/Protocol#Map
- if packetType == 0x24 {
- fmt.Println("This packet is a Map packet")
- //First element of "Map" is a VarInt containing the damage of the map item
- damage, _, err := packet.ReadVarInt()
- if err != nil {
- fmt.Printf("Couldn't read damage. Reason: %s. Reading next packet.\n", err)
- continue
- }
- fmt.Printf(" Damage: %d.\n", damage)
- //Some data we don't care about. We'll use seek
- //Scale: byte. 1 bytes
- //Tracking Position: bool. 1 bytes
- packet.Seek(2, 1)
- //The 1 at the end of the command means "Relative to cursor"
- //We don't care bout the Icons either, but they are variable length, so we have to atleast take the count of them
- iconCount,_ , err := packet.ReadVarInt()
- if err != nil {
- fmt.Printf("Couldn't read iconCount. Reason: %s. Reading next packet.\n", err)
- continue
- }
- //Each icon contains 3 bytes. Direction/Type, X, Y
- packet.Seek(int64(iconCount*3), 1)
- //Number of updated columns
- cols, err := packet.ReaduByte()
- if err != nil {
- fmt.Printf("Couldn't read cols. Reason: %s. Reading next packet.\n", err)
- continue
- }
- //Other stuff is Optional, they only exist if the number of columns is larger than zero
- if cols > 0 {
- //Number of updated Rows
- rows, err := packet.ReaduByte()
- if err != nil {
- fmt.Printf("Couldn't read rows. Reason: %s. Reading next packet.\n", err)
- continue
- }
- //X position of the changed segment
- x, err := packet.ReaduByte()
- if err != nil {
- fmt.Printf("Couldn't read x. Reason: %s. Reading next packet.\n", err)
- continue
- }
- //Y position of the changed segment
- y, err := packet.ReaduByte()
- if err != nil {
- fmt.Printf("Couldn't read y. Reason: %s. Reading next packet.\n", err)
- continue
- }
- fmt.Printf(" It changed a %dx%d area, at X: %d, Y: %d.\n", cols, rows, x, y)
- //Size of data byte array
- dataSize, _, err := packet.ReadVarInt()
- if err != nil {
- fmt.Printf("Couldn't read dataSize. Reason: %s. Reading next packet.\n", err)
- continue
- }
- //Reading data byte array
- data, _, err := packet.ReaduByteArray(dataSize)
- //Looping through every pixel that the packet changed
- for i := 0; i < dataSize; i++ {
- //Converting i to image coordinates
- imageX := i%int(cols) + int(x)
- imageY := i/int(cols) + int(y)
- //Setting pixel on image
- outputImage.Set(imageX, imageY, paletteIDtoColor(data[i]))
- }
- }
- }
- }
- err = replay.Error()
- if err != nil {
- panic(err)
- }
- err = png.Encode(imageFile, outputImage)
- if err != nil {
- panic(err)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment