Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

Playground

board.gno

package hex

import "sort"

// Board is the adjacency table of one game board. ids is sorted so lookups
// can binary-search it; nbr[i][d] is the index in ids of the neighbour of
// ids[i] in direction d, or -1 when the board edge lies that way.
//
// Boards are generated by scripts/gen_board_data.py and never mutated, so
// sharing *Board values is safe.
type Board struct {
	name      string
	ids       []string
	nbr       [][6]int
	qr        [][2]int // axial coordinates (q, r) of ids[i], see geom.gno
	fence     []uint8  // fenced hexsides of ids[i]: bit d for direction d (rule 35)
	obstacles []int    // indices of the obstacle hexes (rule 36)
	// between lists the hex pairs an obstacle lies between for the line of
	// sight (rule 31), computed by the generator from the printed coordinates
	// and the obstacle's outline on the board image: {a, b, o} with a < b
	// indices in ids and o an index in obstacles, sorted by (a, b, o).
	between [][3]int
}

// Name returns the board letter, e.g. "A".
func (b *Board) Name() string { return b.name }

// Size returns the number of hexes.
func (b *Board) Size() int { return len(b.ids) }

// Index returns the position of id in the sorted id list, or -1.
func (b *Board) Index(id string) int {
	i := sort.SearchStrings(b.ids, id)
	if i < len(b.ids) && b.ids[i] == id {
		return i
	}
	return -1
}

// Has reports whether id is a hex of this board.
func (b *Board) Has(id string) bool { return b.Index(id) >= 0 }

// Neighbor returns the hex adjacent to id in direction d. ok is false when
// id is unknown, d is invalid, or the board edge lies in that direction.
func (b *Board) Neighbor(id string, d Dir) (nb string, ok bool) {
	i := b.Index(id)
	if i < 0 || !d.Valid() {
		return "", false
	}
	j := b.nbr[i][d]
	if j < 0 {
		return "", false
	}
	return b.ids[j], true
}

// Neighbors returns the six neighbours of id in direction order; an empty
// string marks the board edge. An unknown id yields six empty strings.
func (b *Board) Neighbors(id string) [6]string {
	var out [6]string
	i := b.Index(id)
	if i < 0 {
		return out
	}
	for d, j := range b.nbr[i] {
		if j >= 0 {
			out[d] = b.ids[j]
		}
	}
	return out
}

// Fenced reports whether a fence or hitching post (rule 35) runs along the
// hexside of id in direction d, so that moving or staggering across it
// costs two delay cards.
func (b *Board) Fenced(id string, d Dir) bool {
	i := b.Index(id)
	if i < 0 || !d.Valid() || i >= len(b.fence) {
		return false
	}
	return b.fence[i]&(1<<uint(d)) != 0
}

// Obstacle reports whether id is an obstacle hex (rule 36: a trough, a
// table...), so that moving or staggering onto or off it costs two delay
// cards.
func (b *Board) Obstacle(id string) bool {
	i := b.Index(id)
	if i < 0 {
		return false
	}
	for _, o := range b.obstacles {
		if o == i {
			return true
		}
	}
	return false
}

// IDs returns a copy of the sorted hex ids.
func (b *Board) IDs() []string {
	out := make([]string, len(b.ids))
	copy(out, b.ids)
	return out
}

board_a.gno

// Code generated by scripts/gen_board_data.py from assets/hex_grid.json. DO NOT EDIT.

package hex

