-
Notifications
You must be signed in to change notification settings - Fork 1
/
ghost.go
201 lines (162 loc) · 3.49 KB
/
ghost.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import "math/rand"
type ghost struct {
kind string
path []position
cooldown int
}
func ghostCanMove(p position, g *ghost, newGhostMap map[position]*ghost, m model) bool {
if t := m.level().tombstoneMap[p]; t != nil {
return false
}
if ghost := m.level().ghostMap[p]; ghost != nil {
return false
}
if ghost := newGhostMap[p]; ghost != nil {
return false
}
return true
}
func findPathForGhost(p position, g ghost, m model) []position {
if g.kind == "hunt" && playerNearby(p, m, 20) {
return huntPath(p, m)
}
if g.kind == "follow" && playerNearby(p, m, 20) {
return followPath(p, m)
}
return wanderPath(p, m)
}
func wanderPath(p position, m model) []position {
var xTarget, yTarget int
for {
xTarget = rand.Intn(10) - 5
yTarget = rand.Intn(10) - 5
if xTarget+p.x > 0 && xTarget+p.x < m.level().width-1 && yTarget+p.y > 0 &&
yTarget+p.y < m.level().height-1 {
break
}
}
xTarget += p.x
yTarget += p.y
path := []position{}
for {
if xTarget != p.x && yTarget != p.y {
if rand.Intn(2) == 0 {
if xTarget > p.x {
xTarget--
} else {
xTarget++
}
} else {
if yTarget > p.y {
yTarget--
} else {
yTarget++
}
}
} else if xTarget != p.x {
if xTarget > p.x {
xTarget--
} else {
xTarget++
}
} else if yTarget != p.y {
if yTarget > p.y {
yTarget--
} else {
yTarget++
}
}
if xTarget == p.x && yTarget == p.y {
break
}
path = append(path, position{x: xTarget, y: yTarget})
}
return path
}
func playerNearby(p position, m model, dist int) bool {
return abs(p.x-m.playerPos.x) <= dist && abs(p.y-m.playerPos.y) <= dist
}
func followPath(p position, m model) []position {
xTarget := m.playerPos.x
yTarget := m.playerPos.y
path := []position{}
for xTarget != p.x || yTarget != p.y {
path = append(path, position{x: xTarget, y: yTarget})
if xTarget != p.x && yTarget != p.y {
if rand.Intn(2) == 0 {
if xTarget > p.x {
xTarget--
} else {
xTarget++
}
} else {
if yTarget > p.y {
yTarget--
} else {
yTarget++
}
}
} else if xTarget != p.x {
if xTarget > p.x {
xTarget--
} else {
xTarget++
}
} else if yTarget != p.y {
if yTarget > p.y {
yTarget--
} else {
yTarget++
}
}
}
return path
}
func huntPath(start position, m model) []position {
goal := m.playerPos
frontier := priorityQueue{}
frontier.put(start, 0)
costSoFar := map[position]int{}
costSoFar[start] = 0
cameFrom := map[position]position{}
cameFrom[start] = start
for !frontier.empty() {
current := frontier.get()
if current == goal {
break
}
neighbors := []position{}
for _, pos := range getNeighbors(current) {
if pos.x < 1 || pos.y < 1 || pos.x > m.level().width-2 || pos.y > m.level().height-2 {
continue
}
if stone := m.level().tombstoneMap[pos]; stone != nil {
continue
}
if ghost := m.level().ghostMap[pos]; ghost != nil {
continue
}
neighbors = append(neighbors, pos)
}
for _, neighbor := range neighbors {
newCost := costSoFar[current] + 1
if cost, ok := costSoFar[neighbor]; ok && newCost >= cost {
continue
}
costSoFar[neighbor] = newCost
cameFrom[neighbor] = current
frontier.put(neighbor, newCost+manhattanDistance(neighbor, goal))
}
}
if _, ok := cameFrom[goal]; !ok {
return []position{}
}
path := []position{}
current := goal
for current != start {
path = append(path, current)
current = cameFrom[current]
}
return path
}