-
Notifications
You must be signed in to change notification settings - Fork 0
/
region.go
160 lines (143 loc) · 4.46 KB
/
region.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
/*
Copyright (C) 2014-2017 Wolfger Schramm <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package kiwotigo
type Region struct {
hexagons []*Hexagon
neighbors []*Region
}
func NewRegion() (region *Region) {
region = new(Region)
region.hexagons = make([]*Hexagon, 0)
region.neighbors = make([]*Region, 0)
return
}
func (region *Region) RegionSize() int {
return len(region.hexagons)
}
func (region *Region) AssignHexagon(hex *Hexagon) *Region {
if hex.Region != nil {
panic("Hexagon already has a Region, cannot reassign")
}
hex.Region = region
region.hexagons = append(region.hexagons, hex)
if hex.NeighborNorth != nil {
region.addNeighbor(hex.NeighborNorth.Region)
}
if hex.NeighborSouth != nil {
region.addNeighbor(hex.NeighborSouth.Region)
}
if hex.NeighborNorthEast != nil {
region.addNeighbor(hex.NeighborNorthEast.Region)
}
if hex.NeighborNorthWest != nil {
region.addNeighbor(hex.NeighborNorthWest.Region)
}
if hex.NeighborSouthEast != nil {
region.addNeighbor(hex.NeighborSouthEast.Region)
}
if hex.NeighborSouthWest != nil {
region.addNeighbor(hex.NeighborSouthWest.Region)
}
return region
}
func (region *Region) AssignHexagons(hexagons []*Hexagon) *Region {
for _, hex := range hexagons {
region.AssignHexagon(hex)
}
return region
}
func (region *Region) IsNeighbor(other *Region) bool {
for _, r := range region.neighbors {
if r == other {
return true
}
}
return false
}
func (region *Region) addNeighbor(neighbor *Region) *Region {
if neighbor != nil && region != neighbor && !region.IsNeighbor(neighbor) {
region.neighbors = append(region.neighbors, neighbor)
neighbor.neighbors = append(neighbor.neighbors, region)
}
return region
}
func isNotInside(hexagons []*Hexagon, hex *Hexagon) bool {
for _, h := range hexagons {
if hex == h {
return false
}
}
return true
}
func appendIfNotInside(hexagons []*Hexagon, hex *Hexagon) []*Hexagon {
if isNotInside(hexagons, hex) {
return append(hexagons, hex)
}
return hexagons
}
func (region *Region) RegionLessNeighborHexagons() (regionLess []*Hexagon) {
regionLess = make([]*Hexagon, 0)
for _, hex := range region.hexagons {
if hex.NeighborNorth != nil && hex.NeighborNorth.Region == nil {
regionLess = appendIfNotInside(regionLess, hex.NeighborNorth)
}
if hex.NeighborNorthEast != nil && hex.NeighborNorthEast.Region == nil {
regionLess = appendIfNotInside(regionLess, hex.NeighborNorthEast)
}
if hex.NeighborNorthWest != nil && hex.NeighborNorthWest.Region == nil {
regionLess = appendIfNotInside(regionLess, hex.NeighborNorthWest)
}
if hex.NeighborSouth != nil && hex.NeighborSouth.Region == nil {
regionLess = appendIfNotInside(regionLess, hex.NeighborSouth)
}
if hex.NeighborSouthEast != nil && hex.NeighborSouthEast.Region == nil {
regionLess = appendIfNotInside(regionLess, hex.NeighborSouthEast)
}
if hex.NeighborSouthWest != nil && hex.NeighborSouthWest.Region == nil {
regionLess = appendIfNotInside(regionLess, hex.NeighborSouthWest)
}
}
return
}
func (region *Region) isAtEdge(hex *Hexagon) bool {
return hex == nil || hex.Region == nil || hex.Region != region
}
func (region *Region) ShapeHexagons() (shape []*Hexagon) {
shape = make([]*Hexagon, 0)
for _, hex := range region.hexagons {
if region.isAtEdge(hex.NeighborNorth) || region.isAtEdge(hex.NeighborNorthEast) || region.isAtEdge(hex.NeighborSouthEast) || region.isAtEdge(hex.NeighborSouth) || region.isAtEdge(hex.NeighborSouthWest) || region.isAtEdge(hex.NeighborNorthWest) {
shape = append(shape, hex)
}
}
return
}
func (region *Region) SingleRandomShapeHexagon() *Hexagon {
// take a hexagon from region
hex := region.hexagons[0]
// find the furthest left hexagon
for {
left := hex.NeighborLeft
if left == nil {
break
}
hex = left
}
// then go right until a hexagon is from our region
for {
if hex.Region == region {
return hex
}
hex = hex.NeighborRight
}
}