Skip to content

Commit

Permalink
Remove global palettes; everything goes through d2asset now (OpenDiab…
Browse files Browse the repository at this point in the history
…lo2#322)

* Configuration cleanup

* Cleanup

* remove global palettes
  • Loading branch information
FooSoft authored Feb 27, 2020
1 parent 1011b2f commit 664b841
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 125 deletions.
28 changes: 0 additions & 28 deletions d2common/d2data/d2datadict/palette.go

This file was deleted.

43 changes: 0 additions & 43 deletions d2common/d2enum/palette_defs.go

This file was deleted.

4 changes: 2 additions & 2 deletions d2core/d2asset/animation_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
6 changes: 0 additions & 6 deletions d2core/d2asset/asset_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions d2core/d2asset/d2asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
41 changes: 35 additions & 6 deletions d2core/d2map/region.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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" {
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
40 changes: 0 additions & 40 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -179,10 +178,6 @@ func initialize() error {
return err
}

if err := loadPalettes(); err != nil {
return err
}

if err := loadStrings(); err != nil {
return err
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 664b841

Please sign in to comment.