This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpos.go
137 lines (121 loc) · 2.64 KB
/
pos.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
package main
import (
"github.com/anaseto/gruid"
)
func distance(from, to gruid.Point) int {
delta := to.Sub(from)
return abs(delta.X) + abs(delta.Y)
}
func distanceChebyshev(from, to gruid.Point) int {
delta := to.Sub(from)
deltaX := abs(delta.X)
deltaY := abs(delta.Y)
if deltaX > deltaY {
return deltaX
}
return deltaY
}
// ZP is the zero value for gruid.Point.
var ZP gruid.Point = gruid.Point{}
func dirString(dir gruid.Point) (s string) {
switch dir {
case ZP:
s = ""
case gruid.Point{1, 0}:
s = "E"
case gruid.Point{1, -1}:
s = "NE"
case gruid.Point{0, -1}:
s = "N"
case gruid.Point{-1, -1}:
s = "NW"
case gruid.Point{-1, 0}:
s = "W"
case gruid.Point{-1, 1}:
s = "SW"
case gruid.Point{0, 1}:
s = "S"
case gruid.Point{1, 1}:
s = "SE"
}
return s
}
func keyToDir(k action) (p gruid.Point) {
switch k {
case ActionW, ActionRunW:
p = gruid.Point{-1, 0}
case ActionE, ActionRunE:
p = gruid.Point{1, 0}
case ActionS, ActionRunS:
p = gruid.Point{0, 1}
case ActionN, ActionRunN:
p = gruid.Point{0, -1}
}
return p
}
func sign(n int) int {
var i int
switch {
case n > 0:
i = 1
case n < 0:
i = -1
}
return i
}
// dirnorm returns a normalized direction between two points, so that
// directions that aren't cardinal nor diagonal are transformed into the
// cardinal part (this corresponds to pruned intermediate nodes in diagonal
// jump).
func dirnorm(p, q gruid.Point) gruid.Point {
dir := q.Sub(p)
dx := abs(dir.X)
dy := abs(dir.Y)
dir = gruid.Point{sign(dir.X), sign(dir.Y)}
switch {
case dx == dy:
case dx > dy:
dir.Y = 0
default:
dir.X = 0
}
return dir
}
func idxtopos(i int) gruid.Point {
return gruid.Point{i % DungeonWidth, i / DungeonWidth}
}
func idx(p gruid.Point) int {
return p.Y*DungeonWidth + p.X
}
func valid(p gruid.Point) bool {
return p.Y >= 0 && p.Y < DungeonHeight && p.X >= 0 && p.X < DungeonWidth
}
func inViewCone(dir, from, to gruid.Point) bool {
if to == from || distance(from, to) <= 1 {
return true
}
d := dirnorm(from, to)
return d == dir || leftDir(d) == dir || rightDir(d) == dir
}
func leftDir(dir gruid.Point) gruid.Point {
switch {
case dir.X == 0 || dir.Y == 0:
return left(dir, dir)
default:
return gruid.Point{(dir.Y + dir.X) / 2, (dir.Y - dir.X) / 2}
}
}
func rightDir(dir gruid.Point) gruid.Point {
switch {
case dir.X == 0 || dir.Y == 0:
return right(dir, dir)
default:
return gruid.Point{(dir.X - dir.Y) / 2, (dir.Y + dir.X) / 2}
}
}
func right(p gruid.Point, dir gruid.Point) gruid.Point {
return gruid.Point{p.X - dir.Y, p.Y + dir.X}
}
func left(p gruid.Point, dir gruid.Point) gruid.Point {
return gruid.Point{p.X + dir.Y, p.Y - dir.X}
}