server/dungeon_tilemap.go (4354B)
1 package main 2 3 import ( 4 "bytes" 5 "encoding/binary" 6 "fmt" 7 "image" 8 "image/png" 9 "io" 10 "math" 11 ) 12 13 type dungeonTilemap struct { 14 width, depth int 15 spacing, scale float32 16 tiles, floors, ceilings []byte 17 tilesetHeights []float32 18 } 19 20 func decodeDungeonTilemap(data []byte) (*dungeonTilemap, error) { 21 // BBR 14.9.1 0x100a3dd0/0x100a43f0: dungeon header and tileset records. 22 if len(data) < 26 || string(data[:4]) != "GNDA" || binary.LittleEndian.Uint32(data[4:8]) != uint32(len(data)) || binary.LittleEndian.Uint32(data[8:12]) != 1 { 23 return nil, fmt.Errorf("invalid dungeon tilemap header") 24 } 25 ground := &dungeonTilemap{ 26 width: int(binary.LittleEndian.Uint16(data[12:14])), depth: int(binary.LittleEndian.Uint16(data[14:16])), 27 spacing: math.Float32frombits(binary.LittleEndian.Uint32(data[16:20])), 28 scale: math.Float32frombits(binary.LittleEndian.Uint32(data[20:24])), 29 } 30 count := int(binary.LittleEndian.Uint16(data[24:26])) 31 if ground.width == 0 || ground.depth == 0 || !(ground.spacing > 0 && ground.spacing < math.MaxFloat32) || !(ground.scale > 0 && ground.scale < math.MaxFloat32) || count == 0 || count > 128 || count*2 > len(data)-26 { 32 return nil, fmt.Errorf("invalid dungeon tilemap dimensions or tilesets") 33 } 34 reader := &surfaceReader{Reader: bytes.NewReader(data[26+count*2:])} 35 for range count { 36 var neighbors, extras byte 37 reader.read(&neighbors) 38 reader.read(&extras) 39 length := int(neighbors)*34 + int(extras)*2 40 if reader.err != nil || length > reader.Len() { 41 return nil, fmt.Errorf("truncated dungeon tileset") 42 } 43 reader.Seek(int64(length), io.SeekCurrent) 44 } 45 46 // Chunk lengths include their headers; alignment is relative to each chunk. 47 chunk := func(magic string, version uint32) []byte { 48 var header [12]byte 49 reader.read(&header) 50 length := int(binary.LittleEndian.Uint32(header[4:8])) 51 padded := (length + 3) &^ 3 52 if reader.err != nil || string(header[:4]) != magic || binary.LittleEndian.Uint32(header[8:]) != version || length < 12 || padded-12 > reader.Len() { 53 reader.err = fmt.Errorf("invalid dungeon %s chunk", magic) 54 return nil 55 } 56 payload := make([]byte, length-12) 57 reader.read(payload) 58 reader.Seek(int64(-length&3), io.SeekCurrent) 59 return payload 60 } 61 for index, magic := range []string{"DCGA", "DHGA", "HCDA"} { 62 payload := chunk(magic, 1) 63 if reader.err != nil { 64 return nil, reader.err 65 } 66 grid, err := decodeDungeonGrid(payload, ground.width, ground.depth) 67 if err != nil { 68 return nil, fmt.Errorf("dungeon %s: %w", magic, err) 69 } 70 switch index { 71 case 0: 72 ground.tiles = grid 73 case 1: 74 ground.floors = grid 75 case 2: 76 // BBR 0x100a4870 adds 32 to every ceiling byte, with byte wraparound. 77 for index := range grid { 78 grid[index] += 32 79 } 80 ground.ceilings = grid 81 } 82 } 83 chunk("IRHA", 1) 84 cursor := len(data) - reader.Len() 85 if reader.Len() >= 4 && string(data[cursor:cursor+4]) == "TAHA" { 86 chunk("TAHA", 1) 87 } 88 chunk("XTHA", 2) 89 if reader.err != nil { 90 return nil, reader.err 91 } 92 93 // HSTA uses a payload length, unlike the preceding chunks (BBR 0x100a40a9). 94 var header [12]byte 95 reader.read(&header) 96 if reader.err != nil || string(header[:4]) != "HSTA" || binary.LittleEndian.Uint32(header[4:8]) != uint32(count*2) || binary.LittleEndian.Uint32(header[8:]) != 1 || reader.Len() != count*2 { 97 return nil, fmt.Errorf("invalid dungeon tileset heights") 98 } 99 ground.tilesetHeights = make([]float32, count) 100 for index := range ground.tilesetHeights { 101 var height uint16 102 reader.read(&height) 103 ground.tilesetHeights[index] = float32(height) * .1 104 } 105 for _, tile := range ground.tiles { 106 if int(tile&127) >= count { 107 return nil, fmt.Errorf("invalid dungeon tile type %d", tile&127) 108 } 109 } 110 return ground, nil 111 } 112 113 func decodeDungeonGrid(data []byte, width, depth int) ([]byte, error) { 114 // The three dungeon grids are 8-bit grayscale PNGs, not outdoor height archives. 115 config, err := png.DecodeConfig(bytes.NewReader(data)) 116 if err != nil { 117 return nil, err 118 } 119 if config.Width != width || config.Height != depth { 120 return nil, fmt.Errorf("dungeon grid dimensions do not match header") 121 } 122 decoded, err := png.Decode(bytes.NewReader(data)) 123 if err != nil { 124 return nil, err 125 } 126 grid, ok := decoded.(*image.Gray) 127 if !ok { 128 return nil, fmt.Errorf("dungeon grid is not 8-bit grayscale") 129 } 130 return grid.Pix, nil 131 }