// A is board A ("Corral"): 138 flat-top hexes in offset vertical columns.
// ids is sorted so Index can binary-search it; nbr holds, for each hex, the
// index of its neighbour in each of the 6 directions (N, NE, SE, S, SW, NW),
// or -1 when the hex is on the board edge; qr holds its axial coordinates;
// fence holds its fenced hexsides, bit d for direction d (rule 35);
// obstacles lists the indices of the obstacle hexes (rule 36);
// between lists the hex pairs an obstacle lies between for the line of
// sight (rule 31), as {a, b, o}: a < b indices in ids, o an index in
// obstacles, sorted by (a, b, o).
var A = &Board{
	name: "A",
	ids: []string{
		"A-A1",
		"A-A10",
		"A-A11",
		"A-A12",
		"A-A2",
		"A-A3",
		"A-A4",
		"A-A5",
		"A-A6",
		"A-A7",
		"A-A8",
		"A-A9",
		"A-B0",
		"A-B1",
		"A-B10",
		"A-B11",
		"A-B12",
		"A-B2",
		"A-B3",
		"A-B4",
		"A-B5",
		"A-B6",
		"A-B7",
		"A-B8",
		"A-B9",
		"A-C1",
		"A-C10",
		"A-C11",
		"A-C12",
		"A-C2",
		"A-C3",
		"A-C4",
		"A-C5",
		"A-C6",
		"A-C7",
		"A-C8",
		"A-C9",
		"A-D0",
		"A-D1",
		"A-D10",
		"A-D11",
		"A-D12",
		"A-D2",
		"A-D3",
		"A-D4",
		"A-D5",
		"A-D6",
		"A-D7",
		"A-D8",
		"A-D9",
		"A-E1",
		"A-E10",
		"A-E11",
		"A-E12",
		"A-E2",
		"A-E3",
		"A-E4",
		"A-E5",
		"A-E6",
		"A-E7",
		"A-E8",
		"A-E9",
		"A-F0",
		"A-F1",
		"A-F10",
		"A-F11",
		"A-F12",
		"A-F2",
		"A-F3",
		"A-F4",
		"A-F5",
		"A-F6",
		"A-F7",
		"A-F8",
		"A-F9",
		"A-G1",
		"A-G10",
		"A-G11",
		"A-G12",
		"A-G2",
		"A-G3",
		"A-G4",
		"A-G5",
		"A-G6",
		"A-G7",
		"A-G8",
		"A-G9",
		"A-H0",
		"A-H1",
		"A-H10",
		"A-H11",
		"A-H12",
		"A-H2",
		"A-H3",
		"A-H4",
		"A-H5",
		"A-H6",
		"A-H7",
		"A-H8",
		"A-H9",
		"A-I1",
		"A-I10",
		"A-I11",
		"A-I11*",
		"A-I12",
		"A-I2",
		"A-I3",
		"A-I4",
		"A-I5",
		"A-I6",
		"A-I7",
		"A-I8",
		"A-I9",
		"A-J0",
		"A-J1",
		"A-J10",
		"A-J11",
		"A-J12",
		"A-J2",
		"A-J3",
		"A-J4",
		"A-J5",
		"A-J6",
		"A-J7",
		"A-J8",
		"A-J9",
		"A-K1",
		"A-K10",
		"A-K11",
		"A-K12",
		"A-K2",
		"A-K3",
		"A-K4",
		"A-K5",
		"A-K6",
		"A-K7",
		"A-K8",
		"A-K9",
	},
	nbr: [][6]int{
		{-1, 12, 13, 4, -1, -1}, // A-A1
		{11, 24, 14, 2, -1, -1}, // A-A10
		{1, 14, 15, 3, -1, -1}, // A-A11
		{2, 15, 16, -1, -1, -1}, // A-A12
		{0, 13, 17, 5, -1, -1}, // A-A2
		{4, 17, 18, 6, -1, -1}, // A-A3
		{5, 18, 19, 7, -1, -1}, // A-A4
		{6, 19, 20, 8, -1, -1}, // A-A5
		{7, 20, 21, 9, -1, -1}, // A-A6
		{8, 21, 22, 10, -1, -1}, // A-A7
		{9, 22, 23, 11, -1, -1}, // A-A8
		{10, 23, 24, 1, -1, -1}, // A-A9
		{-1, -1, 25, 13, 0, -1}, // A-B0
		{12, 25, 29, 17, 4, 0}, // A-B1
		{24, 26, 27, 15, 2, 1}, // A-B10
		{14, 27, 28, 16, 3, 2}, // A-B11
		{15, 28, -1, -1, -1, 3}, // A-B12
		{13, 29, 30, 18, 5, 4}, // A-B2
		{17, 30, 31, 19, 6, 5}, // A-B3
		{18, 31, 32, 20, 7, 6}, // A-B4
		{19, 32, 33, 21, 8, 7}, // A-B5
		{20, 33, 34, 22, 9, 8}, // A-B6
		{21, 34, 35, 23, 10, 9}, // A-B7
		{22, 35, 36, 24, 11, 10}, // A-B8
		{23, 36, 26, 14, 1, 11}, // A-B9
		{-1, 37, 38, 29, 13, 12}, // A-C1
		{36, 49, 39, 27, 14, 24}, // A-C10
		{26, 39, 40, 28, 15, 14}, // A-C11
		{27, 40, 41, -1, 16, 15}, // A-C12
		{25, 38, 42, 30, 17, 13}, // A-C2
		{29, 42, 43, 31, 18, 17}, // A-C3
		{30, 43, 44, 32, 19, 18}, // A-C4
		{31, 44, 45, 33, 20, 19}, // A-C5
		{32, 45, 46, 34, 21, 20}, // A-C6
		{33, 46, 47, 35, 22, 21}, // A-C7
		{34, 47, 48, 36, 23, 22}, // A-C8
		{35, 48, 49, 26, 24, 23}, // A-C9
		{-1, -1, 50, 38, 25, -1}, // A-D0
		{37, 50, 54, 42, 29, 25}, // A-D1
		{49, 51, 52, 40, 27, 26}, // A-D10
		{39, 52, 53, 41, 28, 27}, // A-D11
		{40, 53, -1, -1, -1, 28}, // A-D12
		{38, 54, 55, 43, 30, 29}, // A-D2
		{42, 55, 56, 44, 31, 30}, // A-D3
		{43, 56, 57, 45, 32, 31}, // A-D4
		{44, 57, 58, 46, 33, 32}, // A-D5
		{45, 58, 59, 47, 34, 33}, // A-D6
		{46, 59, 60, 48, 35, 34}, // A-D7
		{47, 60, 61, 49, 36, 35}, // A-D8
		{48, 61, 51, 39, 26, 36}, // A-D9
		{-1, 62, 63, 54, 38, 37}, // A-E1
		{61, 74, 64, 52, 39, 49}, // A-E10
		{51, 64, 65, 53, 40, 39}, // A-E11
		{52, 65, 66, -1, 41, 40}, // A-E12
		{50, 63, 67, 55, 42, 38}, // A-E2
		{54, 67, 68, 56, 43, 42}, // A-E3
		{55, 68, 69, 57, 44, 43}, // A-E4
		{56, 69, 70, 58, 45, 44}, // A-E5
		{57, 70, 71, 59, 46, 45}, // A-E6
		{58, 71, 72, 60, 47, 46}, // A-E7
		{59, 72, 73, 61, 48, 47}, // A-E8
		{60, 73, 74, 51, 49, 48}, // A-E9
		{-1, -1, 75, 63, 50, -1}, // A-F0
		{62, 75, 79, 67, 54, 50}, // A-F1
		{74, 76, 77, 65, 52, 51}, // A-F10
		{64, 77, 78, 66, 53, 52}, // A-F11
		{65, 78, -1, -1, -1, 53}, // A-F12
		{63, 79, 80, 68, 55, 54}, // A-F2
		{67, 80, 81, 69, 56, 55}, // A-F3
		{68, 81, 82, 70, 57, 56}, // A-F4
		{69, 82, 83, 71, 58, 57}, // A-F5
		{70, 83, 84, 72, 59, 58}, // A-F6
		{71, 84, 85, 73, 60, 59}, // A-F7
		{72, 85, 86, 74, 61, 60}, // A-F8
		{73, 86, 76, 64, 51, 61}, // A-F9
		{-1, 87, 88, 79, 63, 62}, // A-G1
		{86, 99, 89, 77, 64, 74}, // A-G10
		{76, 89, 90, 78, 65, 64}, // A-G11
		{77, 90, 91, -1, 66, 65}, // A-G12
		{75, 88, 92, 80, 67, 63}, // A-G2
		{79, 92, 93, 81, 68, 67}, // A-G3
		{80, 93, 94, 82, 69, 68}, // A-G4
		{81, 94, 95, 83, 70, 69}, // A-G5
		{82, 95, 96, 84, 71, 70}, // A-G6
		{83, 96, 97, 85, 72, 71}, // A-G7
		{84, 97, 98, 86, 73, 72}, // A-G8
		{85, 98, 99, 76, 74, 73}, // A-G9
		{-1, -1, 100, 88, 75, -1}, // A-H0
		{87, 100, 105, 92, 79, 75}, // A-H1
		{99, 101, 103, 90, 77, 76}, // A-H10
		{89, 103, 104, 91, 78, 77}, // A-H11
		{90, 104, -1, -1, -1, 78}, // A-H12
		{88, 105, 106, 93, 80, 79}, // A-H2
		{92, 106, 107, 94, 81, 80}, // A-H3
		{93, 107, 108, 95, 82, 81}, // A-H4
		{94, 108, 109, 96, 83, 82}, // A-H5
		{95, 109, 110, 97, 84, 83}, // A-H6
		{96, 110, 111, 98, 85, 84}, // A-H7
		{97, 111, 112, 99, 86, 85}, // A-H8
		{98, 112, 101, 89, 76, 86}, // A-H9
		{-1, 113, 114, 105, 88, 87}, // A-I1
		{112, 125, 115, 103, 89, 99}, // A-I10
		{103, 115, 116, 104, 90, 103}, // A-I11
		{101, 115, 102, 102, 90, 89}, // A-I11*
		{102, 116, 117, -1, 91, 90}, // A-I12
		{100, 114, 118, 106, 92, 88}, // A-I2
		{105, 118, 119, 107, 93, 92}, // A-I3
		{106, 119, 120, 108, 94, 93}, // A-I4
		{107, 120, 121, 109, 95, 94}, // A-I5
		{108, 121, 122, 110, 96, 95}, // A-I6
		{109, 122, 123, 111, 97, 96}, // A-I7
		{110, 123, 124, 112, 98, 97}, // A-I8
		{111, 124, 125, 101, 99, 98}, // A-I9
		{-1, -1, 126, 114, 100, -1}, // A-J0
		{113, 126, 130, 118, 105, 100}, // A-J1
		{125, 127, 128, 116, 103, 101}, // A-J10
		{115, 128, 129, 117, 104, 102}, // A-J11
		{116, 129, -1, -1, -1, 104}, // A-J12
		{114, 130, 131, 119, 106, 105}, // A-J2
		{118, 131, 132, 120, 107, 106}, // A-J3
		{119, 132, 133, 121, 108, 107}, // A-J4
		{120, 133, 134, 122, 109, 108}, // A-J5
		{121, 134, 135, 123, 110, 109}, // A-J6
		{122, 135, 136, 124, 111, 110}, // A-J7
		{123, 136, 137, 125, 112, 111}, // A-J8
		{124, 137, 127, 115, 101, 112}, // A-J9
		{-1, -1, -1, 130, 114, 113}, // A-K1
		{137, -1, -1, 128, 115, 125}, // A-K10
		{127, -1, -1, 129, 116, 115}, // A-K11
		{128, -1, -1, -1, 117, 116}, // A-K12
		{126, -1, -1, 131, 118, 114}, // A-K2
		{130, -1, -1, 132, 119, 118}, // A-K3
		{131, -1, -1, 133, 120, 119}, // A-K4
		{132, -1, -1, 134, 121, 120}, // A-K5
		{133, -1, -1, 135, 122, 121}, // A-K6
		{134, -1, -1, 136, 123, 122}, // A-K7
		{135, -1, -1, 137, 124, 123}, // A-K8
		{136, -1, -1, 127, 125, 124}, // A-K9
	},
	qr: [][2]int{
		{0, 0}, // A-A1
		{0, 9}, // A-A10
		{0, 10}, // A-A11
		{0, 11}, // A-A12
		{0, 1}, // A-A2
		{0, 2}, // A-A3
		{0, 3}, // A-A4
		{0, 4}, // A-A5
		{0, 5}, // A-A6
		{0, 6}, // A-A7
		{0, 7}, // A-A8
		{0, 8}, // A-A9
		{1, -1}, // A-B0
		{1, 0}, // A-B1
		{1, 9}, // A-B10
		{1, 10}, // A-B11
		{1, 11}, // A-B12
		{1, 1}, // A-B2
		{1, 2}, // A-B3
		{1, 3}, // A-B4
		{1, 4}, // A-B5
		{1, 5}, // A-B6
		{1, 6}, // A-B7
		{1, 7}, // A-B8
		{1, 8}, // A-B9
		{2, -1}, // A-C1
		{2, 8}, // A-C10
		{2, 9}, // A-C11
		{2, 10}, // A-C12
		{2, 0}, // A-C2
		{2, 1}, // A-C3
		{2, 2}, // A-C4
		{2, 3}, // A-C5
		{2, 4}, // A-C6
		{2, 5}, // A-C7
		{2, 6}, // A-C8
		{2, 7}, // A-C9
		{3, -2}, // A-D0
		{3, -1}, // A-D1
		{3, 8}, // A-D10
		{3, 9}, // A-D11
		{3, 10}, // A-D12
		{3, 0}, // A-D2
		{3, 1}, // A-D3
		{3, 2}, // A-D4
		{3, 3}, // A-D5
		{3, 4}, // A-D6
		{3, 5}, // A-D7
		{3, 6}, // A-D8
		{3, 7}, // A-D9
		{4, -2}, // A-E1
		{4, 7}, // A-E10
		{4, 8}, // A-E11
		{4, 9}, // A-E12
		{4, -1}, // A-E2
		{4, 0}, // A-E3
		{4, 1}, // A-E4
		{4, 2}, // A-E5
		{4, 3}, // A-E6
		{4, 4}, // A-E7
		{4, 5}, // A-E8
		{4, 6}, // A-E9
		{5, -3}, // A-F0
		{5, -2}, // A-F1
		{5, 7}, // A-F10
		{5, 8}, // A-F11
		{5, 9}, // A-F12
		{5, -1}, // A-F2
		{5, 0}, // A-F3
		{5, 1}, // A-F4
		{5, 2}, // A-F5
		{5, 3}, // A-F6
		{5, 4}, // A-F7
		{5, 5}, // A-F8
		{5, 6}, // A-F9
		{6, -3}, // A-G1
		{6, 6}, // A-G10
		{6, 7}, // A-G11
		{6, 8}, // A-G12
		{6, -2}, // A-G2
		{6, -1}, // A-G3
		{6, 0}, // A-G4
		{6, 1}, // A-G5
		{6, 2}, // A-G6
		{6, 3}, // A-G7
		{6, 4}, // A-G8
		{6, 5}, // A-G9
		{7, -4}, // A-H0
		{7, -3}, // A-H1
		{7, 6}, // A-H10
		{7, 7}, // A-H11
		{7, 8}, // A-H12
		{7, -2}, // A-H2
		{7, -1}, // A-H3
		{7, 0}, // A-H4
		{7, 1}, // A-H5
		{7, 2}, // A-H6
		{7, 3}, // A-H7
		{7, 4}, // A-H8
		{7, 5}, // A-H9
		{8, -4}, // A-I1
		{8, 5}, // A-I10
		{8, 6}, // A-I11
		{8, 6}, // A-I11*
		{8, 7}, // A-I12
		{8, -3}, // A-I2
		{8, -2}, // A-I3
		{8, -1}, // A-I4
		{8, 0}, // A-I5
		{8, 1}, // A-I6
		{8, 2}, // A-I7
		{8, 3}, // A-I8
		{8, 4}, // A-I9
		{9, -5}, // A-J0
		{9, -4}, // A-J1
		{9, 5}, // A-J10
		{9, 6}, // A-J11
		{9, 7}, // A-J12
		{9, -3}, // A-J2
		{9, -2}, // A-J3
		{9, -1}, // A-J4
		{9, 0}, // A-J5
		{9, 1}, // A-J6
		{9, 2}, // A-J7
		{9, 3}, // A-J8
		{9, 4}, // A-J9
		{10, -5}, // A-K1
		{10, 4}, // A-K10
		{10, 5}, // A-K11
		{10, 6}, // A-K12
		{10, -4}, // A-K2
		{10, -3}, // A-K3
		{10, -2}, // A-K4
		{10, -1}, // A-K5
		{10, 0}, // A-K6
		{10, 1}, // A-K7
		{10, 2}, // A-K8
		{10, 3}, // A-K9
	},
	fence: []uint8{
		0, // A-A1
		0, // A-A10
		0, // A-A11
		0, // A-A12
		0, // A-A2
		0, // A-A3
		4, // A-A4
		6, // A-A5
		6, // A-A6
		6, // A-A7
		6, // A-A8
		2, // A-A9
		0, // A-B0
		0, // A-B1
		0, // A-B10
		0, // A-B11
		0, // A-B12
		0, // A-B2
		12, // A-B3
		49, // A-B4
		48, // A-B5
		48, // A-B6
		48, // A-B7
		56, // A-B8
		3, // A-B9
		0, // A-C1
		3, // A-C10
		0, // A-C11
		0, // A-C12
		0, // A-C2
		12, // A-C3
		33, // A-C4
		0, // A-C5
		0, // A-C6
		0, // A-C7
		0, // A-C8
		24, // A-C9
		0, // A-D0
		0, // A-D1
		3, // A-D10
		0, // A-D11
		0, // A-D12
		12, // A-D2
		33, // A-D3
		0, // A-D4
		0, // A-D5
		0, // A-D6
		0, // A-D7
		0, // A-D8
		24, // A-D9
		0, // A-E1
		24, // A-E10
		1, // A-E11
		0, // A-E12
		8, // A-E2
		33, // A-E3
		0, // A-E4
		0, // A-E5
		0, // A-E6
		0, // A-E7
		0, // A-E8
		0, // A-E9
		0, // A-F0
		0, // A-F1
		0, // A-F10
		0, // A-F11
		0, // A-F12
		0, // A-F2
		0, // A-F3
		0, // A-F4
		0, // A-F5
		0, // A-F6
		0, // A-F7
		0, // A-F8
		0, // A-F9
		0, // A-G1
		12, // A-G10
		1, // A-G11
		0, // A-G12
		8, // A-G2
		3, // A-G3
		0, // A-G4
		0, // A-G5
		0, // A-G6
		0, // A-G7
		0, // A-G8
		0, // A-G9
		0, // A-H0
		0, // A-H1
		33, // A-H10
		0, // A-H11
		0, // A-H12
		24, // A-H2
		3, // A-H3
		0, // A-H4
		0, // A-H5
		0, // A-H6
		0, // A-H7
		0, // A-H8
		12, // A-H9
		0, // A-I1
		33, // A-I10
		0, // A-I11
		0, // A-I11*
		0, // A-I12
		0, // A-I2
		24, // A-I3
		3, // A-I4
		0, // A-I5
		0, // A-I6
		0, // A-I7
		0, // A-I8
		12, // A-I9
		0, // A-J0
		0, // A-J1
		0, // A-J10
		0, // A-J11
		0, // A-J12
		0, // A-J2
		24, // A-J3
		7, // A-J4
		6, // A-J5
		6, // A-J6
		6, // A-J7
		14, // A-J8
		33, // A-J9
		0, // A-K1
		0, // A-K10
		0, // A-K11
		0, // A-K12
		0, // A-K2
		0, // A-K3
		16, // A-K4
		48, // A-K5
		48, // A-K6
		48, // A-K7
		48, // A-K8
		32, // A-K9
	},
	obstacles: []int{
		103, // A-I11*
	},
	between: [][3]int{
		{0, 102, 0}, // A-A1 A-I11
		{0, 116, 0}, // A-A1 A-J11
		{0, 117, 0}, // A-A1 A-J12
		{1, 102, 0}, // A-A10 A-I11
		{1, 128, 0}, // A-A10 A-K11
		{2, 102, 0}, // A-A11 A-I11
		{2, 128, 0}, // A-A11 A-K11
		{3, 102, 0}, // A-A12 A-I11
		{3, 128, 0}, // A-A12 A-K11
		{4, 102, 0}, // A-A2 A-I11
		{4, 116, 0}, // A-A2 A-J11
		{4, 117, 0}, // A-A2 A-J12
		{5, 102, 0}, // A-A3 A-I11
		{5, 116, 0}, // A-A3 A-J11
		{5, 117, 0}, // A-A3 A-J12
		{6, 102, 0}, // A-A4 A-I11
		{6, 116, 0}, // A-A4 A-J11
		{6, 117, 0}, // A-A4 A-J12
		{6, 129, 0}, // A-A4 A-K12
		{7, 102, 0}, // A-A5 A-I11
		{7, 116, 0}, // A-A5 A-J11
		{7, 129, 0}, // A-A5 A-K12
		{8, 102, 0}, // A-A6 A-I11
		{8, 116, 0}, // A-A6 A-J11
		{8, 129, 0}, // A-A6 A-K12
		{9, 102, 0}, // A-A7 A-I11
		{9, 116, 0}, // A-A7 A-J11
		{9, 129, 0}, // A-A7 A-K12
		{10, 102, 0}, // A-A8 A-I11
		{10, 116, 0}, // A-A8 A-J11
		{10, 129, 0}, // A-A8 A-K12
		{11, 102, 0}, // A-A9 A-I11
		{11, 116, 0}, // A-A9 A-J11
		{11, 128, 0}, // A-A9 A-K11
		{12, 102, 0}, // A-B0 A-I11
		{12, 117, 0}, // A-B0 A-J12
		{13, 102, 0}, // A-B1 A-I11
		{13, 117, 0}, // A-B1 A-J12
		{14, 102, 0}, // A-B10 A-I11
		{14, 128, 0}, // A-B10 A-K11
		{15, 102, 0}, // A-B11 A-I11
		{15, 128, 0}, // A-B11 A-K11
		{17, 102, 0}, // A-B2 A-I11
		{17, 116, 0}, // A-B2 A-J11
		{17, 117, 0}, // A-B2 A-J12
		{18, 102, 0}, // A-B3 A-I11
		{18, 116, 0}, // A-B3 A-J11
		{18, 117, 0}, // A-B3 A-J12
		{19, 102, 0}, // A-B4 A-I11
		{19, 116, 0}, // A-B4 A-J11
		{19, 117, 0}, // A-B4 A-J12
		{19, 129, 0}, // A-B4 A-K12
		{20, 102, 0}, // A-B5 A-I11
		{20, 116, 0}, // A-B5 A-J11
		{20, 129, 0}, // A-B5 A-K12
		{21, 102, 0}, // A-B6 A-I11
		{21, 116, 0}, // A-B6 A-J11
		{21, 129, 0}, // A-B6 A-K12
		{22, 102, 0}, // A-B7 A-I11
		{22, 116, 0}, // A-B7 A-J11
		{22, 129, 0}, // A-B7 A-K12
		{23, 102, 0}, // A-B8 A-I11
		{23, 116, 0}, // A-B8 A-J11
		{23, 129, 0}, // A-B8 A-K12
		{24, 102, 0}, // A-B9 A-I11
		{24, 116, 0}, // A-B9 A-J11
		{24, 128, 0}, // A-B9 A-K11
		{25, 102, 0}, // A-C1 A-I11
		{25, 117, 0}, // A-C1 A-J12
		{26, 102, 0}, // A-C10 A-I11
		{26, 116, 0}, // A-C10 A-J11
		{26, 128, 0}, // A-C10 A-K11
		{27, 102, 0}, // A-C11 A-I11
		{27, 128, 0}, // A-C11 A-K11
		{29, 102, 0}, // A-C2 A-I11
		{29, 117, 0}, // A-C2 A-J12
		{30, 102, 0}, // A-C3 A-I11
		{30, 116, 0}, // A-C3 A-J11
		{30, 117, 0}, // A-C3 A-J12
		{31, 102, 0}, // A-C4 A-I11
		{31, 116, 0}, // A-C4 A-J11
		{31, 117, 0}, // A-C4 A-J12
		{32, 102, 0}, // A-C5 A-I11
		{32, 116, 0}, // A-C5 A-J11
		{32, 117, 0}, // A-C5 A-J12
		{33, 102, 0}, // A-C6 A-I11
		{33, 116, 0}, // A-C6 A-J11
		{33, 117, 0}, // A-C6 A-J12
		{33, 129, 0}, // A-C6 A-K12
		{34, 102, 0}, // A-C7 A-I11
		{34, 116, 0}, // A-C7 A-J11
		{34, 129, 0}, // A-C7 A-K12
		{35, 102, 0}, // A-C8 A-I11
		{35, 116, 0}, // A-C8 A-J11
		{35, 129, 0}, // A-C8 A-K12
		{36, 102, 0}, // A-C9 A-I11
		{36, 116, 0}, // A-C9 A-J11
		{36, 128, 0}, // A-C9 A-K11
		{36, 129, 0}, // A-C9 A-K12
		{37, 102, 0}, // A-D0 A-I11
		{37, 104, 0}, // A-D0 A-I12
		{37, 117, 0}, // A-D0 A-J12
		{38, 102, 0}, // A-D1 A-I11
		{38, 104, 0}, // A-D1 A-I12
		{38, 117, 0}, // A-D1 A-J12
		{39, 102, 0}, // A-D10 A-I11
		{39, 128, 0}, // A-D10 A-K11
		{40, 102, 0}, // A-D11 A-I11
		{40, 128, 0}, // A-D11 A-K11
		{42, 102, 0}, // A-D2 A-I11
		{42, 117, 0}, // A-D2 A-J12
		{43, 102, 0}, // A-D3 A-I11
		{43, 117, 0}, // A-D3 A-J12
		{44, 102, 0}, // A-D4 A-I11
		{44, 116, 0}, // A-D4 A-J11
		{44, 117, 0}, // A-D4 A-J12
		{45, 102, 0}, // A-D5 A-I11
		{45, 116, 0}, // A-D5 A-J11
		{45, 117, 0}, // A-D5 A-J12
		{46, 102, 0}, // A-D6 A-I11
		{46, 116, 0}, // A-D6 A-J11
		{46, 117, 0}, // A-D6 A-J12
		{46, 129, 0}, // A-D6 A-K12
		{47, 102, 0}, // A-D7 A-I11
		{47, 116, 0}, // A-D7 A-J11
		{47, 129, 0}, // A-D7 A-K12
		{48, 102, 0}, // A-D8 A-I11
		{48, 116, 0}, // A-D8 A-J11
		{48, 129, 0}, // A-D8 A-K12
		{49, 102, 0}, // A-D9 A-I11
		{49, 116, 0}, // A-D9 A-J11
		{49, 128, 0}, // A-D9 A-K11
		{49, 129, 0}, // A-D9 A-K12
		{50, 102, 0}, // A-E1 A-I11
		{50, 104, 0}, // A-E1 A-I12
		{50, 117, 0}, // A-E1 A-J12
		{51, 102, 0}, // A-E10 A-I11
		{51, 116, 0}, // A-E10 A-J11
		{51, 128, 0}, // A-E10 A-K11
		{51, 129, 0}, // A-E10 A-K12
		{52, 102, 0}, // A-E11 A-I11
		{52, 128, 0}, // A-E11 A-K11
		{54, 102, 0}, // A-E2 A-I11
		{54, 104, 0}, // A-E2 A-I12
		{54, 117, 0}, // A-E2 A-J12
		{55, 102, 0}, // A-E3 A-I11
		{55, 104, 0}, // A-E3 A-I12
		{55, 117, 0}, // A-E3 A-J12
		{56, 102, 0}, // A-E4 A-I11
		{56, 104, 0}, // A-E4 A-I12
		{56, 117, 0}, // A-E4 A-J12
		{57, 102, 0}, // A-E5 A-I11
		{57, 117, 0}, // A-E5 A-J12
		{58, 102, 0}, // A-E6 A-I11
		{58, 116, 0}, // A-E6 A-J11
		{58, 117, 0}, // A-E6 A-J12
		{59, 102, 0}, // A-E7 A-I11
		{59, 116, 0}, // A-E7 A-J11
		{59, 117, 0}, // A-E7 A-J12
		{60, 102, 0}, // A-E8 A-I11
		{60, 116, 0}, // A-E8 A-J11
		{60, 129, 0}, // A-E8 A-K12
		{61, 102, 0}, // A-E9 A-I11
		{61, 116, 0}, // A-E9 A-J11
		{61, 129, 0}, // A-E9 A-K12
		{62, 102, 0}, // A-F0 A-I11
		{62, 104, 0}, // A-F0 A-I12
		{63, 102, 0}, // A-F1 A-I11
		{63, 104, 0}, // A-F1 A-I12
		{64, 102, 0}, // A-F10 A-I11
		{64, 116, 0}, // A-F10 A-J11
		{64, 128, 0}, // A-F10 A-K11
		{65, 102, 0}, // A-F11 A-I11
		{66, 127, 0}, // A-F12 A-K10
		{67, 102, 0}, // A-F2 A-I11
		{67, 104, 0}, // A-F2 A-I12
		{67, 117, 0}, // A-F2 A-J12
		{68, 102, 0}, // A-F3 A-I11
		{68, 104, 0}, // A-F3 A-I12
		{68, 117, 0}, // A-F3 A-J12
		{69, 102, 0}, // A-F4 A-I11
		{69, 104, 0}, // A-F4 A-I12
		{69, 117, 0}, // A-F4 A-J12
		{70, 102, 0}, // A-F5 A-I11
		{70, 104, 0}, // A-F5 A-I12
		{70, 117, 0}, // A-F5 A-J12
		{71, 102, 0}, // A-F6 A-I11
		{71, 117, 0}, // A-F6 A-J12
		{72, 102, 0}, // A-F7 A-I11
		{72, 116, 0}, // A-F7 A-J11
		{72, 117, 0}, // A-F7 A-J12
		{73, 102, 0}, // A-F8 A-I11
		{73, 116, 0}, // A-F8 A-J11
		{73, 117, 0}, // A-F8 A-J12
		{73, 129, 0}, // A-F8 A-K12
		{74, 102, 0}, // A-F9 A-I11
		{74, 116, 0}, // A-F9 A-J11
		{74, 129, 0}, // A-F9 A-K12
		{75, 102, 0}, // A-G1 A-I11
		{75, 104, 0}, // A-G1 A-I12
		{76, 102, 0}, // A-G10 A-I11
		{76, 116, 0}, // A-G10 A-J11
		{76, 128, 0}, // A-G10 A-K11
		{76, 129, 0}, // A-G10 A-K12
		{77, 102, 0}, // A-G11 A-I11
		{77, 128, 0}, // A-G11 A-K11
		{78, 115, 0}, // A-G12 A-J10
		{79, 102, 0}, // A-G2 A-I11
		{79, 104, 0}, // A-G2 A-I12
		{80, 102, 0}, // A-G3 A-I11
		{80, 104, 0}, // A-G3 A-I12
		{81, 102, 0}, // A-G4 A-I11
		{81, 104, 0}, // A-G4 A-I12
		{82, 102, 0}, // A-G5 A-I11
		{82, 104, 0}, // A-G5 A-I12
		{82, 117, 0}, // A-G5 A-J12
		{83, 102, 0}, // A-G6 A-I11
		{83, 104, 0}, // A-G6 A-I12
		{83, 117, 0}, // A-G6 A-J12
		{84, 102, 0}, // A-G7 A-I11
		{84, 104, 0}, // A-G7 A-I12
		{84, 117, 0}, // A-G7 A-J12
		{85, 102, 0}, // A-G8 A-I11
		{85, 104, 0}, // A-G8 A-I12
		{85, 116, 0}, // A-G8 A-J11
		{85, 117, 0}, // A-G8 A-J12
		{86, 102, 0}, // A-G9 A-I11
		{86, 116, 0}, // A-G9 A-J11
		{86, 117, 0}, // A-G9 A-J12
		{86, 129, 0}, // A-G9 A-K12
		{87, 102, 0}, // A-H0 A-I11
		{87, 104, 0}, // A-H0 A-I12
		{88, 102, 0}, // A-H1 A-I11
		{88, 104, 0}, // A-H1 A-I12
		{89, 102, 0}, // A-H10 A-I11
		{89, 104, 0}, // A-H10 A-I12
		{89, 116, 0}, // A-H10 A-J11
		{89, 117, 0}, // A-H10 A-J12
		{89, 128, 0}, // A-H10 A-K11
		{89, 129, 0}, // A-H10 A-K12
		{91, 101, 0}, // A-H12 A-I10
		{91, 123, 0}, // A-H12 A-J7
		{91, 124, 0}, // A-H12 A-J8
		{91, 125, 0}, // A-H12 A-J9
		{91, 132, 0}, // A-H12 A-K4
		{91, 133, 0}, // A-H12 A-K5
		{91, 134, 0}, // A-H12 A-K6
		{91, 135, 0}, // A-H12 A-K7
		{91, 136, 0}, // A-H12 A-K8
		{92, 102, 0}, // A-H2 A-I11
		{92, 104, 0}, // A-H2 A-I12
		{93, 102, 0}, // A-H3 A-I11
		{93, 104, 0}, // A-H3 A-I12
		{94, 102, 0}, // A-H4 A-I11
		{94, 104, 0}, // A-H4 A-I12
		{95, 102, 0}, // A-H5 A-I11
		{95, 104, 0}, // A-H5 A-I12
		{96, 102, 0}, // A-H6 A-I11
		{96, 104, 0}, // A-H6 A-I12
		{97, 102, 0}, // A-H7 A-I11
		{97, 104, 0}, // A-H7 A-I12
		{97, 117, 0}, // A-H7 A-J12
		{98, 102, 0}, // A-H8 A-I11
		{98, 104, 0}, // A-H8 A-I12
		{98, 117, 0}, // A-H8 A-J12
		{99, 102, 0}, // A-H9 A-I11
		{99, 104, 0}, // A-H9 A-I12
		{99, 116, 0}, // A-H9 A-J11
		{99, 117, 0}, // A-H9 A-J12
		{100, 102, 0}, // A-I1 A-I11
		{100, 104, 0}, // A-I1 A-I12
		{101, 102, 0}, // A-I10 A-I11
		{101, 104, 0}, // A-I10 A-I12
		{101, 117, 0}, // A-I10 A-J12
		{102, 105, 0}, // A-I11 A-I2
		{102, 106, 0}, // A-I11 A-I3
		{102, 107, 0}, // A-I11 A-I4
		{102, 108, 0}, // A-I11 A-I5
		{102, 109, 0}, // A-I11 A-I6
		{102, 110, 0}, // A-I11 A-I7
		{102, 111, 0}, // A-I11 A-I8
		{102, 112, 0}, // A-I11 A-I9
		{102, 113, 0}, // A-I11 A-J0
		{102, 114, 0}, // A-I11 A-J1
		{102, 118, 0}, // A-I11 A-J2
		{102, 119, 0}, // A-I11 A-J3
		{102, 120, 0}, // A-I11 A-J4
		{102, 121, 0}, // A-I11 A-J5
		{102, 122, 0}, // A-I11 A-J6
		{102, 123, 0}, // A-I11 A-J7
		{102, 124, 0}, // A-I11 A-J8
		{102, 125, 0}, // A-I11 A-J9
		{102, 126, 0}, // A-I11 A-K1
		{102, 130, 0}, // A-I11 A-K2
		{102, 131, 0}, // A-I11 A-K3
		{102, 132, 0}, // A-I11 A-K4
		{102, 133, 0}, // A-I11 A-K5
		{102, 134, 0}, // A-I11 A-K6
		{102, 135, 0}, // A-I11 A-K7
		{102, 136, 0}, // A-I11 A-K8
		{104, 105, 0}, // A-I12 A-I2
		{104, 106, 0}, // A-I12 A-I3
		{104, 107, 0}, // A-I12 A-I4
		{104, 108, 0}, // A-I12 A-I5
		{104, 109, 0}, // A-I12 A-I6
		{104, 110, 0}, // A-I12 A-I7
		{104, 111, 0}, // A-I12 A-I8
		{104, 112, 0}, // A-I12 A-I9
		{104, 113, 0}, // A-I12 A-J0
		{104, 114, 0}, // A-I12 A-J1
		{104, 118, 0}, // A-I12 A-J2
		{104, 119, 0}, // A-I12 A-J3
		{104, 120, 0}, // A-I12 A-J4
		{104, 121, 0}, // A-I12 A-J5
		{104, 122, 0}, // A-I12 A-J6
		{104, 123, 0}, // A-I12 A-J7
		{104, 124, 0}, // A-I12 A-J8
		{104, 126, 0}, // A-I12 A-K1
		{104, 130, 0}, // A-I12 A-K2
		{104, 131, 0}, // A-I12 A-K3
		{104, 132, 0}, // A-I12 A-K4
		{104, 133, 0}, // A-I12 A-K5
	},
}

