-
Notifications
You must be signed in to change notification settings - Fork 9
/
corridorpather.go
134 lines (120 loc) · 3.36 KB
/
corridorpather.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright 2014,2015,2016,2017,2018,2019,2020,2021 SeukWon Kang (kasworld@gmail.com)
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package terrain
import (
"math"
"github.com/kasworld/goguelike/enum/way9type"
astar "github.com/kasworld/goguelike/lib/go-astar"
)
type CorridorPather struct {
X int
Y int
FF *Terrain
}
var dir2vt8 = [][2]int{
way9type.North: [2]int{0, -1},
way9type.NorthEast: [2]int{1, -1},
way9type.East: [2]int{1, 0},
way9type.SouthEast: [2]int{1, 1},
way9type.South: [2]int{0, 1},
way9type.SouthWest: [2]int{-1, 1},
way9type.West: [2]int{-1, 0},
way9type.NorthWest: [2]int{-1, -1},
}
var dir2vt4 = [][2]int{
way9type.North: [2]int{0, -1},
way9type.East: [2]int{1, 0},
way9type.South: [2]int{0, 1},
way9type.West: [2]int{-1, 0},
}
func (crp *CorridorPather) PathNeighbors() []astar.Pather {
rtn := make([]astar.Pather, 0, 8)
if crp.FF.crp8Way {
for _, v := range dir2vt8 {
x, y := crp.FF.WrapXY(crp.X+v[0], crp.Y+v[1])
if crp.FF.corridorPlaceable(x, y) {
rtn = append(rtn, crp.FF.newCRPather(x, y))
}
}
} else {
for _, v := range dir2vt4 {
x, y := crp.FF.WrapXY(crp.X+v[0], crp.Y+v[1])
if crp.FF.corridorPlaceable(x, y) {
rtn = append(rtn, crp.FF.newCRPather(x, y))
}
}
}
return rtn
}
func (crp *CorridorPather) PathNeighborCost(to astar.Pather) float64 {
toftp := to.(*CorridorPather)
w, h := crp.FF.GetXYLen()
contact, dir := way9type.CalcContactDirWrappedXY(
crp.X, crp.Y, toftp.X, toftp.Y, w, h)
if !contact {
crp.FF.log.Warn("invalid neighbor %v %v", crp, toftp)
}
return dir.Len()
}
func (crp *CorridorPather) PathEstimatedCost(to astar.Pather) float64 {
toftp := to.(*CorridorPather)
w, h := crp.FF.GetXYLen()
dx, dy := way9type.CalcDxDyWrapped(
toftp.X-crp.X, toftp.Y-crp.Y, w, h)
if crp.FF.crp8Way {
return math.Sqrt(float64(dx*dx + dy*dy))
} else {
if dx < 0 {
dx = -dx
}
if dy < 0 {
dy = -dy
}
return float64(dx + dy)
}
}
func (tr *Terrain) corridorPlaceable(x, y int) bool {
if tr.roomManager.GetRoomByPos(x, y) != nil {
return false
}
return true
}
func (tr *Terrain) newCRPather(x, y int) *CorridorPather {
x, y = tr.WrapXY(x, y)
crp := tr.crpCache[x][y]
if crp != nil {
return crp
}
crp = &CorridorPather{X: x, Y: y, FF: tr}
tr.crpCache[x][y] = crp
return crp
}
func (tr *Terrain) findCorridor(srcx, srcy, dstx, dsty int, trylimit int, way8 bool) [][2]int {
tr.crp8Way = way8
p, count := astar.Path2(
tr.newCRPather(srcx, srcy),
tr.newCRPather(dstx, dsty), trylimit, tr.Xlen*tr.Ylen)
if p != nil || count < trylimit {
rtn := make([][2]int, len(p))
for i, v := range p {
vv := v.(*CorridorPather)
rtn[len(p)-i-1] = [2]int{vv.X, vv.Y}
}
return rtn
}
return nil
}
func (tr *Terrain) initCrpCache() {
tr.crpCache = make([][]*CorridorPather, tr.Xlen)
for i, _ := range tr.crpCache {
tr.crpCache[i] = make([]*CorridorPather, tr.Ylen)
}
}