-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcubic.py
220 lines (185 loc) · 6.76 KB
/
cubic.py
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
"""Cubic coordinate system library"""
from collections import namedtuple
from dataclasses import dataclass
from math import sin, cos, sqrt, pi#, floor, ceil
# def round(value):
# x = floor(value)
# if (value - x) < .50:
# return x
# else:
# return ceil(value)
class Cube():
"""Cubic coordinate class"""
def __init__(self, q, r, s):
self.q = q
self.r = r
self.s = s
assert (round(q + r + s) == 0), "q + r + s must be 0"
def __eq__(self, other):
t = isinstance(other, Cube)
if not t:
return False
q = self.q == other.q
r = self.r == other.r
s = self.s == other.s
return q and r and s
def __str__(self):
return str((self.q, self.r, self.s))
def __hash__(self):
return hash((self.q, self.r))
def __add__(self, other):
q = self.q + other.q
r = self.r + other.r
s = self.s + other.s
return Cube(q, r, s)
def __sub__(self, other):
q = self.q - other.q
r = self.r - other.r
s = self.s - other.s
return Cube(q, r, s)
def cube_length(cube):
return int((abs(cube.q) + abs(cube.r) + abs(cube.s)) / 2)
def cube_distance(cube_a, cube_b):
return cube_length(cube_a - cube_b)
def cube_direction(direction):
cube_directions = (
Cube(+1, -1, 0), Cube(+1, 0, -1), Cube(0, +1, -1),
Cube(-1, +1, 0), Cube(-1, 0, +1), Cube(0, -1, +1),)
assert 0 <= direction < 6
return cube_directions[direction]
def get_neighbour(cube, direction):
return cube + cube_direction(direction) #try mul
def get_nearest_neighbours(cube):
neighbours = []
for i in range(6):
neighbours.append(get_neighbour(cube, i))
return neighbours
def get_all_neighbours(cube, order):
neighbours = set((cube,))
for _ in range(order):
for neighbour in neighbours.copy():
i = set(get_nearest_neighbours(neighbour))
neighbours.update(i)
return neighbours.difference(set((cube,)))
def cube_lerp(a, b, t):
return Cube(int(round(a.q * (1.0 - t) + b.q * t)),
int(round(a.r * (1.0 - t) + b.r * t)),
int(round(a.s * (1.0 - t) + b.s * t)))
def cube_round(h):
qi = int(round(h.q))
ri = int(round(h.r))
si = int(round(h.s))
q_diff = abs(qi - h.q)
r_diff = abs(ri - h.r)
s_diff = abs(si - h.s)
if q_diff > r_diff and q_diff > s_diff:
qi = -ri - si
else:
if r_diff > s_diff:
ri = -qi - si
else:
si = -qi - ri
return Cube(qi, ri, si)
ntPoint = namedtuple("Point", ["x", "y"])
Orientation = namedtuple('Orientation',
["f0", "f1", "f2", "f3", "b0", "b1", "b2", "b3", "start_angle"])
ntLayout = namedtuple("Layout", ["orientation", "size", "origin"])
@dataclass
class Point:
x: int
y: int
@dataclass
class Layout:
orientation: Orientation
size: Point
origin: Point
layout_pointy = Orientation(sqrt(3.0), sqrt(3.0) / 2.0, 0.0, 3.0 / 2.0,
sqrt(3.0) / 3.0, -1.0 / 3.0, 0.0, 2.0 / 3.0, 0.5)
layout_flat = Orientation(3.0 / 2.0, 0.0, sqrt(3.0) / 2.0, sqrt(3.0),
2.0 / 3.0, 0.0, -1.0 / 3.0, sqrt(3.0) / 3.0, 0.0)
def cube_to_pixel(layout, h):
M = layout.orientation
size = layout.size
origin = layout.origin
x = (M.f0 * h.q + M.f1 * h.r) * size.x
y = (M.f2 * h.q + M.f3 * h.r) * size.y
return Point(x + origin.x, y + origin.y)
def pixel_to_cube(layout, p):
M = layout.orientation
size = layout.size
origin = layout.origin
pt = Point((p.x - origin.x) / size.x, (p.y - origin.y) / size.y)
q = M.b0 * pt.x + M.b1 * pt.y
r = M.b2 * pt.x + M.b3 * pt.y
return Cube(q, r, -q - r)
def cube_corner_offset(layout, corner):
M = layout.orientation
size = layout.size
angle = 2.0 * pi * (M.start_angle - corner) / 6.0
return Point(size.x * cos(angle), size.y * sin(angle))
def polygon_corners(layout, cube):
corners = []
center = cube_to_pixel(layout, cube)
for i in range(0, 6):
offset = cube_corner_offset(layout, i)
corners.append(Point(center.x + offset.x, center.y + offset.y))
return corners
# HE-specific functions
def is_blocked(game_world, cube):
tile = game_world.get(cube)
if not tile:
return True
return bool(tile.army or tile.locality)
def get_reachable_cubes(game_world, start_cube, movement_range):
visited = set((start_cube,)) # set of cubes
#visited = set() # set of cubes
fringes = [] # array of arrays of cubes
fringes.append([start_cube])
k = 1
while k <= movement_range: #1 < k <= movement_range
fringes.append([])
for cube in fringes[k-1]:
direction = 0
while direction < 6: #0 <= direction < 6
neighbour = get_neighbour(cube, direction)
if neighbour not in visited:
# This way we also add obstacles themselves, if they exist
if game_world.get(neighbour):
visited.add(neighbour)
if not is_blocked(game_world, neighbour):
fringes[k].append(neighbour)
direction += 1
k += 1
visited.remove(start_cube)
return visited
# Tests
def complain(name):
print("FAIL {0}".format(name))
def equal_cube(name, a, b):
if not (a.q == b.q and a.s == b.s and a.r == b.r):
complain(name)
def test_cube_round():
a = Cube(0.0, 0.0, 0.0)
b = Cube(1.0, -1.0, 0.0)
c = Cube(0.0, -1.0, 1.0)
equal_cube("cube_round 1", Cube(5, -10, 5), cube_round(cube_lerp(Cube(0.0, 0.0, 0.0), Cube(10.0, -20.0, 10.0), 0.5)))
equal_cube("cube_round 2", cube_round(a), cube_round(cube_lerp(a, b, 0.499)))
equal_cube("cube_round 3", cube_round(b), cube_round(cube_lerp(a, b, 0.501)))
equal_cube("cube_round 4", cube_round(a), cube_round(Cube(a.q * 0.4 + b.q * 0.3 + c.q * 0.3, a.r * 0.4 + b.r * 0.3 + c.r * 0.3, a.s * 0.4 + b.s * 0.3 + c.s * 0.3)))
equal_cube("cube_round 5", cube_round(c), cube_round(Cube(a.q * 0.3 + b.q * 0.3 + c.q * 0.4, a.r * 0.3 + b.r * 0.3 + c.r * 0.4, a.s * 0.3 + b.s * 0.3 + c.s * 0.4)))
def test_layout():
h = Cube(3, 4, -7)
flat = Layout(layout_flat, Point(10.0, 15.0), Point(35.0, 71.0))
equal_cube("layout", h, cube_round(pixel_to_cube(flat, cube_to_pixel(flat, h))))
pointy = Layout(layout_pointy, Point(10.0, 15.0), Point(35.0, 71.0))
equal_cube("layout", h, cube_round(pixel_to_cube(pointy, cube_to_pixel(pointy, h))))
def test_get_all_neighbours():
a = Cube(0, 0, 0)
b = get_all_neighbours(a, 2)
print(b)
print(a in b)
print(Cube(-1, 1, 0) in b)
#test_cube_round()
#test_layout()
#test_get_all_neighbours()
# print(cube_round(pixel_to_cube(Layout(layout_flat, Point(20,20), Point(0,0)), Point(200, 200))))