geom.gno

package hex

import "math"

// Axial step of each direction on the flat-top grid (q east along the
// columns, r down a column), matching scripts/gen_board_data.py.
var dirStep = [6][2]int{{0, -1}, {1, -1}, {1, 0}, {0, 1}, {-1, 1}, {-1, 0}}

// Step returns the axial (q, r) offset of one hex in direction d.
func (d Dir) Step() (q, r int) { return dirStep[d][0], dirStep[d][1] }

// Coords returns the axial coordinates of a hex.
func (b *Board) Coords(id string) (q, r int, ok bool) {
	i := b.Index(id)
	if i < 0 {
		return 0, 0, false
	}
	return b.qr[i][0], b.qr[i][1], true
}

// Distance is the range between two hexes in hexes along the shortest route
// over the board (rule 13.51), or -1 when either hex is unknown.
func (b *Board) Distance(from, to string) int {
	src, dst := b.Index(from), b.Index(to)
	if src < 0 || dst < 0 {
		return -1
	}
	if src == dst {
		return 0
	}
	dist := make([]int, len(b.ids))
	for i := range dist {
		dist[i] = -1
	}
	dist[src] = 0
	queue := []int{src}
	for len(queue) > 0 {
		i := queue[0]
		queue = queue[1:]
		for _, j := range b.nbr[i] {
			if j < 0 || dist[j] >= 0 {
				continue
			}
			dist[j] = dist[i] + 1
			if j == dst {
				return dist[j]
			}
			queue = append(queue, j)
		}
	}
	return -1
}

