Skip to content

Commit

Permalink
Fix up subtile flag stuff (OpenDiablo2#288)
Browse files Browse the repository at this point in the history
* Switch to a 2d array for looking up subtile flags

Also fix up the walkableArea generation

* Check correct tiletype for walls

And fix the subtile tooltip text.
  • Loading branch information
Ziemas authored Feb 2, 2020
1 parent 241e8e2 commit 0b7a433
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
14 changes: 10 additions & 4 deletions d2common/d2fileformats/d2dt1/tile.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@ type Tile struct {
Blocks []Block
}

var subtileLookup = [5][5]int{
{20, 21, 22, 23, 24},
{15, 16, 17, 18, 19},
{10, 11, 12, 13, 14},
{5, 6, 7, 8, 9},
{0, 1, 2, 3, 4},
}

func (t *Tile) GetSubTileFlags(x, y int) *SubTileFlags {
if x < 0 || x > 4 || y < 0 || y > 4 {
return &SubTileFlags{}
}
return &t.SubTileFlags[x + (y * 5)]

return &t.SubTileFlags[subtileLookup[y][x]]
}
35 changes: 18 additions & 17 deletions d2core/d2map/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import (
)

type PathTile struct {
Walkable bool
Walkable bool
Up, Down, Left, Right, UpLeft, UpRight, DownLeft, DownRight *PathTile
X, Y float64
X, Y float64
}

func (t *PathTile) PathNeighbors() []astar.Pather {
Expand Down Expand Up @@ -153,19 +153,19 @@ func loadRegion(seed int64, tileOffsetX, tileOffsetY int, levelType d2enum.Regio
}

func (mr *MapRegion) generateWalkableMatrix() {
mr.walkableArea = make([][]PathTile, mr.tileRect.Height * 5)
for y := 0; y < mr.tileRect.Height * 5; y++ {
mr.walkableArea[y] = make([]PathTile, mr.tileRect.Width * 5)
mr.walkableArea = make([][]PathTile, mr.tileRect.Height*5)
for y := 0; y < mr.tileRect.Height*5; y++ {
mr.walkableArea[y] = make([]PathTile, mr.tileRect.Width*5)
ty := int(float64(y) / 5.0)
for x := 0; x < mr.tileRect.Width * 5; x++ {
for x := 0; x < mr.tileRect.Width*5; x++ {
tx := int(float64(x) / 5.0)
tile := mr.GetTile(tx, ty)
isBlocked := false
for _, floor := range tile.Floors {
tileData := mr.GetTileData(int32(floor.Style), int32(floor.Sequence), d2enum.Floor)
tileSubAttrs := &d2dt1.SubTileFlags{}
if tileData != nil {
tileSubAttrs = tileData.GetSubTileFlags(x % 5, 4 - (y % 5))
tileSubAttrs = tileData.GetSubTileFlags(x%5, y%5)
}
isBlocked = isBlocked || tileSubAttrs.BlockWalk
if isBlocked {
Expand All @@ -174,23 +174,23 @@ func (mr *MapRegion) generateWalkableMatrix() {
}
if !isBlocked {
for _, wall := range tile.Walls {
tileData := mr.GetTileData(int32(wall.Style), int32(wall.Sequence), d2enum.Floor)
tileData := mr.GetTileData(int32(wall.Style), int32(wall.Sequence), d2enum.TileType(wall.Type))
tileSubAttrs := &d2dt1.SubTileFlags{}
if tileData != nil {
tileSubAttrs = tileData.GetSubTileFlags(x % 5, 4 - (y % 5))
tileSubAttrs = tileData.GetSubTileFlags(x%5, y%5)
}
isBlocked = isBlocked || tileSubAttrs.BlockPlayerWalk
isBlocked = isBlocked || tileSubAttrs.BlockWalk
if isBlocked {
break
}
}
}
mr.walkableArea[y][x] = PathTile{
Walkable: !isBlocked,
X: float64(x) / 5.0,
Y: float64(y) / 5.0,
X: float64(x) / 5.0,
Y: float64(y) / 5.0,
}
if !isBlocked && y > 0 && mr.walkableArea[y-1][x].Walkable {
if !isBlocked && y > 0 && mr.walkableArea[y-1][x].Walkable {
mr.walkableArea[y][x].Up = &mr.walkableArea[y-1][x]
mr.walkableArea[y-1][x].Down = &mr.walkableArea[y][x]
}
Expand All @@ -202,7 +202,7 @@ func (mr *MapRegion) generateWalkableMatrix() {
mr.walkableArea[y][x].UpLeft = &mr.walkableArea[y-1][x-1]
mr.walkableArea[y-1][x-1].DownRight = &mr.walkableArea[y][x]
}
if !isBlocked && y > 0 && x < (mr.tileRect.Width * 5) && mr.walkableArea[y-1][x+1].Walkable {
if !isBlocked && y > 0 && x < (mr.tileRect.Width*5) && mr.walkableArea[y-1][x+1].Walkable {
mr.walkableArea[y][x].UpRight = &mr.walkableArea[y-1][x+1]
mr.walkableArea[y-1][x+1].DownLeft = &mr.walkableArea[y][x]
}
Expand Down Expand Up @@ -474,7 +474,7 @@ func (mr *MapRegion) renderWall(tile d2ds1.WallRecord, viewport *Viewport, targe
return
}

viewport.PushTranslationOrtho(-80, float64(tile.YAdjust) - 16)
viewport.PushTranslationOrtho(-80, float64(tile.YAdjust)-16)
defer viewport.PopTranslation()

target.PushTranslation(viewport.GetTranslationScreen())
Expand Down Expand Up @@ -521,7 +521,7 @@ func (mr *MapRegion) renderTileDebug(x, y int, debugVisLevel int, viewport *View
}
subTileColor := color.RGBA{R: 80, G: 80, B: 255, A: 50}
tileColor := color.RGBA{R: 255, G: 255, B: 255, A: 100}
tileCollisionColor := color.RGBA{R: 128, G:0, B:0, A:100}
tileCollisionColor := color.RGBA{R: 128, G: 0, B: 0, A: 100}

screenX1, screenY1 := viewport.WorldToScreen(float64(x), float64(y))
screenX2, screenY2 := viewport.WorldToScreen(float64(x+1), float64(y))
Expand Down Expand Up @@ -551,6 +551,7 @@ func (mr *MapRegion) renderTileDebug(x, y int, debugVisLevel int, viewport *View
}

tile := mr.ds1.Tiles[ay][ax]

for i, floor := range tile.Floors {
target.PushTranslation(-20, 10+(i+1)*14)
target.DrawText("f: %v-%v", floor.Style, floor.Sequence)
Expand All @@ -562,7 +563,7 @@ func (mr *MapRegion) renderTileDebug(x, y int, debugVisLevel int, viewport *View
isoX := (xx - yy) * 16
isoY := (xx + yy) * 8
target.PushTranslation(isoX-3, isoY+4)
var walkableArea = mr.walkableArea[yy + (ay * 5)][xx + (ax * 5)]
var walkableArea = mr.walkableArea[yy+(ay*5)][xx+(ax*5)]
if !walkableArea.Walkable {
target.DrawRect(5, 5, tileCollisionColor)
}
Expand Down
3 changes: 2 additions & 1 deletion d2game/d2gamescene/map_engine_testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,9 @@ func (met *MapEngineTest) Render(screen d2render.Surface) {

tileX := int(math.Floor(worldX)) - tileRect.Left
tileY := int(math.Floor(worldY)) - tileRect.Top

subtileX := int((worldX - float64(int(worldX))) * 5)
subtileY := 4 - int((worldY-float64(int(worldY)))*5)
subtileY := int((worldY - float64(int(worldY))) * 5)

regionWidth, regionHeight := curRegion.GetTileSize()
if tileX >= 0 && tileY >= 0 && tileX < regionWidth && tileY < regionHeight {
Expand Down

0 comments on commit 0b7a433

Please sign in to comment.