From 664b8416f14c9097e6982fd8f8eb22d4fe542e55 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Wed, 26 Feb 2020 19:46:47 -0800 Subject: [PATCH] Remove global palettes; everything goes through d2asset now (#322) * Configuration cleanup * Cleanup * remove global palettes --- d2common/d2data/d2datadict/palette.go | 28 ----------------- d2common/d2enum/palette_defs.go | 43 --------------------------- d2core/d2asset/animation_manager.go | 4 +-- d2core/d2asset/asset_manager.go | 6 ---- d2core/d2asset/d2asset.go | 6 ++++ d2core/d2map/region.go | 41 +++++++++++++++++++++---- main.go | 40 ------------------------- 7 files changed, 43 insertions(+), 125 deletions(-) delete mode 100644 d2common/d2data/d2datadict/palette.go delete mode 100644 d2common/d2enum/palette_defs.go diff --git a/d2common/d2data/d2datadict/palette.go b/d2common/d2data/d2datadict/palette.go deleted file mode 100644 index 67c8ed25a..000000000 --- a/d2common/d2data/d2datadict/palette.go +++ /dev/null @@ -1,28 +0,0 @@ -package d2datadict - -import ( - "github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum" - "github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2dat" -) - -// PaletteType represents a palette -type PaletteRec struct { - Name d2enum.PaletteType - Colors [256]d2dat.DATColor -} - -var Palettes map[d2enum.PaletteType]PaletteRec - -// CreatePalette creates a palette -func CreatePalette(name d2enum.PaletteType, data []byte) PaletteRec { - palette, _ := d2dat.LoadDAT(data) - return PaletteRec{Name: name, Colors: palette.Colors} -} - -func LoadPalette(paletteType d2enum.PaletteType, file []byte) { - if Palettes == nil { - Palettes = make(map[d2enum.PaletteType]PaletteRec) - } - palette := CreatePalette(paletteType, file) - Palettes[paletteType] = palette -} diff --git a/d2common/d2enum/palette_defs.go b/d2common/d2enum/palette_defs.go deleted file mode 100644 index 805cf1008..000000000 --- a/d2common/d2enum/palette_defs.go +++ /dev/null @@ -1,43 +0,0 @@ -package d2enum - -// PaletteType represents a named palette -type PaletteType string - -const ( - // Act1 palette - Act1 PaletteType = "act1" - // Act2 palette - Act2 PaletteType = "act2" - // Act3 palette - Act3 PaletteType = "act3" - // Act4 palette - Act4 PaletteType = "act4" - // Act5 palette - Act5 PaletteType = "act5" - // EndGame palette - EndGame PaletteType = "endgame" - // EndGame2 palette - EndGame2 PaletteType = "endgame2" - // Fechar palette - Fechar PaletteType = "fechar" - // Loading palette - Loading PaletteType = "loading" - // Menu0 palette - Menu0 PaletteType = "menu0" - // Menu1 palette - Menu1 PaletteType = "menu1" - // Menu2 palette - Menu2 PaletteType = "menu2" - // Menu3 palette - Menu3 PaletteType = "menu3" - // Menu4 palette - Menu4 PaletteType = "menu4" - // Sky palette - Sky PaletteType = "sky" - // Static palette - Static PaletteType = "static" - // Trademark palette - Trademark PaletteType = "trademark" - // Units palette - Units PaletteType = "units" -) diff --git a/d2core/d2asset/animation_manager.go b/d2core/d2asset/animation_manager.go index cb91d027b..61cba0fd3 100644 --- a/d2core/d2asset/animation_manager.go +++ b/d2core/d2asset/animation_manager.go @@ -35,7 +35,7 @@ func (am *animationManager) loadAnimation(animationPath, palettePath string, tra return nil, err } - palette, err := loadPalette(palettePath) + palette, err := LoadPalette(palettePath) if err != nil { return nil, err } @@ -50,7 +50,7 @@ func (am *animationManager) loadAnimation(animationPath, palettePath string, tra return nil, err } - palette, err := loadPalette(palettePath) + palette, err := LoadPalette(palettePath) if err != nil { return nil, err } diff --git a/d2core/d2asset/asset_manager.go b/d2core/d2asset/asset_manager.go index 326fe51fc..093835c0d 100644 --- a/d2core/d2asset/asset_manager.go +++ b/d2core/d2asset/asset_manager.go @@ -4,7 +4,6 @@ import ( "errors" "github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2cof" - "github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2dat" "github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2dc6" "github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2dcc" ) @@ -22,11 +21,6 @@ type assetManager struct { fontManager *fontManager } -func loadPalette(palettePath string) (*d2dat.DATPalette, error) { - verifyWasInit() - return singleton.paletteManager.loadPalette(palettePath) -} - func loadDC6(dc6Path string) (*d2dc6.DC6File, error) { dc6Data, err := LoadFile(dc6Path) if err != nil { diff --git a/d2core/d2asset/d2asset.go b/d2core/d2asset/d2asset.go index d26cb99a1..7751036b7 100644 --- a/d2core/d2asset/d2asset.go +++ b/d2core/d2asset/d2asset.go @@ -4,6 +4,7 @@ import ( "log" "github.com/OpenDiablo2/OpenDiablo2/d2common/d2data/d2datadict" + "github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2dat" "github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2mpq" "github.com/OpenDiablo2/OpenDiablo2/d2core/d2config" @@ -109,6 +110,11 @@ func LoadFont(tablePath, spritePath, palettePath string) (*Font, error) { return singleton.fontManager.loadFont(tablePath, spritePath, palettePath) } +func LoadPalette(palettePath string) (*d2dat.DATPalette, error) { + verifyWasInit() + return singleton.paletteManager.loadPalette(palettePath) +} + func verifyWasInit() { if singleton == nil { panic(ErrNotInit) diff --git a/d2core/d2map/region.go b/d2core/d2map/region.go index c04e3a195..59a0366a3 100644 --- a/d2core/d2map/region.go +++ b/d2core/d2map/region.go @@ -1,15 +1,16 @@ package d2map import ( + "errors" "image/color" "log" "math" "math/rand" - "strconv" "github.com/OpenDiablo2/OpenDiablo2/d2common" "github.com/OpenDiablo2/OpenDiablo2/d2common/d2data/d2datadict" "github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum" + "github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2dat" "github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2ds1" "github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2dt1" "github.com/OpenDiablo2/OpenDiablo2/d2common/d2resource" @@ -81,7 +82,7 @@ type MapRegion struct { levelPreset d2datadict.LevelPresetRecord tiles []d2dt1.Tile ds1 *d2ds1.DS1 - palette d2datadict.PaletteRec + palette *d2dat.DATPalette startX float64 startY float64 imageCacheRecords map[uint32]d2render.Surface @@ -99,10 +100,7 @@ func loadRegion(seed int64, tileOffsetX, tileOffsetY int, levelType d2enum.Regio seed: seed, } - region.palette = d2datadict.Palettes[d2enum.PaletteType("act"+strconv.Itoa(region.levelType.Act))] - if levelType == d2enum.RegionAct5Lava { - region.palette = d2datadict.Palettes[d2enum.PaletteType("act4")] - } + region.palette, _ = loadPaletteForAct(levelType) for _, levelTypeDt1 := range region.levelType.Files { if len(levelTypeDt1) != 0 && levelTypeDt1 != "" && levelTypeDt1 != "0" { @@ -823,6 +821,7 @@ func (mr *MapRegion) decodeTileGfxData(blocks []d2dt1.Block, pixels *[]byte, til colorIndex := block.EncodedData[idx] if colorIndex != 0 { pixelColor := mr.palette.Colors[colorIndex] + offset := 4 * (((blockY + y + tileYOffset) * tileWidth) + (blockX + x)) (*pixels)[offset] = pixelColor.R (*pixels)[offset+1] = pixelColor.G @@ -838,3 +837,33 @@ func (mr *MapRegion) decodeTileGfxData(blocks []d2dt1.Block, pixels *[]byte, til } } } + +func loadPaletteForAct(levelType d2enum.RegionIdType) (*d2dat.DATPalette, error) { + var palettePath string + switch levelType { + case d2enum.RegionAct1Town, d2enum.RegionAct1Wilderness, d2enum.RegionAct1Cave, d2enum.RegionAct1Crypt, + d2enum.RegionAct1Monestary, d2enum.RegionAct1Courtyard, d2enum.RegionAct1Barracks, + d2enum.RegionAct1Jail, d2enum.RegionAct1Cathedral, d2enum.RegionAct1Catacombs, d2enum.RegionAct1Tristram: + palettePath = d2resource.PaletteAct1 + break + case d2enum.RegionAct2Town, d2enum.RegionAct2Sewer, d2enum.RegionAct2Harem, d2enum.RegionAct2Basement, + d2enum.RegionAct2Desert, d2enum.RegionAct2Tomb, d2enum.RegionAct2Lair, d2enum.RegionAct2Arcane: + palettePath = d2resource.PaletteAct2 + break + case d2enum.RegionAct3Town, d2enum.RegionAct3Jungle, d2enum.RegionAct3Kurast, d2enum.RegionAct3Spider, + d2enum.RegionAct3Dungeon, d2enum.RegionAct3Sewer: + palettePath = d2resource.PaletteAct3 + break + case d2enum.RegionAct4Town, d2enum.RegionAct4Mesa, d2enum.RegionAct4Lava, d2enum.RegionAct5Lava: + palettePath = d2resource.PaletteAct4 + break + case d2enum.RegonAct5Town, d2enum.RegionAct5Siege, d2enum.RegionAct5Barricade, d2enum.RegionAct5Temple, + d2enum.RegionAct5IceCaves, d2enum.RegionAct5Baal: + palettePath = d2resource.PaletteAct5 + break + default: + return nil, errors.New("failed to find palette for region") + } + + return d2asset.LoadPalette(palettePath) +} diff --git a/main.go b/main.go index 197c0f1e7..36c485e75 100644 --- a/main.go +++ b/main.go @@ -18,7 +18,6 @@ import ( "github.com/OpenDiablo2/OpenDiablo2/d2common" "github.com/OpenDiablo2/OpenDiablo2/d2common/d2data" "github.com/OpenDiablo2/OpenDiablo2/d2common/d2data/d2datadict" - "github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum" "github.com/OpenDiablo2/OpenDiablo2/d2common/d2resource" "github.com/OpenDiablo2/OpenDiablo2/d2game/d2gamescene" @@ -179,10 +178,6 @@ func initialize() error { return err } - if err := loadPalettes(); err != nil { - return err - } - if err := loadStrings(); err != nil { return err } @@ -370,41 +365,6 @@ func renderDebug(target d2render.Surface) error { return nil } -func loadPalettes() error { - palNames := []string{ - "act1", - "act2", - "act3", - "act4", - "act5", - "endgame", - "endgame2", - "fechar", - "loading", - "menu0", - "menu1", - "menu2", - "menu3", - "menu4", - "sky", - "static", - "trademark", - "units", - } - - for _, pal := range palNames { - file, err := d2asset.LoadFile(`data\global\palette\` + pal + `\pal.dat`) - if err != nil { - return err - } - - d2datadict.LoadPalette(d2enum.PaletteType(pal), file) - } - - log.Printf("Loaded %d palettes", len(d2datadict.Palettes)) - return nil -} - func loadDataDict() error { entries := []struct { path string