// InAimZone reports whether target lies in the aim zone of a character at
// from facing f (rule 8.32): its own hex and every hex in or between its
// ahead-left and ahead-right rows, i.e. the 120° cone spanned by those two
// directions.
func (b *Board) InAimZone(from string, f Dir, target string) bool {
	q0, r0, ok := b.Coords(from)
	q1, r1, ok2 := b.Coords(target)
	if !ok || !ok2 || !f.Valid() {
		return false
	}
	dq, dr := q1-q0, r1-r0
	uq, ur := Dir((int(f) + 5) % 6).Step() // ahead left
	wq, wr := Dir((int(f) + 1) % 6).Step() // ahead right
	// Solve a*u + b*w = d; the step vectors 120° apart have determinant ±1.
	det := uq*wr - ur*wq
	a := (dq*wr - dr*wq) / det
	c := (uq*dr - ur*dq) / det
	return a >= 0 && c >= 0
}

// LongDir is the direction from target that continues the line drawn from
// shooter through target (rule 14.32): the "Long" hex of a direction card is
// the target's neighbour that way. When the line runs between two hexes the
// one closest to it wins, lowest direction first; in a shared hex it is the
// shooter's facing.
func (b *Board) LongDir(shooter, target string, shooterFacing Dir) Dir {
	q0, r0, ok := b.Coords(shooter)
	q1, r1, ok2 := b.Coords(target)
	if !ok || !ok2 || (q0 == q1 && r0 == r1) {
		return shooterFacing
	}
	x, y := planar(q1-q0, r1-r0)
	best, bestCos := Dir(0), -2.0
	for d := 0; d < 6; d++ {
		sq, sr := Dir(d).Step()
		sx, sy := planar(sq, sr)
		cos := (x*sx + y*sy) / (math.Sqrt(x*x+y*y) * math.Sqrt(sx*sx+sy*sy))
		if cos > bestCos+1e-9 {
			best, bestCos = Dir(d), cos
		}
	}
	return best
}

