2020-02-01 18:55:56 -05:00
|
|
|
package d2map
|
2019-10-31 21:17:13 -04:00
|
|
|
|
|
|
|
import (
|
2020-06-13 18:32:09 -04:00
|
|
|
"log"
|
2020-02-02 12:46:19 -05:00
|
|
|
"math"
|
2019-10-31 21:17:13 -04:00
|
|
|
|
2020-04-11 14:56:47 -04:00
|
|
|
"github.com/beefsack/go-astar"
|
|
|
|
|
2020-02-02 14:04:37 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
|
2020-01-31 23:18:11 -05:00
|
|
|
|
2020-02-02 14:04:37 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2render"
|
2019-10-31 21:17:13 -04:00
|
|
|
)
|
|
|
|
|
2019-12-13 00:33:11 -05:00
|
|
|
type MapEntity interface {
|
2020-02-01 20:39:28 -05:00
|
|
|
Render(target d2render.Surface)
|
2019-12-13 00:33:11 -05:00
|
|
|
Advance(tickTime float64)
|
2019-12-28 16:46:08 -05:00
|
|
|
GetPosition() (float64, float64)
|
2019-10-31 21:17:13 -04:00
|
|
|
}
|
|
|
|
|
2020-06-18 14:11:04 -04:00
|
|
|
// Represents the map data for a specific location
|
2019-12-13 00:33:11 -05:00
|
|
|
type MapEngine struct {
|
2020-06-13 18:32:09 -04:00
|
|
|
seed int64
|
2019-12-13 00:33:11 -05:00
|
|
|
regions []*MapRegion
|
2020-02-26 08:40:32 -05:00
|
|
|
entities MapEntitiesSearcher
|
2019-10-31 21:17:13 -04:00
|
|
|
}
|
|
|
|
|
2020-06-18 14:11:04 -04:00
|
|
|
// Creates a new instance of the map engine
|
2020-06-19 02:19:27 -04:00
|
|
|
func CreateMapEngine(seed int64) *MapEngine {
|
2019-12-13 00:33:11 -05:00
|
|
|
engine := &MapEngine{
|
2020-06-19 02:19:27 -04:00
|
|
|
seed: seed,
|
2020-06-13 18:32:09 -04:00
|
|
|
entities: NewRangeSearcher(),
|
2019-10-31 21:17:13 -04:00
|
|
|
}
|
2019-12-08 22:18:42 -05:00
|
|
|
|
|
|
|
return engine
|
2019-10-31 21:17:13 -04:00
|
|
|
}
|
|
|
|
|
2020-06-19 02:19:27 -04:00
|
|
|
// Sets the seed of the map for generation
|
2020-06-13 18:32:09 -04:00
|
|
|
func (m *MapEngine) SetSeed(seed int64) {
|
|
|
|
log.Printf("Setting map engine seed to %d", seed)
|
|
|
|
m.seed = seed
|
|
|
|
}
|
|
|
|
|
2020-02-01 21:51:49 -05:00
|
|
|
func (m *MapEngine) GetStartPosition() (float64, float64) {
|
2019-12-13 00:33:11 -05:00
|
|
|
var startX, startY float64
|
2020-06-19 02:19:27 -04:00
|
|
|
|
|
|
|
// TODO: Temporary code, only works for starting map
|
2020-02-01 21:51:49 -05:00
|
|
|
if len(m.regions) > 0 {
|
|
|
|
region := m.regions[0]
|
2019-12-13 00:33:11 -05:00
|
|
|
startX, startY = region.getStartTilePosition()
|
|
|
|
}
|
2019-12-06 09:44:52 -05:00
|
|
|
|
2019-12-13 00:33:11 -05:00
|
|
|
return startX, startY
|
2019-12-06 09:44:52 -05:00
|
|
|
}
|
|
|
|
|
2020-06-18 14:11:04 -04:00
|
|
|
// Returns the center of the map
|
2020-02-01 21:51:49 -05:00
|
|
|
func (m *MapEngine) GetCenterPosition() (float64, float64) {
|
2019-12-13 00:33:11 -05:00
|
|
|
var centerX, centerY float64
|
2020-02-01 21:51:49 -05:00
|
|
|
if len(m.regions) > 0 {
|
|
|
|
region := m.regions[0]
|
2019-12-13 00:33:11 -05:00
|
|
|
centerX = float64(region.tileRect.Left) + float64(region.tileRect.Width)/2
|
|
|
|
centerY = float64(region.tileRect.Top) + float64(region.tileRect.Height)/2
|
|
|
|
}
|
2019-11-17 01:14:58 -05:00
|
|
|
|
2019-12-13 00:33:11 -05:00
|
|
|
return centerX, centerY
|
2019-12-08 22:18:42 -05:00
|
|
|
}
|
|
|
|
|
2020-06-13 18:32:09 -04:00
|
|
|
func (m *MapEngine) GenerateMap(regionType d2enum.RegionIdType, levelPreset int, fileIndex int, cacheTiles bool) {
|
|
|
|
region, entities := loadRegion(m.seed, 0, 0, regionType, levelPreset, fileIndex, cacheTiles)
|
2020-02-01 21:51:49 -05:00
|
|
|
m.regions = append(m.regions, region)
|
2020-02-26 08:40:32 -05:00
|
|
|
m.entities.Add(entities...)
|
2019-11-06 21:25:09 -05:00
|
|
|
}
|
|
|
|
|
2020-06-19 02:19:27 -04:00
|
|
|
// Appends a region to the map
|
|
|
|
func (m *MapEngine) AppendRegion(region *MapRegion) {
|
2020-02-01 21:51:49 -05:00
|
|
|
m.regions = append(m.regions, region)
|
2020-06-19 02:19:27 -04:00
|
|
|
// Stitch together the walk map
|
2020-06-20 14:04:51 -04:00
|
|
|
|
|
|
|
// Top/Bottom
|
2020-06-19 02:19:27 -04:00
|
|
|
for x := 0; x < region.tileRect.Width*5; x++ {
|
|
|
|
otherRegion := m.GetRegionAtTile(region.tileRect.Left+(x/5), region.tileRect.Top-1)
|
|
|
|
if otherRegion == nil {
|
|
|
|
continue
|
|
|
|
}
|
2020-06-20 14:04:51 -04:00
|
|
|
xDiff := (region.tileRect.Left - otherRegion.tileRect.Left) * 5
|
2019-11-06 21:25:09 -05:00
|
|
|
|
2020-06-19 02:19:27 -04:00
|
|
|
sourceSubtile := ®ion.walkableArea[0][x]
|
|
|
|
if !sourceSubtile.Walkable {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// North West
|
|
|
|
otherX := x + xDiff - 1
|
|
|
|
otherY := (otherRegion.tileRect.Height * 5) - 1
|
|
|
|
if otherX < 0 || otherX >= len(otherRegion.walkableArea[otherY]) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
otherRegion.walkableArea[otherY][x+xDiff].DownRight = sourceSubtile
|
|
|
|
sourceSubtile.UpLeft = &otherRegion.walkableArea[otherY][x+xDiff]
|
|
|
|
|
|
|
|
// North
|
|
|
|
otherX++
|
|
|
|
if otherX < 0 || otherX >= len(otherRegion.walkableArea[otherY]) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
otherRegion.walkableArea[otherY][x+xDiff].Down = sourceSubtile
|
|
|
|
sourceSubtile.Up = &otherRegion.walkableArea[otherY][x+xDiff]
|
|
|
|
|
|
|
|
// NorthEast
|
|
|
|
otherX++
|
|
|
|
if otherX < 0 || otherX >= len(otherRegion.walkableArea[otherY]) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
otherRegion.walkableArea[otherY][x+xDiff].DownLeft = sourceSubtile
|
|
|
|
sourceSubtile.UpRight = &otherRegion.walkableArea[otherY][x+xDiff]
|
2019-11-17 08:09:54 -05:00
|
|
|
}
|
|
|
|
|
2020-06-20 14:04:51 -04:00
|
|
|
// West/East
|
|
|
|
for y := 0; y < region.tileRect.Height*5; y++ {
|
|
|
|
otherRegion := m.GetRegionAtTile(region.tileRect.Left-1, region.tileRect.Top+(y/5))
|
|
|
|
if otherRegion == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
yDiff := (region.tileRect.Top - otherRegion.tileRect.Top) * 5
|
|
|
|
|
|
|
|
sourceSubtile := ®ion.walkableArea[y][0]
|
|
|
|
if !sourceSubtile.Walkable {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// North West
|
|
|
|
otherX := (otherRegion.tileRect.Width * 5) - 1
|
|
|
|
otherY := y + yDiff - 1
|
|
|
|
if otherY < 0 || otherY >= len(otherRegion.walkableArea) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
otherRegion.walkableArea[y+yDiff][otherX].DownRight = sourceSubtile
|
|
|
|
sourceSubtile.UpLeft = &otherRegion.walkableArea[y+yDiff][otherX]
|
|
|
|
|
|
|
|
// West
|
|
|
|
otherY++
|
|
|
|
if otherY < 0 || otherY >= len(otherRegion.walkableArea) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
otherRegion.walkableArea[y+yDiff][otherX].Right = sourceSubtile
|
|
|
|
sourceSubtile.Left = &otherRegion.walkableArea[y+yDiff][otherX]
|
|
|
|
|
|
|
|
// South East
|
|
|
|
otherY++
|
|
|
|
if otherY < 0 || otherY >= len(otherRegion.walkableArea) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
otherRegion.walkableArea[y+yDiff][otherX].UpRight = sourceSubtile
|
|
|
|
sourceSubtile.DownLeft = &otherRegion.walkableArea[y+yDiff][otherX]
|
|
|
|
}
|
|
|
|
|
2020-02-02 12:46:19 -05:00
|
|
|
}
|
|
|
|
|
2020-06-18 14:11:04 -04:00
|
|
|
// Returns the region located at the specified tile location
|
2020-02-01 21:51:49 -05:00
|
|
|
func (m *MapEngine) GetRegionAtTile(x, y int) *MapRegion {
|
2020-06-20 14:04:51 -04:00
|
|
|
// Read in reverse order as tiles can be placed over other tiles, and we prioritize the top level tiles
|
|
|
|
for i := len(m.regions) - 1; i >= 0; i-- {
|
|
|
|
region := m.regions[i]
|
2019-12-13 00:33:11 -05:00
|
|
|
if region.tileRect.IsInRect(x, y) {
|
|
|
|
return region
|
2019-11-16 22:53:55 -05:00
|
|
|
}
|
|
|
|
}
|
2019-12-13 00:33:11 -05:00
|
|
|
return nil
|
2019-11-16 22:53:55 -05:00
|
|
|
}
|
|
|
|
|
2020-06-19 02:19:27 -04:00
|
|
|
// Adds an entity to the map engine
|
2020-02-01 21:51:49 -05:00
|
|
|
func (m *MapEngine) AddEntity(entity MapEntity) {
|
2020-02-26 08:40:32 -05:00
|
|
|
m.entities.Add(entity)
|
2019-11-15 11:03:58 -05:00
|
|
|
}
|
|
|
|
|
2020-06-19 02:19:27 -04:00
|
|
|
// Removes an entity from the map engine
|
2020-02-22 20:44:30 -05:00
|
|
|
func (m *MapEngine) RemoveEntity(entity MapEntity) {
|
|
|
|
if entity == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-26 08:40:32 -05:00
|
|
|
m.entities.Remove(entity)
|
2020-02-22 20:44:30 -05:00
|
|
|
}
|
|
|
|
|
2020-06-19 02:19:27 -04:00
|
|
|
// Advances time on the map engine
|
2020-02-01 21:51:49 -05:00
|
|
|
func (m *MapEngine) Advance(tickTime float64) {
|
|
|
|
for _, region := range m.regions {
|
2020-06-13 18:32:09 -04:00
|
|
|
//if region.isVisbile(m.viewport) {
|
|
|
|
region.advance(tickTime)
|
|
|
|
//}
|
2019-11-16 22:53:55 -05:00
|
|
|
}
|
|
|
|
|
2020-02-26 08:40:32 -05:00
|
|
|
for _, entity := range m.entities.All() {
|
2019-12-13 00:33:11 -05:00
|
|
|
entity.Advance(tickTime)
|
2019-11-01 14:12:23 -04:00
|
|
|
}
|
2020-02-26 08:40:32 -05:00
|
|
|
|
|
|
|
m.entities.Update()
|
2019-11-16 22:53:55 -05:00
|
|
|
}
|
2019-11-15 09:04:27 -05:00
|
|
|
|
2020-06-19 02:19:27 -04:00
|
|
|
// Finds a walkable path between two points
|
2020-02-22 20:44:30 -05:00
|
|
|
func (m *MapEngine) PathFind(startX, startY, endX, endY float64) (path []astar.Pather, distance float64, found bool) {
|
2020-02-02 12:46:19 -05:00
|
|
|
startTileX := int(math.Floor(startX))
|
|
|
|
startTileY := int(math.Floor(startY))
|
|
|
|
startSubtileX := int((startX - float64(int(startX))) * 5)
|
|
|
|
startSubtileY := int((startY - float64(int(startY))) * 5)
|
|
|
|
startRegion := m.GetRegionAtTile(startTileX, startTileY)
|
2020-06-19 02:19:27 -04:00
|
|
|
if startRegion == nil {
|
|
|
|
return
|
|
|
|
}
|
2020-02-22 20:44:30 -05:00
|
|
|
startNode := &startRegion.walkableArea[startSubtileY+((startTileY-startRegion.tileRect.Top)*5)][startSubtileX+((startTileX-startRegion.tileRect.Left)*5)]
|
2020-02-02 12:46:19 -05:00
|
|
|
|
|
|
|
endTileX := int(math.Floor(endX))
|
|
|
|
endTileY := int(math.Floor(endY))
|
|
|
|
endSubtileX := int((endX - float64(int(endX))) * 5)
|
|
|
|
endSubtileY := int((endY - float64(int(endY))) * 5)
|
|
|
|
endRegion := m.GetRegionAtTile(endTileX, endTileY)
|
2020-06-19 02:19:27 -04:00
|
|
|
if endRegion == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
endNodeY := endSubtileY + ((endTileY - endRegion.tileRect.Top) * 5)
|
|
|
|
endNodeX := endSubtileX + ((endTileX - endRegion.tileRect.Left) * 5)
|
|
|
|
if endNodeY < 0 || endNodeY >= len(endRegion.walkableArea) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if endNodeX < 0 || endNodeX >= len(endRegion.walkableArea[endNodeY]) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
endNode := &endRegion.walkableArea[endNodeY][endNodeX]
|
2020-02-02 12:46:19 -05:00
|
|
|
|
|
|
|
path, distance, found = astar.Path(endNode, startNode)
|
|
|
|
if path != nil {
|
|
|
|
path = path[1:]
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|