2019-11-10 08:51:02 -05:00
|
|
|
package d2mapengine
|
2019-10-31 13:39:05 -04:00
|
|
|
|
|
|
|
import (
|
2019-11-17 00:52:13 -05:00
|
|
|
"fmt"
|
2019-11-01 22:12:07 -04:00
|
|
|
"image/color"
|
|
|
|
"log"
|
2019-10-31 13:39:05 -04:00
|
|
|
"math"
|
|
|
|
"math/rand"
|
2019-11-17 00:52:13 -05:00
|
|
|
"sort"
|
2019-11-01 14:12:23 -04:00
|
|
|
"strconv"
|
2019-11-06 18:25:19 -05:00
|
|
|
"sync"
|
2019-11-17 00:52:13 -05:00
|
|
|
"time"
|
2019-11-01 14:12:23 -04:00
|
|
|
|
2019-11-17 00:16:33 -05:00
|
|
|
"github.com/OpenDiablo2/D2Shared/d2data/d2dt1"
|
2019-11-10 10:44:13 -05:00
|
|
|
|
2019-11-17 00:16:33 -05:00
|
|
|
"github.com/OpenDiablo2/D2Shared/d2data/d2ds1"
|
2019-11-10 10:44:13 -05:00
|
|
|
|
2019-11-10 08:51:02 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core"
|
|
|
|
|
2019-11-17 00:16:33 -05:00
|
|
|
"github.com/OpenDiablo2/D2Shared/d2helper"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2corehelper"
|
2019-10-31 13:39:05 -04:00
|
|
|
|
2019-11-17 00:16:33 -05:00
|
|
|
"github.com/OpenDiablo2/D2Shared/d2common/d2interface"
|
2019-11-10 03:36:53 -05:00
|
|
|
|
2019-11-17 00:16:33 -05:00
|
|
|
"github.com/OpenDiablo2/D2Shared/d2common/d2enum"
|
2019-11-10 03:36:53 -05:00
|
|
|
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2render"
|
2019-10-31 13:39:05 -04:00
|
|
|
|
2019-11-17 00:16:33 -05:00
|
|
|
"github.com/OpenDiablo2/D2Shared/d2data"
|
2019-11-10 03:36:53 -05:00
|
|
|
|
2019-11-17 00:16:33 -05:00
|
|
|
"github.com/OpenDiablo2/D2Shared/d2data/d2datadict"
|
2019-11-10 03:36:53 -05:00
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten"
|
2019-10-31 13:39:05 -04:00
|
|
|
)
|
|
|
|
|
2019-11-17 00:52:13 -05:00
|
|
|
//TODO: move to corresponding file
|
|
|
|
type ByRarity []d2dt1.Tile
|
|
|
|
|
|
|
|
func (a ByRarity) Len() int { return len(a) }
|
|
|
|
func (a ByRarity) Less(i, j int) bool { return a[i].RarityFrameIndex < a[j].RarityFrameIndex }
|
|
|
|
func (a ByRarity) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
|
|
|
2019-10-31 13:39:05 -04:00
|
|
|
type Region struct {
|
2019-11-06 21:25:09 -05:00
|
|
|
RegionPath string
|
2019-11-10 08:51:02 -05:00
|
|
|
LevelType d2datadict.LevelTypeRecord
|
2019-11-17 00:52:13 -05:00
|
|
|
LevelPreset d2datadict.LevelPresetRecord
|
2019-11-06 18:25:19 -05:00
|
|
|
TileWidth int32
|
|
|
|
TileHeight int32
|
2019-11-10 10:44:13 -05:00
|
|
|
Tiles []d2dt1.Tile
|
|
|
|
DS1 d2ds1.DS1
|
2019-11-10 08:51:02 -05:00
|
|
|
Palette d2datadict.PaletteRec
|
2019-11-17 00:52:13 -05:00
|
|
|
FloorCache map[string]*TileCacheRecord
|
|
|
|
ShadowCache map[string]*TileCacheRecord
|
|
|
|
WallCache map[string]*TileCacheRecord
|
2019-11-10 10:44:13 -05:00
|
|
|
AnimationEntities []d2render.AnimatedEntity
|
2019-11-10 08:51:02 -05:00
|
|
|
NPCs []*d2core.NPC
|
2019-11-07 23:44:03 -05:00
|
|
|
StartX float64
|
|
|
|
StartY float64
|
2019-10-31 13:39:05 -04:00
|
|
|
}
|
|
|
|
|
2019-11-17 00:52:13 -05:00
|
|
|
func LoadRegion(seed rand.Source, levelType d2enum.RegionIdType, levelPreset int, fileProvider d2interface.FileProvider, fileIndex int) *Region {
|
2019-10-31 13:39:05 -04:00
|
|
|
result := &Region{
|
2019-11-10 08:51:02 -05:00
|
|
|
LevelType: d2datadict.LevelTypes[levelType],
|
2019-11-17 00:52:13 -05:00
|
|
|
LevelPreset: d2datadict.LevelPresets[levelPreset],
|
2019-11-10 10:44:13 -05:00
|
|
|
Tiles: make([]d2dt1.Tile, 0),
|
2019-11-17 00:52:13 -05:00
|
|
|
FloorCache: make(map[string]*TileCacheRecord),
|
|
|
|
ShadowCache: make(map[string]*TileCacheRecord),
|
|
|
|
WallCache: make(map[string]*TileCacheRecord),
|
2019-10-31 13:39:05 -04:00
|
|
|
}
|
2019-11-10 08:51:02 -05:00
|
|
|
result.Palette = d2datadict.Palettes[d2enum.PaletteType("act"+strconv.Itoa(int(result.LevelType.Act)))]
|
2019-11-11 23:48:55 -05:00
|
|
|
//bm := result.levelPreset.Dt1Mask
|
2019-11-06 18:25:19 -05:00
|
|
|
for _, levelTypeDt1 := range result.LevelType.Files {
|
2019-11-01 16:51:50 -04:00
|
|
|
/*
|
|
|
|
if bm&1 == 0 {
|
|
|
|
bm >>= 1
|
|
|
|
continue
|
|
|
|
}
|
2019-11-01 14:12:23 -04:00
|
|
|
bm >>= 1
|
2019-11-01 16:51:50 -04:00
|
|
|
*/
|
2019-10-31 13:39:05 -04:00
|
|
|
if len(levelTypeDt1) == 0 || levelTypeDt1 == "" || levelTypeDt1 == "0" {
|
|
|
|
continue
|
|
|
|
}
|
2019-11-10 10:44:13 -05:00
|
|
|
dt1 := d2dt1.LoadDT1("/data/global/tiles/"+levelTypeDt1, fileProvider)
|
2019-10-31 21:17:13 -04:00
|
|
|
result.Tiles = append(result.Tiles, dt1.Tiles...)
|
2019-10-31 13:39:05 -04:00
|
|
|
}
|
|
|
|
levelFilesToPick := make([]string, 0)
|
2019-11-17 00:52:13 -05:00
|
|
|
for _, fileRecord := range result.LevelPreset.Files {
|
2019-10-31 13:39:05 -04:00
|
|
|
if len(fileRecord) == 0 || fileRecord == "" || fileRecord == "0" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
levelFilesToPick = append(levelFilesToPick, fileRecord)
|
|
|
|
}
|
|
|
|
random := rand.New(seed)
|
|
|
|
levelIndex := int(math.Round(float64(len(levelFilesToPick)-1) * random.Float64()))
|
2019-11-17 00:52:13 -05:00
|
|
|
if fileIndex >= 0 && fileIndex < len(levelFilesToPick) {
|
|
|
|
levelIndex = fileIndex
|
|
|
|
}
|
2019-10-31 13:39:05 -04:00
|
|
|
levelFile := levelFilesToPick[levelIndex]
|
2019-11-06 21:25:09 -05:00
|
|
|
result.RegionPath = levelFile
|
2019-11-10 10:44:13 -05:00
|
|
|
result.DS1 = d2ds1.LoadDS1("/data/global/tiles/"+levelFile, fileProvider)
|
2019-10-31 13:39:05 -04:00
|
|
|
result.TileWidth = result.DS1.Width
|
|
|
|
result.TileHeight = result.DS1.Height
|
2019-11-07 23:44:03 -05:00
|
|
|
result.loadObjects(fileProvider)
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2019-11-10 03:36:53 -05:00
|
|
|
func (v *Region) loadObjects(fileProvider d2interface.FileProvider) {
|
2019-11-06 18:25:19 -05:00
|
|
|
var wg sync.WaitGroup
|
2019-11-07 23:44:03 -05:00
|
|
|
wg.Add(len(v.DS1.Objects))
|
2019-11-10 10:44:13 -05:00
|
|
|
v.AnimationEntities = make([]d2render.AnimatedEntity, 0)
|
2019-11-10 08:51:02 -05:00
|
|
|
v.NPCs = make([]*d2core.NPC, 0)
|
2019-11-07 23:44:03 -05:00
|
|
|
for _, object := range v.DS1.Objects {
|
2019-11-10 03:36:53 -05:00
|
|
|
go func(object d2data.Object) {
|
2019-11-06 18:25:19 -05:00
|
|
|
defer wg.Done()
|
|
|
|
switch object.Lookup.Type {
|
2019-11-10 08:51:02 -05:00
|
|
|
case d2datadict.ObjectTypeCharacter:
|
2019-11-07 23:44:03 -05:00
|
|
|
// Temp code, maybe..
|
2019-11-06 18:25:19 -05:00
|
|
|
if object.Lookup.Base == "" || object.Lookup.Token == "" || object.Lookup.TR == "" {
|
|
|
|
return
|
|
|
|
}
|
2019-11-11 23:48:55 -05:00
|
|
|
npc := d2core.CreateNPC(object.X, object.Y, object.Lookup, fileProvider, 1)
|
|
|
|
npc.SetPaths(object.Paths)
|
2019-11-07 23:44:03 -05:00
|
|
|
v.NPCs = append(v.NPCs, npc)
|
2019-11-10 08:51:02 -05:00
|
|
|
case d2datadict.ObjectTypeItem:
|
2019-11-09 23:37:02 -05:00
|
|
|
if object.ObjectInfo == nil || !object.ObjectInfo.Draw || object.Lookup.Base == "" || object.Lookup.Token == "" {
|
2019-11-07 23:44:03 -05:00
|
|
|
return
|
|
|
|
}
|
2019-11-11 23:48:55 -05:00
|
|
|
entity := d2render.CreateAnimatedEntity(object.X, object.Y, object.Lookup, fileProvider, d2enum.Units)
|
2019-11-14 22:20:01 -05:00
|
|
|
entity.SetMode(object.Lookup.Mode, object.Lookup.Class, 0)
|
2019-11-07 23:44:03 -05:00
|
|
|
v.AnimationEntities = append(v.AnimationEntities, entity)
|
2019-11-05 22:23:48 -05:00
|
|
|
}
|
2019-11-06 18:25:19 -05:00
|
|
|
}(object)
|
2019-11-05 22:23:48 -05:00
|
|
|
}
|
2019-11-06 18:25:19 -05:00
|
|
|
wg.Wait()
|
2019-10-31 13:39:05 -04:00
|
|
|
}
|
|
|
|
|
2019-11-11 23:48:55 -05:00
|
|
|
func (v *Region) RenderTile(offsetX, offsetY, tileX, tileY int, layerType d2enum.RegionLayerType, layerIndex int, target *ebiten.Image) {
|
2019-11-06 18:25:19 -05:00
|
|
|
offsetX -= 80
|
2019-10-31 13:39:05 -04:00
|
|
|
switch layerType {
|
2019-11-11 23:48:55 -05:00
|
|
|
case d2enum.RegionLayerTypeFloors:
|
2019-11-17 00:52:13 -05:00
|
|
|
v.renderFloor(v.DS1.Tiles[tileY][tileX].Floors[layerIndex], offsetX, offsetY, target, tileX, tileY)
|
2019-11-11 23:48:55 -05:00
|
|
|
case d2enum.RegionLayerTypeWalls:
|
2019-11-17 00:52:13 -05:00
|
|
|
v.renderWall(v.DS1.Tiles[tileY][tileX].Walls[layerIndex], offsetX, offsetY, target, tileX, tileY)
|
2019-11-11 23:48:55 -05:00
|
|
|
case d2enum.RegionLayerTypeShadows:
|
2019-11-17 00:52:13 -05:00
|
|
|
v.renderShadow(v.DS1.Tiles[tileY][tileX].Shadows[layerIndex], offsetX, offsetY, target, tileX, tileY)
|
2019-10-31 13:39:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-17 00:52:13 -05:00
|
|
|
func (v *Region) getRandomTile(tiles []d2dt1.Tile) *d2dt1.Tile {
|
|
|
|
if len(tiles) == 1 {
|
|
|
|
return &tiles[0]
|
|
|
|
}
|
|
|
|
sort.Sort(ByRarity(tiles))
|
|
|
|
s := 0
|
|
|
|
for _, t := range tiles {
|
|
|
|
s += int(t.RarityFrameIndex)
|
|
|
|
}
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
r := 0
|
|
|
|
if s != 0 {
|
|
|
|
r = rand.Intn(s) + 1
|
|
|
|
}
|
|
|
|
for _, t := range tiles {
|
|
|
|
r -= int(t.RarityFrameIndex)
|
|
|
|
if r <= 0 {
|
|
|
|
return &t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &tiles[0]
|
|
|
|
}
|
|
|
|
|
2019-11-10 10:44:13 -05:00
|
|
|
func (v *Region) getTile(mainIndex, subIndex, orientation int32) *d2dt1.Tile {
|
2019-11-17 00:52:13 -05:00
|
|
|
tiles := []d2dt1.Tile{}
|
2019-10-31 13:39:05 -04:00
|
|
|
for _, tile := range v.Tiles {
|
2019-11-01 14:12:23 -04:00
|
|
|
if tile.MainIndex != mainIndex || tile.SubIndex != subIndex || tile.Orientation != orientation {
|
2019-10-31 13:39:05 -04:00
|
|
|
continue
|
|
|
|
}
|
2019-11-17 00:52:13 -05:00
|
|
|
tiles = append(tiles, tile)
|
|
|
|
}
|
|
|
|
if len(tiles) == 0 {
|
|
|
|
log.Printf("Unknown tile ID [%d %d %d]\n", mainIndex, subIndex, orientation)
|
|
|
|
return nil
|
2019-10-31 13:39:05 -04:00
|
|
|
}
|
2019-11-17 00:52:13 -05:00
|
|
|
return v.getRandomTile(tiles)
|
2019-10-31 13:39:05 -04:00
|
|
|
}
|
|
|
|
|
2019-11-17 00:52:13 -05:00
|
|
|
func (v *Region) renderFloor(tile d2ds1.FloorShadowRecord, offsetX, offsetY int, target *ebiten.Image, tileX, tileY int) {
|
|
|
|
tileCacheIndex := fmt.Sprintf("%v-%v-%v-%v", tileY, tileX, tile.MainIndex, tile.SubIndex)
|
2019-11-01 22:12:07 -04:00
|
|
|
tileCache, exists := v.FloorCache[tileCacheIndex]
|
|
|
|
if !exists {
|
|
|
|
v.FloorCache[tileCacheIndex] = v.generateFloorCache(tile)
|
|
|
|
tileCache = v.FloorCache[tileCacheIndex]
|
|
|
|
if tileCache == nil {
|
2019-11-17 00:52:13 -05:00
|
|
|
log.Println("Could not load floor tile: " + tileCacheIndex)
|
2019-11-13 14:26:42 -05:00
|
|
|
return
|
2019-11-01 22:12:07 -04:00
|
|
|
}
|
2019-11-01 16:51:50 -04:00
|
|
|
}
|
2019-11-13 14:26:42 -05:00
|
|
|
if tileCache == nil {
|
2019-11-17 00:52:13 -05:00
|
|
|
log.Println("Nil tile cache: " + tileCacheIndex)
|
2019-11-13 14:26:42 -05:00
|
|
|
return
|
|
|
|
}
|
2019-11-01 14:12:23 -04:00
|
|
|
opts := &ebiten.DrawImageOptions{}
|
|
|
|
opts.GeoM.Translate(float64(offsetX+tileCache.XOffset), float64(offsetY+tileCache.YOffset))
|
|
|
|
target.DrawImage(tileCache.Image, opts)
|
2019-11-17 00:52:13 -05:00
|
|
|
return
|
2019-11-01 14:12:23 -04:00
|
|
|
}
|
|
|
|
|
2019-11-17 00:52:13 -05:00
|
|
|
func (v *Region) renderWall(tile d2ds1.WallRecord, offsetX, offsetY int, target *ebiten.Image, tileX, tileY int) {
|
|
|
|
tileCacheIndex := fmt.Sprintf("%v-%v-%v-%v-%v", tileY, tileX, tile.MainIndex, tile.SubIndex, tile.Orientation)
|
2019-11-01 22:12:07 -04:00
|
|
|
tileCache, exists := v.WallCache[tileCacheIndex]
|
|
|
|
if !exists {
|
|
|
|
v.WallCache[tileCacheIndex] = v.generateWallCache(tile)
|
|
|
|
if v.WallCache[tileCacheIndex] == nil {
|
2019-11-17 00:52:13 -05:00
|
|
|
log.Println("Could not generate wall: " + tileCacheIndex)
|
2019-11-13 14:26:42 -05:00
|
|
|
return
|
2019-11-01 22:12:07 -04:00
|
|
|
}
|
|
|
|
tileCache = v.WallCache[tileCacheIndex]
|
2019-11-01 16:51:50 -04:00
|
|
|
}
|
2019-11-13 14:26:42 -05:00
|
|
|
if tileCache == nil {
|
2019-11-17 00:52:13 -05:00
|
|
|
log.Println("Nil tile cache: " + tileCacheIndex)
|
2019-11-13 14:26:42 -05:00
|
|
|
return
|
|
|
|
}
|
2019-11-01 22:12:07 -04:00
|
|
|
opts := &ebiten.DrawImageOptions{}
|
|
|
|
opts.GeoM.Translate(float64(offsetX+tileCache.XOffset), float64(offsetY+tileCache.YOffset))
|
|
|
|
target.DrawImage(tileCache.Image, opts)
|
|
|
|
}
|
|
|
|
|
2019-11-17 00:52:13 -05:00
|
|
|
func (v *Region) renderShadow(tile d2ds1.FloorShadowRecord, offsetX, offsetY int, target *ebiten.Image, tileX, tileY int) {
|
|
|
|
tileCacheIndex := fmt.Sprintf("%v-%v-%v-%v", tileY, tileX, tile.MainIndex, tile.SubIndex)
|
2019-11-01 22:12:07 -04:00
|
|
|
tileCache, exists := v.ShadowCache[tileCacheIndex]
|
|
|
|
if !exists {
|
|
|
|
v.ShadowCache[tileCacheIndex] = v.generateShadowCache(tile)
|
|
|
|
tileCache = v.ShadowCache[tileCacheIndex]
|
|
|
|
if tileCache == nil {
|
2019-11-17 00:52:13 -05:00
|
|
|
log.Println("Could not load shadow tile: " + tileCacheIndex)
|
2019-11-13 14:26:42 -05:00
|
|
|
return
|
2019-11-01 16:51:50 -04:00
|
|
|
}
|
|
|
|
}
|
2019-11-13 14:26:42 -05:00
|
|
|
if tileCache == nil {
|
2019-11-17 00:52:13 -05:00
|
|
|
log.Println("Nil tile cache: " + tileCacheIndex)
|
2019-11-13 14:26:42 -05:00
|
|
|
return
|
|
|
|
}
|
2019-11-01 16:51:50 -04:00
|
|
|
opts := &ebiten.DrawImageOptions{}
|
|
|
|
opts.GeoM.Translate(float64(offsetX+tileCache.XOffset), float64(offsetY+tileCache.YOffset))
|
2019-11-17 00:16:33 -05:00
|
|
|
opts.ColorM = d2corehelper.ColorToColorM(color.RGBA{255, 255, 255, 160})
|
2019-11-01 16:51:50 -04:00
|
|
|
target.DrawImage(tileCache.Image, opts)
|
|
|
|
}
|
2019-11-01 14:12:23 -04:00
|
|
|
|
2019-11-10 10:44:13 -05:00
|
|
|
func (v *Region) decodeTileGfxData(blocks []d2dt1.Block, pixels []byte, tileYOffset int32, tileWidth int32) {
|
2019-11-01 16:51:50 -04:00
|
|
|
for _, block := range blocks {
|
2019-11-10 10:44:13 -05:00
|
|
|
if block.Format == d2dt1.BlockFormatIsometric {
|
2019-11-01 14:12:23 -04:00
|
|
|
// 3D isometric decoding
|
|
|
|
xjump := []int32{14, 12, 10, 8, 6, 4, 2, 0, 2, 4, 6, 8, 10, 12, 14}
|
|
|
|
nbpix := []int32{4, 8, 12, 16, 20, 24, 28, 32, 28, 24, 20, 16, 12, 8, 4}
|
|
|
|
blockX := int32(block.X)
|
|
|
|
blockY := int32(block.Y)
|
|
|
|
length := int32(256)
|
|
|
|
x := int32(0)
|
|
|
|
y := int32(0)
|
|
|
|
idx := 0
|
|
|
|
for length > 0 {
|
|
|
|
x = xjump[y]
|
|
|
|
n := nbpix[y]
|
|
|
|
length -= n
|
|
|
|
for n > 0 {
|
|
|
|
colorIndex := block.EncodedData[idx]
|
|
|
|
if colorIndex != 0 {
|
|
|
|
pixelColor := v.Palette.Colors[colorIndex]
|
2019-11-01 22:12:07 -04:00
|
|
|
offset := 4 * (((blockY + y + tileYOffset) * tileWidth) + (blockX + x))
|
|
|
|
pixels[offset] = pixelColor.R
|
|
|
|
pixels[offset+1] = pixelColor.G
|
|
|
|
pixels[offset+2] = pixelColor.B
|
|
|
|
pixels[offset+3] = 255
|
2019-11-01 14:12:23 -04:00
|
|
|
}
|
|
|
|
x++
|
|
|
|
n--
|
|
|
|
idx++
|
|
|
|
}
|
|
|
|
y++
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// RLE Encoding
|
|
|
|
blockX := int32(block.X)
|
|
|
|
blockY := int32(block.Y)
|
|
|
|
x := int32(0)
|
|
|
|
y := int32(0)
|
|
|
|
idx := 0
|
|
|
|
length := block.Length
|
|
|
|
for length > 0 {
|
2019-11-01 22:12:07 -04:00
|
|
|
b1 := block.EncodedData[idx]
|
|
|
|
b2 := block.EncodedData[idx+1]
|
|
|
|
idx += 2
|
2019-11-01 14:12:23 -04:00
|
|
|
length -= 2
|
2019-11-01 22:12:07 -04:00
|
|
|
if (b1 | b2) == 0 {
|
2019-11-01 14:12:23 -04:00
|
|
|
x = 0
|
|
|
|
y++
|
|
|
|
continue
|
|
|
|
}
|
2019-11-01 22:12:07 -04:00
|
|
|
x += int32(b1)
|
|
|
|
length -= int32(b2)
|
2019-11-01 14:12:23 -04:00
|
|
|
for b2 > 0 {
|
|
|
|
colorIndex := block.EncodedData[idx]
|
|
|
|
if colorIndex != 0 {
|
|
|
|
pixelColor := v.Palette.Colors[colorIndex]
|
2019-11-01 22:12:07 -04:00
|
|
|
offset := 4 * (((blockY + y + tileYOffset) * tileWidth) + (blockX + x))
|
|
|
|
pixels[offset] = pixelColor.R
|
|
|
|
pixels[offset+1] = pixelColor.G
|
|
|
|
pixels[offset+2] = pixelColor.B
|
|
|
|
pixels[offset+3] = 255
|
|
|
|
|
2019-11-01 14:12:23 -04:00
|
|
|
}
|
|
|
|
idx++
|
|
|
|
x++
|
|
|
|
b2--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-01 16:51:50 -04:00
|
|
|
}
|
|
|
|
|
2019-11-10 10:44:13 -05:00
|
|
|
func (v *Region) generateFloorCache(tile d2ds1.FloorShadowRecord) *TileCacheRecord {
|
2019-11-01 16:51:50 -04:00
|
|
|
tileData := v.getTile(int32(tile.MainIndex), int32(tile.SubIndex), 0)
|
|
|
|
if tileData == nil {
|
2019-11-13 14:26:42 -05:00
|
|
|
log.Printf("Could not locate tile Idx:%d, Sub: %d, Ori: %d\n", tile.MainIndex, tile.SubIndex, 0)
|
|
|
|
tileData = &d2dt1.Tile{}
|
|
|
|
tileData.Width = 10
|
|
|
|
tileData.Height = 10
|
2019-11-01 16:51:50 -04:00
|
|
|
}
|
|
|
|
tileYMinimum := int32(0)
|
|
|
|
for _, block := range tileData.Blocks {
|
2019-11-10 03:36:53 -05:00
|
|
|
tileYMinimum = d2helper.MinInt32(tileYMinimum, int32(block.Y))
|
2019-11-01 16:51:50 -04:00
|
|
|
}
|
2019-11-10 03:36:53 -05:00
|
|
|
tileYOffset := d2helper.AbsInt32(tileYMinimum)
|
|
|
|
tileHeight := d2helper.AbsInt32(tileData.Height)
|
2019-11-01 16:51:50 -04:00
|
|
|
image, _ := ebiten.NewImage(int(tileData.Width), int(tileHeight), ebiten.FilterNearest)
|
|
|
|
pixels := make([]byte, 4*tileData.Width*tileHeight)
|
2019-11-01 22:12:07 -04:00
|
|
|
v.decodeTileGfxData(tileData.Blocks, pixels, tileYOffset, tileData.Width)
|
2019-11-01 16:51:50 -04:00
|
|
|
image.ReplacePixels(pixels)
|
|
|
|
return &TileCacheRecord{image, 0, 0}
|
|
|
|
}
|
|
|
|
|
2019-11-10 10:44:13 -05:00
|
|
|
func (v *Region) generateShadowCache(tile d2ds1.FloorShadowRecord) *TileCacheRecord {
|
2019-11-01 22:12:07 -04:00
|
|
|
tileData := v.getTile(int32(tile.MainIndex), int32(tile.SubIndex), 13)
|
2019-11-01 16:51:50 -04:00
|
|
|
if tileData == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2019-11-01 22:12:07 -04:00
|
|
|
tileMinY := int32(0)
|
|
|
|
tileMaxY := int32(0)
|
2019-11-01 16:51:50 -04:00
|
|
|
for _, block := range tileData.Blocks {
|
2019-11-10 03:36:53 -05:00
|
|
|
tileMinY = d2helper.MinInt32(tileMinY, int32(block.Y))
|
|
|
|
tileMaxY = d2helper.MaxInt32(tileMaxY, int32(block.Y+32))
|
2019-11-01 16:51:50 -04:00
|
|
|
}
|
2019-11-01 22:12:07 -04:00
|
|
|
tileYOffset := -tileMinY
|
|
|
|
tileHeight := int(tileMaxY - tileMinY)
|
2019-11-01 16:51:50 -04:00
|
|
|
image, _ := ebiten.NewImage(int(tileData.Width), int(tileHeight), ebiten.FilterNearest)
|
2019-11-01 22:12:07 -04:00
|
|
|
pixels := make([]byte, 4*tileData.Width*int32(tileHeight))
|
|
|
|
v.decodeTileGfxData(tileData.Blocks, pixels, tileYOffset, tileData.Width)
|
2019-11-01 14:12:23 -04:00
|
|
|
image.ReplacePixels(pixels)
|
2019-11-01 22:12:07 -04:00
|
|
|
return &TileCacheRecord{image, 0, int(tileMinY) + 80}
|
|
|
|
}
|
|
|
|
|
2019-11-10 10:44:13 -05:00
|
|
|
func (v *Region) generateWallCache(tile d2ds1.WallRecord) *TileCacheRecord {
|
2019-11-01 22:12:07 -04:00
|
|
|
tileData := v.getTile(int32(tile.MainIndex), int32(tile.SubIndex), int32(tile.Orientation))
|
|
|
|
if tileData == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2019-11-10 10:44:13 -05:00
|
|
|
var newTileData *d2dt1.Tile = nil
|
2019-11-01 22:12:07 -04:00
|
|
|
if tile.Orientation == 3 {
|
|
|
|
newTileData = v.getTile(int32(tile.MainIndex), int32(tile.SubIndex), int32(4))
|
|
|
|
}
|
|
|
|
|
|
|
|
tileMinY := int32(0)
|
|
|
|
tileMaxY := int32(0)
|
|
|
|
target := tileData
|
|
|
|
if newTileData != nil && newTileData.Height < tileData.Height {
|
|
|
|
target = newTileData
|
|
|
|
}
|
|
|
|
for _, block := range target.Blocks {
|
2019-11-10 03:36:53 -05:00
|
|
|
tileMinY = d2helper.MinInt32(tileMinY, int32(block.Y))
|
|
|
|
tileMaxY = d2helper.MaxInt32(tileMaxY, int32(block.Y+32))
|
2019-11-01 22:12:07 -04:00
|
|
|
}
|
2019-11-10 03:36:53 -05:00
|
|
|
realHeight := d2helper.MaxInt32(d2helper.AbsInt32(tileData.Height), tileMaxY-tileMinY)
|
2019-11-01 22:12:07 -04:00
|
|
|
tileYOffset := -tileMinY
|
|
|
|
//tileHeight := int(tileMaxY - tileMinY)
|
|
|
|
image, _ := ebiten.NewImage(160, int(realHeight), ebiten.FilterNearest)
|
|
|
|
pixels := make([]byte, 4*160*realHeight)
|
|
|
|
v.decodeTileGfxData(tileData.Blocks, pixels, tileYOffset, 160)
|
|
|
|
if newTileData != nil {
|
|
|
|
v.decodeTileGfxData(newTileData.Blocks, pixels, tileYOffset, 160)
|
|
|
|
}
|
2019-11-01 16:51:50 -04:00
|
|
|
yAdjust := 0
|
2019-11-01 22:12:07 -04:00
|
|
|
if tile.Orientation > 15 {
|
|
|
|
// Lower Walls
|
|
|
|
yAdjust = 80
|
|
|
|
} else if tile.Orientation == 15 {
|
2019-11-01 16:51:50 -04:00
|
|
|
// Roof
|
|
|
|
yAdjust = -int(tileData.RoofHeight)
|
|
|
|
} else {
|
2019-11-01 22:12:07 -04:00
|
|
|
// Upper Walls, Special Tiles
|
|
|
|
yAdjust = int(tileMinY) + 80
|
2019-11-01 14:12:23 -04:00
|
|
|
}
|
2019-11-01 22:12:07 -04:00
|
|
|
|
2019-11-11 23:48:55 -05:00
|
|
|
// TODO: This may also need to be an atlas, but could get pretty large...
|
|
|
|
if err := image.ReplacePixels(pixels); err != nil {
|
|
|
|
log.Panicf(err.Error())
|
|
|
|
}
|
2019-11-01 14:12:23 -04:00
|
|
|
return &TileCacheRecord{
|
|
|
|
image,
|
2019-11-01 16:51:50 -04:00
|
|
|
0,
|
|
|
|
yAdjust,
|
2019-11-01 14:12:23 -04:00
|
|
|
}
|
2019-10-31 13:39:05 -04:00
|
|
|
}
|