// planar maps an axial offset to flat-top pixel-like coordinates (x east, y south).
func planar(q, r int) (x, y float64) {
	return 1.5 * float64(q), math.Sqrt(3) * (float64(r) + float64(q)/2)
}

gnomod.toml

module = "gno.land/p/g1th0masp67zfs9lg6d34qhghldhg6kjcqjuv7c2/gnoslinger/hex/v0"
gno = "0.9"

[addpkg]
  creator = "g1th0masp67zfs9lg6d34qhghldhg6kjcqjuv7c2"
  height = 758848
  max_deposit = "150000000ugnot"

hex.gno

// Package hex holds the pure hex geometry of the game: absolute directions,
// directions relative to a character's facing, and board adjacency tables.
package hex

// Dir is an absolute direction on a flat-top hex grid laid out in vertical
// columns: 0=N 1=NE 2=SE 3=S 4=SW 5=NW.
type Dir int

const (
	N Dir = iota
	NE
	SE
	S
	SW
	NW
)

var dirNames = [6]string{"N", "NE", "SE", "S", "SW", "NW"}

// Valid reports whether d is one of the six directions.
func (d Dir) Valid() bool { return d >= 0 && d < 6 }

// String returns the compass name of d.
func (d Dir) String() string {
	if !d.Valid() {
		return "?"
	}
	return dirNames[d]
}

