2019-11-10 08:51:02 -05:00
|
|
|
package d2mapengine
|
2019-10-31 13:39:05 -04:00
|
|
|
|
|
|
|
import (
|
2019-11-01 22:12:07 -04:00
|
|
|
"image/color"
|
|
|
|
"log"
|
2019-10-31 13:39:05 -04:00
|
|
|
"math"
|
|
|
|
"math/rand"
|
2019-11-01 14:12:23 -04:00
|
|
|
"strconv"
|
|
|
|
|
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/d2datadict"
|
2019-11-10 03:36:53 -05:00
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten"
|
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-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-11-17 14:21:48 -05:00
|
|
|
imageCacheRecords map[uint32]*ebiten.Image
|
2019-11-19 00:38:02 -05:00
|
|
|
seed int64
|
2019-11-24 01:11:32 -05:00
|
|
|
currentFrame byte
|
|
|
|
lastFrameTime float64
|
2019-10-31 13:39:05 -04:00
|
|
|
}
|
|
|
|
|
2019-11-18 22:29:34 -05:00
|
|
|
func LoadRegion(seed int64, levelType d2enum.RegionIdType, levelPreset int, fileProvider d2interface.FileProvider, fileIndex int) *Region {
|
2019-10-31 13:39:05 -04:00
|
|
|
result := &Region{
|
2019-11-17 14:21:48 -05:00
|
|
|
LevelType: d2datadict.LevelTypes[levelType],
|
|
|
|
LevelPreset: d2datadict.LevelPresets[levelPreset],
|
|
|
|
Tiles: make([]d2dt1.Tile, 0),
|
|
|
|
imageCacheRecords: map[uint32]*ebiten.Image{},
|
2019-11-19 00:38:02 -05:00
|
|
|
seed: seed,
|
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-23 08:48:33 -05:00
|
|
|
// Temp hack
|
|
|
|
if levelType == d2enum.RegionAct5Lava {
|
|
|
|
result.Palette = d2datadict.Palettes[d2enum.PaletteType("act4")]
|
|
|
|
}
|
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)
|
|
|
|
}
|
2019-11-21 19:54:56 -05:00
|
|
|
levelIndex := int(math.Round(float64(len(levelFilesToPick)-1) * rand.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-21 19:54:56 -05:00
|
|
|
|
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-24 01:11:32 -05:00
|
|
|
result.currentFrame = 0
|
2019-11-07 23:44:03 -05:00
|
|
|
result.loadObjects(fileProvider)
|
2019-11-17 01:14:58 -05:00
|
|
|
result.loadSpecials()
|
2019-11-07 23:44:03 -05:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2019-11-17 01:14:58 -05:00
|
|
|
func (v *Region) loadSpecials() {
|
|
|
|
for y := range v.DS1.Tiles {
|
|
|
|
for x := range v.DS1.Tiles[y] {
|
|
|
|
for _, wall := range v.DS1.Tiles[y][x].Walls {
|
2019-11-21 20:34:29 -05:00
|
|
|
if wall.Type != 10 {
|
2019-11-17 01:14:58 -05:00
|
|
|
continue
|
|
|
|
}
|
2019-11-21 20:34:29 -05:00
|
|
|
if wall.Style == 30 && wall.Sequence == 0 {
|
2019-11-17 01:14:58 -05:00
|
|
|
v.StartX = float64(x) + 0.5
|
|
|
|
v.StartY = float64(y) + 0.5
|
|
|
|
log.Printf("Starting location: %d, %d", x, y)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-10 03:36:53 -05:00
|
|
|
func (v *Region) loadObjects(fileProvider d2interface.FileProvider) {
|
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-17 14:21:48 -05:00
|
|
|
switch object.Lookup.Type {
|
|
|
|
case d2datadict.ObjectTypeCharacter:
|
|
|
|
// Temp code, maybe..
|
|
|
|
if object.Lookup.Base == "" || object.Lookup.Token == "" || object.Lookup.TR == "" {
|
|
|
|
continue
|
2019-11-05 22:23:48 -05:00
|
|
|
}
|
2019-11-17 14:21:48 -05:00
|
|
|
npc := d2core.CreateNPC(object.X, object.Y, object.Lookup, fileProvider, 1)
|
|
|
|
npc.SetPaths(object.Paths)
|
|
|
|
v.NPCs = append(v.NPCs, npc)
|
|
|
|
case d2datadict.ObjectTypeItem:
|
|
|
|
if object.ObjectInfo == nil || !object.ObjectInfo.Draw || object.Lookup.Base == "" || object.Lookup.Token == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
entity := d2render.CreateAnimatedEntity(object.X, object.Y, object.Lookup, fileProvider, d2enum.Units)
|
|
|
|
entity.SetMode(object.Lookup.Mode, object.Lookup.Class, 0)
|
|
|
|
v.AnimationEntities = append(v.AnimationEntities, entity)
|
|
|
|
}
|
2019-11-05 22:23:48 -05:00
|
|
|
}
|
2019-10-31 13:39:05 -04:00
|
|
|
}
|
|
|
|
|
2019-11-24 01:11:32 -05:00
|
|
|
func (v *Region) UpdateAnimations() {
|
|
|
|
now := d2helper.Now()
|
|
|
|
framesToAdd := math.Floor((now - v.lastFrameTime) / 0.1)
|
|
|
|
if framesToAdd > 0 {
|
|
|
|
v.lastFrameTime += 0.1 * framesToAdd
|
|
|
|
v.currentFrame += byte(math.Floor(framesToAdd))
|
|
|
|
if v.currentFrame > 9 {
|
|
|
|
v.currentFrame = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-24 01:11:32 -05:00
|
|
|
func (v *Region) getRandomTile(tiles []d2dt1.Tile, x, y int, seed int64) byte {
|
2019-11-21 19:54:56 -05:00
|
|
|
/* Walker's Alias Method for weighted random selection
|
|
|
|
* with xorshifting for random numbers */
|
|
|
|
|
|
|
|
var tileSeed uint64
|
|
|
|
tileSeed = uint64(seed) + uint64(x)
|
|
|
|
tileSeed *= uint64(y) + uint64(v.LevelType.Id)
|
|
|
|
|
|
|
|
tileSeed ^= tileSeed << 13
|
|
|
|
tileSeed ^= tileSeed >> 17
|
|
|
|
tileSeed ^= tileSeed << 5
|
|
|
|
|
|
|
|
weightSum := 0
|
|
|
|
for _, tile := range tiles {
|
|
|
|
weightSum += int(tile.RarityFrameIndex)
|
|
|
|
}
|
|
|
|
|
|
|
|
if weightSum == 0 {
|
2019-11-24 01:11:32 -05:00
|
|
|
return 0
|
2019-11-21 19:54:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
random := (tileSeed % uint64(weightSum))
|
|
|
|
|
|
|
|
sum := 0
|
|
|
|
for i, tile := range tiles {
|
|
|
|
sum += int(tile.RarityFrameIndex)
|
|
|
|
if sum >= int(random) {
|
2019-11-24 01:11:32 -05:00
|
|
|
return byte(i)
|
2019-11-21 19:54:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This return shouldn't be hit
|
2019-11-24 01:11:32 -05:00
|
|
|
return 0
|
2019-11-17 00:52:13 -05:00
|
|
|
}
|
|
|
|
|
2019-11-24 01:11:32 -05:00
|
|
|
func (v *Region) getTiles(style, sequence, tileType int32, x, y int, seed int64) []d2dt1.Tile {
|
2019-11-19 00:38:02 -05:00
|
|
|
var tiles []d2dt1.Tile
|
2019-10-31 13:39:05 -04:00
|
|
|
for _, tile := range v.Tiles {
|
2019-11-21 20:34:29 -05:00
|
|
|
if tile.Style != style || tile.Sequence != sequence || tile.Type != tileType {
|
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 {
|
2019-11-21 20:34:29 -05:00
|
|
|
log.Printf("Unknown tile ID [%d %d %d]\n", style, sequence, tileType)
|
2019-11-24 01:11:32 -05:00
|
|
|
return nil
|
2019-10-31 13:39:05 -04:00
|
|
|
}
|
2019-11-24 01:11:32 -05:00
|
|
|
return 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) {
|
2019-11-24 01:11:32 -05:00
|
|
|
var img *ebiten.Image
|
|
|
|
if !tile.Animated {
|
|
|
|
img = v.GetImageCacheRecord(tile.Style, tile.Sequence, 0, tile.RandomIndex)
|
|
|
|
} else {
|
|
|
|
img = v.GetImageCacheRecord(tile.Style, tile.Sequence, 0, v.currentFrame)
|
|
|
|
}
|
2019-11-17 14:21:48 -05:00
|
|
|
if img == nil {
|
2019-11-21 20:34:29 -05:00
|
|
|
log.Printf("Render called on uncached floor {%v,%v}", tile.Style, tile.Sequence)
|
2019-11-21 19:54:56 -05:00
|
|
|
return
|
2019-11-17 14:21:48 -05:00
|
|
|
}
|
2019-11-21 19:54:56 -05:00
|
|
|
opts := &ebiten.DrawImageOptions{}
|
2019-11-17 14:21:48 -05:00
|
|
|
opts.GeoM.Translate(float64(offsetX), float64(offsetY))
|
|
|
|
_ = target.DrawImage(img, 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) {
|
2019-11-21 20:34:29 -05:00
|
|
|
img := v.GetImageCacheRecord(tile.Style, tile.Sequence, tile.Type, tile.RandomIndex)
|
2019-11-17 14:21:48 -05:00
|
|
|
if img == nil {
|
2019-11-21 20:34:29 -05:00
|
|
|
log.Printf("Render called on uncached wall {%v,%v,%v}", tile.Style, tile.Sequence, tile.Type)
|
2019-11-13 14:26:42 -05:00
|
|
|
return
|
|
|
|
}
|
2019-11-01 22:12:07 -04:00
|
|
|
opts := &ebiten.DrawImageOptions{}
|
2019-11-21 19:54:56 -05:00
|
|
|
opts.GeoM.Translate(float64(offsetX), float64(offsetY+tile.YAdjust))
|
2019-11-17 14:21:48 -05:00
|
|
|
target.DrawImage(img, opts)
|
2019-11-01 22:12:07 -04:00
|
|
|
}
|
|
|
|
|
2019-11-17 00:52:13 -05:00
|
|
|
func (v *Region) renderShadow(tile d2ds1.FloorShadowRecord, offsetX, offsetY int, target *ebiten.Image, tileX, tileY int) {
|
2019-11-21 20:34:29 -05:00
|
|
|
img := v.GetImageCacheRecord(tile.Style, tile.Sequence, 13, tile.RandomIndex)
|
2019-11-17 14:21:48 -05:00
|
|
|
if img == nil {
|
2019-11-21 20:34:29 -05:00
|
|
|
log.Printf("Render called on uncached shadow {%v,%v}", tile.Style, tile.Sequence)
|
2019-11-13 14:26:42 -05:00
|
|
|
return
|
|
|
|
}
|
2019-11-01 16:51:50 -04:00
|
|
|
opts := &ebiten.DrawImageOptions{}
|
2019-11-21 19:54:56 -05:00
|
|
|
opts.GeoM.Translate(float64(offsetX), float64(offsetY+tile.YAdjust))
|
2019-11-17 00:16:33 -05:00
|
|
|
opts.ColorM = d2corehelper.ColorToColorM(color.RGBA{255, 255, 255, 160})
|
2019-11-17 14:21:48 -05:00
|
|
|
target.DrawImage(img, opts)
|
2019-11-01 16:51:50 -04:00
|
|
|
}
|
2019-11-01 14:12:23 -04:00
|
|
|
|
2019-11-17 14:21:48 -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))
|
2019-11-17 14:21:48 -05:00
|
|
|
(*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))
|
2019-11-17 14:21:48 -05:00
|
|
|
(*pixels)[offset] = pixelColor.R
|
|
|
|
(*pixels)[offset+1] = pixelColor.G
|
|
|
|
(*pixels)[offset+2] = pixelColor.B
|
|
|
|
(*pixels)[offset+3] = 255
|
2019-11-01 22:12:07 -04:00
|
|
|
|
2019-11-01 14:12:23 -04:00
|
|
|
}
|
|
|
|
idx++
|
|
|
|
x++
|
|
|
|
b2--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-01 16:51:50 -04:00
|
|
|
}
|
|
|
|
|
2019-11-24 01:11:32 -05:00
|
|
|
func (v *Region) generateFloorCache(tile *d2ds1.FloorShadowRecord, tileX, tileY int) {
|
|
|
|
tileOptions := v.getTiles(int32(tile.Style), int32(tile.Sequence), 0, tileX, tileY, v.seed)
|
|
|
|
var tileData []*d2dt1.Tile
|
|
|
|
var tileIndex byte
|
|
|
|
|
|
|
|
if tileOptions == nil {
|
2019-11-21 20:34:29 -05:00
|
|
|
log.Printf("Could not locate tile Style:%d, Seq: %d, Type: %d\n", tile.Style, tile.Sequence, 0)
|
2019-11-24 01:11:32 -05:00
|
|
|
tileData = append(tileData, &d2dt1.Tile{})
|
|
|
|
tileData[0].Width = 10
|
|
|
|
tileData[0].Height = 10
|
|
|
|
} else {
|
|
|
|
if !tileOptions[0].MaterialFlags.Animated {
|
|
|
|
tileIndex = v.getRandomTile(tileOptions, tileX, tileY, v.seed)
|
|
|
|
tileData = append(tileData, &tileOptions[tileIndex])
|
|
|
|
} else {
|
|
|
|
tile.Animated = true
|
|
|
|
for i := range tileOptions {
|
|
|
|
tileData = append(tileData, &tileOptions[i])
|
|
|
|
}
|
|
|
|
}
|
2019-11-17 14:21:48 -05:00
|
|
|
}
|
2019-11-24 01:11:32 -05:00
|
|
|
|
|
|
|
for i := range tileData {
|
|
|
|
if !tileData[i].MaterialFlags.Animated {
|
|
|
|
tile.RandomIndex = tileIndex
|
|
|
|
} else {
|
|
|
|
tileIndex = byte(tileData[i].RarityFrameIndex)
|
|
|
|
}
|
|
|
|
cachedImage := v.GetImageCacheRecord(tile.Style, tile.Sequence, 0, tileIndex)
|
|
|
|
if cachedImage != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
tileYMinimum := int32(0)
|
|
|
|
for _, block := range tileData[i].Blocks {
|
|
|
|
tileYMinimum = d2helper.MinInt32(tileYMinimum, int32(block.Y))
|
|
|
|
}
|
|
|
|
tileYOffset := d2helper.AbsInt32(tileYMinimum)
|
|
|
|
tileHeight := d2helper.AbsInt32(tileData[i].Height)
|
|
|
|
image, _ := ebiten.NewImage(int(tileData[i].Width), int(tileHeight), ebiten.FilterNearest)
|
|
|
|
pixels := make([]byte, 4*tileData[i].Width*tileHeight)
|
|
|
|
v.decodeTileGfxData(tileData[i].Blocks, &pixels, tileYOffset, tileData[i].Width)
|
|
|
|
image.ReplacePixels(pixels)
|
|
|
|
v.SetImageCacheRecord(tile.Style, tile.Sequence, 0, tileIndex, image)
|
2019-11-01 16:51:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-24 01:11:32 -05:00
|
|
|
func (v *Region) generateShadowCache(tile *d2ds1.FloorShadowRecord, tileX, tileY int) {
|
|
|
|
tileOptions := v.getTiles(int32(tile.Style), int32(tile.Sequence), 13, tileX, tileY, v.seed)
|
|
|
|
var tileIndex byte
|
|
|
|
var tileData *d2dt1.Tile
|
|
|
|
if tileOptions == nil {
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
tileIndex = v.getRandomTile(tileOptions, tileX, tileY, v.seed)
|
|
|
|
tileData = &tileOptions[tileIndex]
|
2019-11-01 16:51:50 -04:00
|
|
|
}
|
2019-11-24 01:11:32 -05:00
|
|
|
|
2019-11-19 00:38:02 -05:00
|
|
|
tile.RandomIndex = tileIndex
|
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-21 19:54:56 -05:00
|
|
|
tile.YAdjust = int(tileMinY + 80)
|
|
|
|
|
2019-11-21 20:34:29 -05:00
|
|
|
cachedImage := v.GetImageCacheRecord(tile.Style, tile.Sequence, 13, tileIndex)
|
2019-11-21 19:54:56 -05:00
|
|
|
if cachedImage != nil {
|
2019-11-24 01:11:32 -05:00
|
|
|
return
|
2019-11-21 19:54:56 -05:00
|
|
|
}
|
|
|
|
|
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))
|
2019-11-17 14:21:48 -05:00
|
|
|
v.decodeTileGfxData(tileData.Blocks, &pixels, tileYOffset, tileData.Width)
|
2019-11-01 14:12:23 -04:00
|
|
|
image.ReplacePixels(pixels)
|
2019-11-21 20:34:29 -05:00
|
|
|
v.SetImageCacheRecord(tile.Style, tile.Sequence, 13, tileIndex, image)
|
2019-11-01 22:12:07 -04:00
|
|
|
}
|
|
|
|
|
2019-11-24 01:11:32 -05:00
|
|
|
func (v *Region) generateWallCache(tile *d2ds1.WallRecord, tileX, tileY int) {
|
|
|
|
tileOptions := v.getTiles(int32(tile.Style), int32(tile.Sequence), int32(tile.Type), tileX, tileY, v.seed)
|
|
|
|
var tileIndex byte
|
|
|
|
var tileData *d2dt1.Tile
|
|
|
|
if tileOptions == nil {
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
tileIndex = v.getRandomTile(tileOptions, tileX, tileY, v.seed)
|
|
|
|
tileData = &tileOptions[tileIndex]
|
2019-11-01 22:12:07 -04:00
|
|
|
}
|
2019-11-21 19:54:56 -05:00
|
|
|
|
2019-11-19 00:38:02 -05:00
|
|
|
tile.RandomIndex = tileIndex
|
2019-11-10 10:44:13 -05:00
|
|
|
var newTileData *d2dt1.Tile = nil
|
2019-11-21 19:54:56 -05:00
|
|
|
|
2019-11-21 20:34:29 -05:00
|
|
|
if tile.Type == 3 {
|
2019-11-24 01:11:32 -05:00
|
|
|
newTileOptions := v.getTiles(int32(tile.Style), int32(tile.Sequence), int32(4), tileX, tileY, v.seed)
|
|
|
|
newTileIndex := v.getRandomTile(newTileOptions, tileX, tileY, v.seed)
|
|
|
|
newTileData = &newTileOptions[newTileIndex]
|
2019-11-01 22:12:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
tileMinY := int32(0)
|
|
|
|
tileMaxY := int32(0)
|
2019-11-21 19:54:56 -05:00
|
|
|
|
2019-11-01 22:12:07 -04:00
|
|
|
target := tileData
|
2019-11-21 19:54:56 -05:00
|
|
|
|
2019-11-01 22:12:07 -04:00
|
|
|
if newTileData != nil && newTileData.Height < tileData.Height {
|
|
|
|
target = newTileData
|
|
|
|
}
|
2019-11-21 19:54:56 -05:00
|
|
|
|
2019-11-01 22:12:07 -04:00
|
|
|
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-21 19:54:56 -05: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)
|
2019-11-17 14:21:48 -05:00
|
|
|
|
2019-11-21 20:34:29 -05:00
|
|
|
if tile.Type == 15 {
|
2019-11-21 19:54:56 -05:00
|
|
|
tile.YAdjust = -int(tileData.RoofHeight)
|
|
|
|
} else {
|
|
|
|
tile.YAdjust = int(tileMinY) + 80
|
|
|
|
}
|
|
|
|
|
2019-11-21 20:34:29 -05:00
|
|
|
cachedImage := v.GetImageCacheRecord(tile.Style, tile.Sequence, tile.Type, tileIndex)
|
2019-11-17 14:21:48 -05:00
|
|
|
if cachedImage != nil {
|
2019-11-24 01:11:32 -05:00
|
|
|
return
|
2019-11-17 14:21:48 -05:00
|
|
|
}
|
2019-11-21 19:54:56 -05:00
|
|
|
|
2019-11-17 16:06:02 -05:00
|
|
|
if realHeight == 0 {
|
|
|
|
log.Printf("Invalid 0 height for wall tile")
|
2019-11-24 01:11:32 -05:00
|
|
|
return
|
2019-11-17 16:06:02 -05:00
|
|
|
}
|
2019-11-21 19:54:56 -05:00
|
|
|
|
2019-11-01 22:12:07 -04:00
|
|
|
image, _ := ebiten.NewImage(160, int(realHeight), ebiten.FilterNearest)
|
|
|
|
pixels := make([]byte, 4*160*realHeight)
|
2019-11-17 14:21:48 -05:00
|
|
|
v.decodeTileGfxData(tileData.Blocks, &pixels, tileYOffset, 160)
|
2019-11-21 19:54:56 -05:00
|
|
|
|
2019-11-01 22:12:07 -04:00
|
|
|
if newTileData != nil {
|
2019-11-17 14:21:48 -05:00
|
|
|
v.decodeTileGfxData(newTileData.Blocks, &pixels, tileYOffset, 160)
|
2019-11-01 22:12:07 -04:00
|
|
|
}
|
2019-11-21 19:54:56 -05:00
|
|
|
|
2019-11-11 23:48:55 -05:00
|
|
|
if err := image.ReplacePixels(pixels); err != nil {
|
|
|
|
log.Panicf(err.Error())
|
|
|
|
}
|
2019-11-21 19:54:56 -05:00
|
|
|
|
2019-11-21 20:34:29 -05:00
|
|
|
v.SetImageCacheRecord(tile.Style, tile.Sequence, tile.Type, tileIndex, image)
|
2019-11-17 14:21:48 -05:00
|
|
|
}
|
|
|
|
|
2019-11-22 23:30:02 -05:00
|
|
|
func (v *Region) GetImageCacheRecord(style, sequence byte, tileType d2enum.TileType, randomIndex byte) *ebiten.Image {
|
2019-11-21 20:34:29 -05:00
|
|
|
lookupIndex := uint32(style)<<24 | uint32(sequence)<<16 | uint32(tileType)<<8 | uint32(randomIndex)
|
2019-11-17 14:21:48 -05:00
|
|
|
return v.imageCacheRecords[lookupIndex]
|
|
|
|
}
|
|
|
|
|
2019-11-22 23:30:02 -05:00
|
|
|
func (v *Region) SetImageCacheRecord(style, sequence byte, tileType d2enum.TileType, randomIndex byte, image *ebiten.Image) {
|
2019-11-21 20:34:29 -05:00
|
|
|
lookupIndex := uint32(style)<<24 | uint32(sequence)<<16 | uint32(tileType)<<8 | uint32(randomIndex)
|
2019-11-17 14:21:48 -05:00
|
|
|
v.imageCacheRecords[lookupIndex] = image
|
2019-10-31 13:39:05 -04:00
|
|
|
}
|