bela333

Replay To Map

Sep 14th, 2017
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 5.80 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "os"
  5.     "replayReader"
  6.     "fmt"
  7.     "image/color"
  8.     "image"
  9.     "image/png"
  10. )
  11.  
  12. var basePalette = []color.Color{
  13.     color.RGBA{0, 0, 0, 0},
  14.     color.RGBA{127, 178, 56, 255},
  15.     color.RGBA{247, 233, 163, 255},
  16.     color.RGBA{199, 199, 199, 255},
  17.     color.RGBA{255, 0, 0, 255},
  18.     color.RGBA{160, 160, 255, 255},
  19.     color.RGBA{167, 167, 167, 255},
  20.     color.RGBA{0, 124, 0, 255},
  21.     color.RGBA{255, 255, 255, 255},
  22.     color.RGBA{164, 168, 184, 255},
  23.     color.RGBA{151, 109, 77, 255},
  24.     color.RGBA{112, 112, 112, 255},
  25.     color.RGBA{64, 64, 255, 255},
  26.     color.RGBA{143, 119, 72, 255},
  27.     color.RGBA{255, 252, 245, 255},
  28.     color.RGBA{216, 127, 51, 255},
  29.     color.RGBA{178, 76, 216, 255},
  30.     color.RGBA{102, 153, 216, 255},
  31.     color.RGBA{229, 229, 51, 255},
  32.     color.RGBA{127, 204, 25, 255},
  33.     color.RGBA{242, 127, 165, 255},
  34.     color.RGBA{76, 76, 76, 255},
  35.     color.RGBA{153, 153, 153, 255},
  36.     color.RGBA{76, 127, 153, 255},
  37.     color.RGBA{127, 63, 178, 255},
  38.     color.RGBA{51, 76, 178, 255},
  39.     color.RGBA{102, 76, 51, 255},
  40.     color.RGBA{102, 127, 51, 255},
  41.     color.RGBA{153, 51, 51, 255},
  42.     color.RGBA{25, 25, 25, 255},
  43.     color.RGBA{250, 238, 77, 255},
  44.     color.RGBA{92, 219, 213, 255},
  45.     color.RGBA{74, 128, 255, 255},
  46.     color.RGBA{0, 217, 58, 255},
  47.     color.RGBA{129, 86, 49, 255},
  48.     color.RGBA{112, 2, 0, 255},
  49.     color.RGBA{209, 177, 161, 255},
  50.     color.RGBA{159, 82, 36, 255},
  51.     color.RGBA{149, 87, 108, 255},
  52.     color.RGBA{112, 108, 138, 255},
  53.     color.RGBA{186, 133, 36, 255},
  54.     color.RGBA{103, 117, 53, 255},
  55.     color.RGBA{160, 77, 78, 255},
  56.     color.RGBA{57, 41, 35, 255},
  57.     color.RGBA{135, 107, 98, 255},
  58.     color.RGBA{87, 92, 92, 255},
  59.     color.RGBA{122, 73, 88, 255},
  60.     color.RGBA{76, 62, 92, 255},
  61.     color.RGBA{76, 50, 35, 255},
  62.     color.RGBA{76, 82, 42, 255},
  63.     color.RGBA{142, 60, 46, 255},
  64.     color.RGBA{37, 22, 16, 255},
  65. }
  66.  
  67. var shadePalette = []float32{
  68.     180,
  69.     220,
  70.     255,
  71.     135,
  72. }
  73.  
  74. func paletteIDtoColor(mapColor byte)(color.Color){
  75.     basePaletteID := mapColor/4
  76.     shadePaletteID := mapColor%4
  77.  
  78.     basePaletteColor := basePalette[basePaletteID]
  79.     shadePaletteFactor := shadePalette[shadePaletteID]
  80.  
  81.  
  82.     r, g, b, a := basePaletteColor.RGBA()
  83.     rFloat := float32(uint8(r))
  84.     gFloat := float32(uint8(g))
  85.     bFloat := float32(uint8(b))
  86.  
  87.     rFloat = rFloat * shadePaletteFactor / 255
  88.     gFloat = gFloat * shadePaletteFactor / 255
  89.     bFloat = bFloat * shadePaletteFactor / 255
  90.  
  91.     return color.RGBA{uint8(rFloat),uint8(gFloat),uint8(bFloat),uint8(a)}
  92.  
  93. }
  94.  
  95. func main() {
  96.     //Open output image file
  97.     imageFile, err := os.Create("output.png")
  98.     if err != nil {
  99.         panic(err)
  100.     }
  101.  
  102.     //Create output image
  103.     bounds := image.Rect(0, 0, 128, 128)
  104.     outputImage := image.NewNRGBA(bounds)
  105.  
  106.     //Open TMCPR file
  107.     f, err := os.Open("recording.tmcpr")
  108.     if err != nil {
  109.         panic(err)
  110.     }
  111.  
  112.     replay := replayReader.NewReplay(f)
  113.  
  114.     packet := &replayReader.Packet{}
  115.  
  116.     for replay.Next(packet) {
  117.  
  118.         //First byte of the packet is (probably) always the Packet ID
  119.  
  120.         packetType, err := packet.ReaduByte()
  121.         if err != nil {
  122.             fmt.Printf("Couldn't read packetType. Reason: %s. Reading next packet.\n", err)
  123.             continue
  124.         }
  125.         // Packet #0x24 is "Map"
  126.         // You can find the structure of the "Map" packet at http://wiki.vg/Protocol#Map
  127.         if packetType == 0x24 {
  128.             fmt.Println("This packet is a Map packet")
  129.  
  130.  
  131.             //First element of "Map" is a VarInt containing the damage of the map item
  132.             damage, _, err := packet.ReadVarInt()
  133.             if err != nil {
  134.                 fmt.Printf("Couldn't read damage. Reason: %s. Reading next packet.\n", err)
  135.                 continue
  136.             }
  137.             fmt.Printf("    Damage: %d.\n", damage)
  138.  
  139.             //Some data we don't care about. We'll use seek
  140.             //Scale:                byte.   1 bytes
  141.             //Tracking Position:    bool.   1 bytes
  142.  
  143.             packet.Seek(2, 1)
  144.             //The 1 at the end of the command means "Relative to cursor"
  145.  
  146.             //We don't care bout the Icons either, but they are variable length, so we have to atleast take the count of them
  147.             iconCount,_ , err := packet.ReadVarInt()
  148.             if err != nil {
  149.                 fmt.Printf("Couldn't read iconCount. Reason: %s. Reading next packet.\n", err)
  150.                 continue
  151.             }
  152.  
  153.             //Each icon contains 3 bytes. Direction/Type, X, Y
  154.             packet.Seek(int64(iconCount*3), 1)
  155.  
  156.             //Number of updated columns
  157.             cols, err := packet.ReaduByte()
  158.             if err != nil {
  159.                 fmt.Printf("Couldn't read cols. Reason: %s. Reading next packet.\n", err)
  160.                 continue
  161.             }
  162.  
  163.             //Other stuff is Optional, they only exist if the number of columns is larger than zero
  164.             if cols > 0 {
  165.                 //Number of updated Rows
  166.                 rows, err := packet.ReaduByte()
  167.                 if err != nil {
  168.                     fmt.Printf("Couldn't read rows. Reason: %s. Reading next packet.\n", err)
  169.                     continue
  170.                 }
  171.                 //X position of the changed segment
  172.                 x, err := packet.ReaduByte()
  173.                 if err != nil {
  174.                     fmt.Printf("Couldn't read x. Reason: %s. Reading next packet.\n", err)
  175.                     continue
  176.                 }
  177.                 //Y position of the changed segment
  178.                 y, err := packet.ReaduByte()
  179.                 if err != nil {
  180.                     fmt.Printf("Couldn't read y. Reason: %s. Reading next packet.\n", err)
  181.                     continue
  182.                 }
  183.                 fmt.Printf("    It changed a %dx%d area, at X: %d, Y: %d.\n", cols, rows, x, y)
  184.  
  185.                 //Size of data byte array
  186.                 dataSize, _, err := packet.ReadVarInt()
  187.                 if err != nil {
  188.                     fmt.Printf("Couldn't read dataSize. Reason: %s. Reading next packet.\n", err)
  189.                     continue
  190.                 }
  191.  
  192.                 //Reading data byte array
  193.                 data, _, err := packet.ReaduByteArray(dataSize)
  194.  
  195.                 //Looping through every pixel that the packet changed
  196.  
  197.                 for i := 0; i < dataSize; i++ {
  198.                     //Converting i to image coordinates
  199.                     imageX := i%int(cols) + int(x)
  200.                     imageY := i/int(cols) + int(y)
  201.  
  202.                     //Setting pixel on image
  203.                     outputImage.Set(imageX, imageY, paletteIDtoColor(data[i]))
  204.                 }
  205.             }
  206.  
  207.         }
  208.  
  209.     }
  210.  
  211.     err = replay.Error()
  212.  
  213.     if err != nil {
  214.         panic(err)
  215.     }
  216.  
  217.     err = png.Encode(imageFile, outputImage)
  218.     if err != nil {
  219.         panic(err)
  220.     }
  221.  
  222. }
Advertisement
Add Comment
Please, Sign In to add comment