// Opposite returns the direction pointing back at d.
func (d Dir) Opposite() Dir { return (d + 3) % 6 }

// Rel is a direction relative to a character's facing, as chosen on a move
// or turn card. The names are the ones the client and the plan string use.
// Same and Long only appear on the HEX line of result cards (direction
// cards, rule 14.3, relative to the target's body counter): they are not
// movement choices, ParseRel rejects them and Offset panics on them.
type Rel string

const (
	AheadLeft  Rel = "ahead_left"
	Ahead      Rel = "ahead"
	AheadRight Rel = "ahead_right"
	BackLeft   Rel = "back_left"
	Back       Rel = "back"
	BackRight  Rel = "back_right"
	Same       Rel = "same" // the target's own hex (14.33)
	Long       Rel = "long" // the hex beyond the target on the line from the shooter's hex through the target's (14.32)
)

// AheadRels are the three forward directions a move_ahead/turn_ahead card offers.
var AheadRels = [3]Rel{AheadLeft, Ahead, AheadRight}

// BackRels are the three backward directions a move_back/turn_back card offers.
var BackRels = [3]Rel{BackLeft, Back, BackRight}

// ParseRel converts a plan string token to a Rel.
func ParseRel(s string) (Rel, bool) {
	switch r := Rel(s); r {
	case AheadLeft, Ahead, AheadRight, BackLeft, Back, BackRight:
		return r, true
	}
	return "", false
}

