File: neighbors.go

package info (click to toggle)
boohu 0.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 752 kB
  • sloc: makefile: 6
file content (53 lines) | stat: -rw-r--r-- 1,283 bytes parent folder | download | duplicates (2)
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
package main

func (pos position) Neighbors(nb []position, keep func(position) bool) []position {
	neighbors := [8]position{pos.E(), pos.W(), pos.N(), pos.S(), pos.NE(), pos.NW(), pos.SE(), pos.SW()}
	nb = nb[:0]
	for _, npos := range neighbors {
		if keep(npos) {
			nb = append(nb, npos)
		}
	}
	return nb
}

func (pos position) CardinalNeighbors(nb []position, keep func(position) bool) []position {
	neighbors := [4]position{pos.E(), pos.W(), pos.N(), pos.S()}
	nb = nb[:0]
	for _, npos := range neighbors {
		if keep(npos) {
			nb = append(nb, npos)
		}
	}
	return nb
}

func (pos position) OutsideNeighbors() []position {
	nb := make([]position, 0, 8)
	nb = pos.Neighbors(nb, func(npos position) bool {
		return !npos.valid()
	})
	return nb
}

func (pos position) ValidNeighbors() []position {
	nb := make([]position, 0, 8)
	nb = pos.Neighbors(nb, position.valid)
	return nb
}

func (d *dungeon) IsFreeCell(pos position) bool {
	return pos.valid() && d.Cell(pos).T != WallCell
}

func (d *dungeon) FreeNeighbors(pos position) []position {
	nb := make([]position, 0, 8)
	nb = pos.Neighbors(nb, d.IsFreeCell)
	return nb
}

func (d *dungeon) CardinalFreeNeighbors(pos position) []position {
	nb := make([]position, 0, 4)
	nb = pos.CardinalNeighbors(nb, d.IsFreeCell)
	return nb
}