2020-06-21 18:40:37 -04:00
|
|
|
package d2maprenderer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
2020-09-14 14:47:11 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2util"
|
|
|
|
|
2020-07-03 14:00:56 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
|
2020-06-21 18:40:37 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2ds1"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2dt1"
|
2020-09-12 16:25:09 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2math"
|
2020-06-21 18:40:37 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func (mr *MapRenderer) generateTileCache() {
|
2020-09-23 13:30:54 -04:00
|
|
|
var err error
|
|
|
|
mr.palette, err = mr.loadPaletteForAct(d2enum.RegionIdType(mr.mapEngine.LevelType().ID))
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
2020-06-21 18:40:37 -04:00
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
tiles := *mr.mapEngine.Tiles()
|
|
|
|
for idx := range tiles {
|
|
|
|
tile := &tiles[idx]
|
2020-07-09 16:11:01 -04:00
|
|
|
|
2020-07-21 08:50:45 -04:00
|
|
|
for i := range tile.Components.Floors {
|
|
|
|
if !tile.Components.Floors[i].Hidden && tile.Components.Floors[i].Prop1 != 0 {
|
2020-07-23 12:56:50 -04:00
|
|
|
mr.generateFloorCache(&tile.Components.Floors[i])
|
2020-06-21 18:40:37 -04:00
|
|
|
}
|
|
|
|
}
|
2020-07-09 16:11:01 -04:00
|
|
|
|
2020-07-21 08:50:45 -04:00
|
|
|
for i := range tile.Components.Shadows {
|
|
|
|
if !tile.Components.Shadows[i].Hidden && tile.Components.Shadows[i].Prop1 != 0 {
|
2020-07-23 12:56:50 -04:00
|
|
|
mr.generateShadowCache(&tile.Components.Shadows[i])
|
2020-06-21 18:40:37 -04:00
|
|
|
}
|
|
|
|
}
|
2020-07-09 16:11:01 -04:00
|
|
|
|
2020-07-21 08:50:45 -04:00
|
|
|
for i := range tile.Components.Walls {
|
|
|
|
if !tile.Components.Walls[i].Hidden && tile.Components.Walls[i].Prop1 != 0 {
|
2020-07-23 12:56:50 -04:00
|
|
|
mr.generateWallCache(&tile.Components.Walls[i])
|
2020-06-21 18:40:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
func (mr *MapRenderer) generateFloorCache(tile *d2ds1.FloorShadowRecord) {
|
2020-07-21 08:50:45 -04:00
|
|
|
tileOptions := mr.mapEngine.GetTiles(int(tile.Style), int(tile.Sequence), 0)
|
2020-07-09 16:11:01 -04:00
|
|
|
|
2020-06-21 18:40:37 -04:00
|
|
|
var tileData []*d2dt1.Tile
|
2020-07-09 16:11:01 -04:00
|
|
|
|
2020-06-21 18:40:37 -04:00
|
|
|
if tileOptions == nil {
|
|
|
|
log.Printf("Could not locate tile Style:%d, Seq: %d, Type: %d\n", tile.Style, tile.Sequence, 0)
|
2020-07-09 16:11:01 -04:00
|
|
|
|
2020-06-21 18:40:37 -04:00
|
|
|
tileData = append(tileData, &d2dt1.Tile{})
|
|
|
|
tileData[0].Width = 10
|
|
|
|
tileData[0].Height = 10
|
|
|
|
} else {
|
|
|
|
if !tileOptions[0].MaterialFlags.Lava {
|
2020-07-21 08:50:45 -04:00
|
|
|
tileData = append(tileData, &tileOptions[tile.RandomIndex])
|
2020-06-21 18:40:37 -04:00
|
|
|
} else {
|
|
|
|
tile.Animated = true
|
|
|
|
for i := range tileOptions {
|
|
|
|
tileData = append(tileData, &tileOptions[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-21 08:50:45 -04:00
|
|
|
var tileIndex byte
|
|
|
|
|
2020-06-21 18:40:37 -04:00
|
|
|
for i := range tileData {
|
2020-07-21 08:50:45 -04:00
|
|
|
if tileData[i].MaterialFlags.Lava {
|
2020-06-21 18:40:37 -04:00
|
|
|
tileIndex = byte(tileData[i].RarityFrameIndex)
|
2020-07-21 08:50:45 -04:00
|
|
|
} else {
|
|
|
|
tileIndex = tile.RandomIndex
|
2020-06-21 18:40:37 -04:00
|
|
|
}
|
2020-07-09 16:11:01 -04:00
|
|
|
|
2020-06-21 18:40:37 -04:00
|
|
|
cachedImage := mr.getImageCacheRecord(tile.Style, tile.Sequence, 0, tileIndex)
|
2020-07-09 16:11:01 -04:00
|
|
|
|
2020-06-21 18:40:37 -04:00
|
|
|
if cachedImage != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-07-09 16:11:01 -04:00
|
|
|
|
2020-06-21 18:40:37 -04:00
|
|
|
tileYMinimum := int32(0)
|
2020-07-09 16:11:01 -04:00
|
|
|
|
2020-06-21 18:40:37 -04:00
|
|
|
for _, block := range tileData[i].Blocks {
|
2020-08-05 00:03:33 -04:00
|
|
|
tileYMinimum = d2math.MinInt32(tileYMinimum, int32(block.Y))
|
2020-06-21 18:40:37 -04:00
|
|
|
}
|
2020-07-09 16:11:01 -04:00
|
|
|
|
2020-08-05 00:03:33 -04:00
|
|
|
tileYOffset := d2math.AbsInt32(tileYMinimum)
|
|
|
|
tileHeight := d2math.AbsInt32(tileData[i].Height)
|
2020-09-23 13:30:54 -04:00
|
|
|
image, err := mr.renderer.NewSurface(int(tileData[i].Width), int(tileHeight), d2enum.FilterNearest)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
|
2020-07-08 17:46:45 -04:00
|
|
|
indexData := make([]byte, tileData[i].Width*tileHeight)
|
2020-07-21 08:50:45 -04:00
|
|
|
d2dt1.DecodeTileGfxData(tileData[i].Blocks, &indexData, tileYOffset, tileData[i].Width)
|
2020-09-14 14:47:11 -04:00
|
|
|
pixels := d2util.ImgIndexToRGBA(indexData, mr.palette)
|
2020-07-08 17:46:45 -04:00
|
|
|
|
2020-09-23 13:30:54 -04:00
|
|
|
err = image.ReplacePixels(pixels)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
2020-06-21 18:40:37 -04:00
|
|
|
mr.setImageCacheRecord(tile.Style, tile.Sequence, 0, tileIndex, image)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
func (mr *MapRenderer) generateShadowCache(tile *d2ds1.FloorShadowRecord) {
|
2020-07-21 08:50:45 -04:00
|
|
|
tileOptions := mr.mapEngine.GetTiles(int(tile.Style), int(tile.Sequence), 13)
|
2020-07-09 16:11:01 -04:00
|
|
|
|
2020-06-21 18:40:37 -04:00
|
|
|
var tileData *d2dt1.Tile
|
2020-07-09 16:11:01 -04:00
|
|
|
|
2020-06-21 18:40:37 -04:00
|
|
|
if tileOptions == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
tileData = &tileOptions[tile.RandomIndex]
|
|
|
|
|
2020-06-21 18:40:37 -04:00
|
|
|
if tileData.Width == 0 || tileData.Height == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tileMinY := int32(0)
|
|
|
|
tileMaxY := int32(0)
|
2020-07-09 16:11:01 -04:00
|
|
|
|
2020-06-21 18:40:37 -04:00
|
|
|
for _, block := range tileData.Blocks {
|
2020-08-05 00:03:33 -04:00
|
|
|
tileMinY = d2math.MinInt32(tileMinY, int32(block.Y))
|
|
|
|
tileMaxY = d2math.MaxInt32(tileMaxY, int32(block.Y+32))
|
2020-06-21 18:40:37 -04:00
|
|
|
}
|
2020-07-09 16:11:01 -04:00
|
|
|
|
2020-06-21 18:40:37 -04:00
|
|
|
tileYOffset := -tileMinY
|
|
|
|
tileHeight := int(tileMaxY - tileMinY)
|
|
|
|
tile.YAdjust = int(tileMinY + 80)
|
|
|
|
|
2020-07-21 08:50:45 -04:00
|
|
|
cachedImage := mr.getImageCacheRecord(tile.Style, tile.Sequence, 13, tile.RandomIndex)
|
2020-06-21 18:40:37 -04:00
|
|
|
if cachedImage != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-23 13:30:54 -04:00
|
|
|
image, err := mr.renderer.NewSurface(int(tileData.Width), tileHeight, d2enum.FilterNearest)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
|
2020-07-08 17:46:45 -04:00
|
|
|
indexData := make([]byte, tileData.Width*int32(tileHeight))
|
2020-07-21 08:50:45 -04:00
|
|
|
d2dt1.DecodeTileGfxData(tileData.Blocks, &indexData, tileYOffset, tileData.Width)
|
2020-09-14 14:47:11 -04:00
|
|
|
pixels := d2util.ImgIndexToRGBA(indexData, mr.palette)
|
2020-09-23 13:30:54 -04:00
|
|
|
|
|
|
|
err = image.ReplacePixels(pixels)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
|
2020-07-21 08:50:45 -04:00
|
|
|
mr.setImageCacheRecord(tile.Style, tile.Sequence, 13, tile.RandomIndex, image)
|
2020-06-21 18:40:37 -04:00
|
|
|
}
|
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
func (mr *MapRenderer) generateWallCache(tile *d2ds1.WallRecord) {
|
2020-07-21 08:50:45 -04:00
|
|
|
tileOptions := mr.mapEngine.GetTiles(int(tile.Style), int(tile.Sequence), int(tile.Type))
|
2020-07-09 16:11:01 -04:00
|
|
|
|
2020-06-21 18:40:37 -04:00
|
|
|
var tileData *d2dt1.Tile
|
2020-07-09 16:11:01 -04:00
|
|
|
|
2020-06-21 18:40:37 -04:00
|
|
|
if tileOptions == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-21 08:50:45 -04:00
|
|
|
tileData = &tileOptions[tile.RandomIndex]
|
2020-07-09 16:11:01 -04:00
|
|
|
|
2020-06-21 18:40:37 -04:00
|
|
|
var newTileData *d2dt1.Tile = nil
|
|
|
|
|
|
|
|
if tile.Type == 3 {
|
2020-07-21 08:50:45 -04:00
|
|
|
newTileOptions := mr.mapEngine.GetTiles(int(tile.Style), int(tile.Sequence), int(4))
|
|
|
|
newTileData = &newTileOptions[tile.RandomIndex]
|
2020-06-21 18:40:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
tileMinY := int32(0)
|
|
|
|
tileMaxY := int32(0)
|
|
|
|
|
|
|
|
target := tileData
|
|
|
|
|
|
|
|
if newTileData != nil && newTileData.Height < tileData.Height {
|
|
|
|
target = newTileData
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, block := range target.Blocks {
|
2020-08-05 00:03:33 -04:00
|
|
|
tileMinY = d2math.MinInt32(tileMinY, int32(block.Y))
|
|
|
|
tileMaxY = d2math.MaxInt32(tileMaxY, int32(block.Y+32))
|
2020-06-21 18:40:37 -04:00
|
|
|
}
|
|
|
|
|
2020-08-05 00:03:33 -04:00
|
|
|
realHeight := d2math.MaxInt32(d2math.AbsInt32(tileData.Height), tileMaxY-tileMinY)
|
2020-06-21 18:40:37 -04:00
|
|
|
tileYOffset := -tileMinY
|
|
|
|
|
|
|
|
if tile.Type == 15 {
|
|
|
|
tile.YAdjust = -int(tileData.RoofHeight)
|
|
|
|
} else {
|
|
|
|
tile.YAdjust = int(tileMinY) + 80
|
|
|
|
}
|
|
|
|
|
2020-07-21 08:50:45 -04:00
|
|
|
cachedImage := mr.getImageCacheRecord(tile.Style, tile.Sequence, tile.Type, tile.RandomIndex)
|
2020-06-21 18:40:37 -04:00
|
|
|
if cachedImage != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if realHeight == 0 {
|
|
|
|
log.Printf("Invalid 0 height for wall tile")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-23 13:30:54 -04:00
|
|
|
image, err := mr.renderer.NewSurface(160, int(realHeight), d2enum.FilterNearest)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
|
2020-07-08 17:46:45 -04:00
|
|
|
indexData := make([]byte, 160*realHeight)
|
2020-07-03 14:00:56 -04:00
|
|
|
|
2020-07-21 08:50:45 -04:00
|
|
|
d2dt1.DecodeTileGfxData(tileData.Blocks, &indexData, tileYOffset, 160)
|
2020-06-21 18:40:37 -04:00
|
|
|
|
|
|
|
if newTileData != nil {
|
2020-07-21 08:50:45 -04:00
|
|
|
d2dt1.DecodeTileGfxData(newTileData.Blocks, &indexData, tileYOffset, 160)
|
2020-06-21 18:40:37 -04:00
|
|
|
}
|
|
|
|
|
2020-09-14 14:47:11 -04:00
|
|
|
pixels := d2util.ImgIndexToRGBA(indexData, mr.palette)
|
2020-07-08 17:46:45 -04:00
|
|
|
|
2020-06-21 18:40:37 -04:00
|
|
|
if err := image.ReplacePixels(pixels); err != nil {
|
|
|
|
log.Panicf(err.Error())
|
|
|
|
}
|
|
|
|
|
2020-07-21 08:50:45 -04:00
|
|
|
mr.setImageCacheRecord(tile.Style, tile.Sequence, tile.Type, tile.RandomIndex, image)
|
2020-06-21 18:40:37 -04:00
|
|
|
}
|