// IsAhead reports whether r is one of the three forward directions.
func (r Rel) IsAhead() bool { return r == AheadLeft || r == Ahead || r == AheadRight }

// IsBack reports whether r is one of the three backward directions.
func (r Rel) IsBack() bool { return r == BackLeft || r == Back || r == BackRight }

// Offset is the number of 60° clockwise steps from the facing to r
// (ahead_left=5 ahead=0 ahead_right=1 back_right=2 back=3 back_left=4).
// Left and right are the character's own sides while it keeps facing
// forward: facing N, back_right is SE and back_left is SW.
func (r Rel) Offset() int {
	switch r {
	case Ahead:
		return 0
	case AheadRight:
		return 1
	case BackRight:
		return 2
	case Back:
		return 3
	case BackLeft:
		return 4
	case AheadLeft:
		return 5
	}
	panic("hex: invalid relative direction " + string(r))
}

// Resolve turns a relative direction into an absolute one for a facing.
func Resolve(facing Dir, r Rel) Dir {
	return Dir((int(facing) + r.Offset()) % 6)
}

// AheadDirs returns the absolute directions of ahead_left, ahead and
// ahead_right for a facing; these are also the aim-zone spokes (rule 8.32).
func AheadDirs(f Dir) [3]Dir {
	return [3]Dir{Resolve(f, AheadLeft), Resolve(f, Ahead), Resolve(f, AheadRight)}
}

