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
|
package game
import (
"log"
"math"
)
func DistanceBetweenPositions(origin, pos Position) int {
x, y := float64(pos.X-origin.X), float64(pos.Y-origin.Y)
ax, ay := math.Abs(x), math.Abs(y)
var abs float64
if ax > ay {
abs = ax + math.Floor(ay/2)
} else {
abs = ay + math.Floor(ax/2)
}
if int(abs) < 0 {
log.Panicf("Distance smaller than 0")
}
return int(abs)
}
func DistanceBetweenPermanents(p1, p2 Permanent) int {
return DistanceBetweenPositions(p1.Tile().Position, p2.Tile().Position)
}
func IsPositionInRange(origin Position, pos Position, r int) bool {
return DistanceBetweenPositions(origin, pos) <= r
}
func PositionsInRange(origin Position, r int, includeOrigin bool) []Position {
positions := []Position{}
for x := -r; x <= r; x++ {
for y := -r; y <= r; y++ {
pos := Position{origin.X + x, origin.Y + y}
if pos.X < 0 || pos.Y < 0 {
continue
}
if !includeOrigin && pos == origin {
continue
}
if IsPositionInRange(origin, pos, r) {
positions = append(positions, pos)
}
}
}
return positions
}
func tilesInRangeFromOrigin(m *Map, origin Position, r int, includeOrigin bool) []*Tile {
tiles := []*Tile{}
for _, pos := range PositionsInRange(origin, r, includeOrigin) {
tile := m.TileAt(pos)
if tile != nil {
tiles = append(tiles, tile)
}
}
return tiles
}
// TilesInRangeFromOrigin returns all tiles in range r from a position.
//
// The position itself is excluded.
func TilesInRangeFromOrigin(m *Map, origin Position, r int) []*Tile {
return tilesInRangeFromOrigin(m, origin, r, false)
}
// TilesInRange returns all tiles in range r from a Permanent.
//
// The permanent's position itself is excluded.
func TilesInRange(m *Map, p Permanent, r int) []*Tile {
return TilesInRangeFromOrigin(m, TileOrContainingPermTile(p).Position, r)
}
|