// BackDirs returns the absolute directions of back_left, back and back_right.
func BackDirs(f Dir) [3]Dir {
	return [3]Dir{Resolve(f, BackLeft), Resolve(f, Back), Resolve(f, BackRight)}
}

sight.gno

package hex

import "sort"

// Height is the height of a counter or of a terrain feature for the line
// of sight (rule 31.3, the HEIGHTS table), at ground elevation: a higher
// number is higher on the map. Hill and gully elevations come with the
// boards that have them.
type Height int

const (
	// HeightDown is a DOWN body counter.
	HeightDown Height = 3
	// HeightObstacle is an obstacle (36.3) or a window sill (33.5).
	HeightObstacle Height = 4
	// HeightUpright is an upright counter, a counter on an obstacle (30.32)
	// and a hex aimed at (31.3).
	HeightUpright Height = 5
	// HeightWall is a wall or a treetrunk, higher than anything (33.2, 38.3).
	HeightWall Height = 10
)

// Between returns the obstacle hexes lying between two hexes for the line
// of sight (rule 31): those every string from one hex's printed coordinate
// to the other's passes through (31.2, 36.3), as the generator computed
// them from the board image into the between table. The order of the two
// hexes does not matter. It is nil when nothing lies between them, when
// they are the same hex or when either is unknown, and it never names one
// of the two hexes (36.5).
func (b *Board) Between(from, to string) []string {
	var out []string
	for _, o := range b.obstaclesBetween(b.Index(from), b.Index(to)) {
		out = append(out, b.ids[o])
	}
	return out
}

// obstaclesBetween is Between on indices: the obstacle hexes between ids[i] and
// ids[j], as indices in ids.
func (b *Board) obstaclesBetween(i, j int) []int {
	if i < 0 || j < 0 || i == j {
		return nil
	}
	if i > j {
		i, j = j, i
	}
	// The rows are sorted by (a, b, o): a pair's rows are contiguous.
	k := sort.Search(len(b.between), func(k int) bool {
		r := b.between[k]
		return r[0] > i || (r[0] == i && r[1] >= j)
	})
	var out []int
	for ; k < len(b.between) && b.between[k][0] == i && b.between[k][1] == j; k++ {
		out = append(out, b.obstacles[b.between[k][2]])
	}
	return out
}

// LineOfSight reports whether a character at from, of height hFrom, has a
// clear line of sight to a target at to, of height hTo (rule 31). Every
// obstacle lying between the two hexes is compared with the two heights
// (31.3): it blocks the sight when higher than both and not when lower
// than both (31.31); when it is between the two heights it blocks only if
// the lower end is adjacent to it (31.32; the plateau effect of 31.33
// needs hill or gully hexes, which no board has yet). The aim zone is not
// looked at here: 31.21 is InAimZone. blocker is the first obstacle
// blocking the sight, "" when it is clear or a hex is unknown (which
// blocks).
func (b *Board) LineOfSight(from, to string, hFrom, hTo Height) (clear bool, blocker string) {
	i, j := b.Index(from), b.Index(to)
	if i < 0 || j < 0 {
		return false, ""
	}
	for _, o := range b.obstaclesBetween(i, j) {
		if blocks(HeightObstacle, hFrom, hTo, b.adjacent(i, o), b.adjacent(j, o)) {
			return false, b.ids[o]
		}
	}
	return true, ""
}

// blocks applies rules 31.31 and 31.32 to a feature of height h lying
// between two counters of heights h1 and h2, each adjacent to it or not.
func blocks(h, h1, h2 Height, adj1, adj2 bool) bool {
	switch {
	case h > h1 && h > h2:
		return true // higher than both (31.31)
	case h < h1 && h < h2:
		return false // lower than both (31.31)
	case h1 < h:
		return adj1 // the lower counter blocks the sight when adjacent (31.32)
	case h2 < h:
		return adj2
	}
	return false
}

// adjacent reports whether ids[j] is a neighbour of ids[i].
func (b *Board) adjacent(i, j int) bool {
	for _, n := range b.nbr[i] {
		if n == j {
			return true
		}
	}
	return false
}