This repository was archived by the owner on Jul 11, 2026. It is now read-only.
forked from darrellanderson/CrLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedBlobHex.ttslua
More file actions
281 lines (248 loc) · 11.8 KB
/
Copy pathRedBlobHex.ttslua
File metadata and controls
281 lines (248 loc) · 11.8 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
-------------------------------------------------------------------------------
--- Hex math functions
-- Adapted from Red Blob Games
-- @see https://www.redblobgames.com/grids/hexagons/implementation.html
-- The full Red Blob Games hex library contains many other functions.
-- @author Darrell
-------------------------------------------------------------------------------
local TAG = 'CrLua.RedBlobHex'
CrLua = CrLua or {} -- global, <include> wraps in a do .. end block
CrLua.RedBlobHex = assert(not CrLua.RedBlobHex) and {
_require = {}
}
--- Standard hex orientations.
CrLua.RedBlobHex.ORIENTATION = {
POINTY_TOP = 1,
FLAT_TOP = 2
}
-------------------------------------------------------------------------------
--- Hex grid orientation, used to translate between screen position and unit hex
-- (unit meaning it does not take into account hex size or screen origin).
-- Outside users can references CrLua.RedBlobHex.ORIENTATION.{POINT|FLAT}_TOP
-- values to get an orientation for standard hex grids.
-- @param f* : matrix to translate from hex to screen position.
-- @param b* : matrix to translate from screen position to hex.
-- @param start_angle : angle to the first corner (flat vs pointy top).
-- @return table : orientation data.
-------------------------------------------------------------------------------
function CrLua.RedBlobHex.orientation(f0, f1, f2, f3, b0, b1, b2, b3, start_angle)
return { f0 = f0, f1 = f1, f2 = f2, f3 = f3, b0 = b0, b1 = b1, b2 = b2, b3 = b3, start_angle = start_angle }
end
-------------------------------------------------------------------------------
--- Hex grid layout. Used to translate between real screen position and hex.
-- @param orientatation : one of the CrLua.RedBlobHex.ORIENTATION, or an orientation.
-- @param hexSize 2D point table : {x,y} keys to hex width and height dimensions.
-- @param origin 2D point table : {x,y} keys to world origin.
-- @return table : layout data.
-------------------------------------------------------------------------------
function CrLua.RedBlobHex.layout(orientation, hexSize, origin)
-- If orientation it an ORIENTATION entry, use a standard orientation.
if orientation == CrLua.RedBlobHex.ORIENTATION.POINTY_TOP then
orientation = CrLua.RedBlobHex.orientation(math.sqrt(3.0), math.sqrt(3.0) / 2.0, 0.0, 3.0 / 2.0, math.sqrt(3.0) / 3.0, -1.0 / 3.0, 0.0, 2.0 / 3.0, 0.5)
elseif orientation == CrLua.RedBlobHex.ORIENTATION.FLAT_TOP then
orientation = CrLua.RedBlobHex.orientation(3.0 / 2.0, 0.0, math.sqrt(3.0) / 2.0, math.sqrt(3.0), 2.0 / 3.0, 0.0, -1.0 / 3.0, math.sqrt(3.0) / 3.0, 0.0)
elseif type(orientation) == 'string' then
error('unknown orientation "' .. orientation .. '"')
end
assert(type(orientation) == 'table' and type(orientation.f0) == 'number')
assert(type(hexSize) == 'table' and type(hexSize.x) == 'number' and type(hexSize.y) == 'number')
assert(type(origin) == 'table' and type(origin.x) == 'number' and type(origin.y) == 'number')
return { orientation = orientation, size = hexSize, origin = origin }
end
-------------------------------------------------------------------------------
--- 2D point for world space.
-- @param x : number
-- @param y : number
-- @return point table : {x,y} keys.
-------------------------------------------------------------------------------
function CrLua.RedBlobHex.point(x, y)
return { x = x, y = y }
end
function CrLua.RedBlobHex._testPoint()
local point = CrLua.RedBlobHex.point(1, 2)
assert(point.x == 1 and point.y == 2)
end
-------------------------------------------------------------------------------
--- Hex coordinates (Axial).
-- @param q : number.
-- @param r : number.
-- @param s : number.
-- @return hex table : {q,r,s} keys.
-------------------------------------------------------------------------------
function CrLua.RedBlobHex.hex(q, r, s)
assert(not (math.floor (0.5 + q + r + s) ~= 0), 'q + r + s must be 0')
return { q = q, r = r, s = s }
end
function CrLua.RedBlobHex._testHex()
local hex = CrLua.RedBlobHex.hex(1, 2, -3)
assert(hex.q == 1, hex.r == 2 and hex.s == -3)
end
-------------------------------------------------------------------------------
--- Marshal hex to string.
-- @param hex table : {q,r,s} keys.
-- @return string : hex encoded as a string.
-------------------------------------------------------------------------------
function CrLua.RedBlobHex.toString(hex)
return '<' .. hex.q .. ',' .. hex.r .. ',' .. hex.s .. '>'
end
function CrLua.RedBlobHex._testToString()
local hex = CrLua.RedBlobHex.hex(1, 2, -3)
local str = CrLua.RedBlobHex.toString(hex)
assert(str == '<1,2,-3>')
end
-------------------------------------------------------------------------------
--- Marshal string to hex.
-- @param string : hex encoded as a string (via toString).
-- @return hex table : {q,r,s} keys.
-------------------------------------------------------------------------------
function CrLua.RedBlobHex.fromString(str)
local q, r, s = string.match(str, '<(%-?%d+),(%-?%d+),(%-?%d+)>')
return CrLua.RedBlobHex.hex(tonumber(q), tonumber(r), tonumber(s))
end
function CrLua.RedBlobHex._testFromString()
local str = '<1,2,-3>'
local hex = CrLua.RedBlobHex.fromString(str)
assert(hex.q == 1, hex.r == 2 and hex.s == -3)
end
-------------------------------------------------------------------------------
--- Get hex from a world 2D point.
-- Note this may be a fractional hex if point is not exactly center of hex,
-- use the round function for a "clean" hex.
-- @param layout : transform between world 2D and hex space.
-- @param point table : {x,y} keys.
-- @return hex : (not-rounded) hex.
-------------------------------------------------------------------------------
function CrLua.RedBlobHex.fromPoint(layout, position)
assert(layout.orientation and position.x)
local M = layout.orientation
local size = layout.size
local origin = layout.origin
local pt = CrLua.RedBlobHex.point((position.x - origin.x) / size.x, (position.y - origin.y) / size.y)
local q = M.b0 * pt.x + M.b1 * pt.y
local r = M.b2 * pt.x + M.b3 * pt.y
return CrLua.RedBlobHex.hex(q, r, -q - r)
end
function CrLua.RedBlobHex._testFromPoint()
local orientation = CrLua.RedBlobHex.ORIENTATION.FLAT_TOP
local hexSize = CrLua.RedBlobHex.point(1, 1)
local origin = CrLua.RedBlobHex.point(0, 0)
local layout = CrLua.RedBlobHex.layout(orientation, hexSize, origin)
-- Floating point precision can lead to "almost" the correct number.
local function almost(a, b)
return math.abs(a - b) < 0.001
end
local point = CrLua.RedBlobHex.point(0, 0)
local hex = CrLua.RedBlobHex.fromPoint(layout, point)
assert(almost(hex.q, 0) and almost(hex.r, 0), 'q=' .. hex.q .. ', r=' .. hex.r)
local point = CrLua.RedBlobHex.point(1.5, math.sqrt(3) / 2)
local hex = CrLua.RedBlobHex.fromPoint(layout, point)
assert(almost(hex.q, 1) and almost(hex.r, 0), 'q=' .. hex.q .. ', r=' .. hex.r)
end
-------------------------------------------------------------------------------
--- Get world 2D point from hex.
-- @param layout : transform between world 2D and hex space.
-- @param hex table : {q,r,s} keys.
-- @return point table : {x,y} keys.
-------------------------------------------------------------------------------
function CrLua.RedBlobHex.toPoint(layout, hex)
assert(layout.orientation and hex.q)
local M = layout.orientation
local size = layout.size
local origin = layout.origin
local x = (M.f0 * hex.q + M.f1 * hex.r) * size.x
local y = (M.f2 * hex.q + M.f3 * hex.r) * size.y
return CrLua.RedBlobHex.point(x + origin.x, y + origin.y)
end
function CrLua.RedBlobHex._testToPoint()
local orientation = CrLua.RedBlobHex.ORIENTATION.FLAT_TOP
local hexSize = CrLua.RedBlobHex.point(1, 1)
local origin = CrLua.RedBlobHex.point(0, 0)
local layout = CrLua.RedBlobHex.layout(orientation, hexSize, origin)
local hex = CrLua.RedBlobHex.hex(0, 0, 0)
local point = CrLua.RedBlobHex.toPoint(layout, hex)
assert(point.x == 0 and point.y == 0, 'x=' .. point.x .. ', y=' .. point.y)
local hex = CrLua.RedBlobHex.hex(1, 0, -1)
local point = CrLua.RedBlobHex.toPoint(layout, hex)
assert(point.x == 1.5 and point.y == math.sqrt(3) / 2, 'x=' .. point.x .. ', y=' .. point.y)
end
-------------------------------------------------------------------------------
--- Round to the nearest on-grid hex.
-- @param hex : hex, probably from a not-center-of-hex 2D point.
-- @return hex : nearest on-grid hex.
-------------------------------------------------------------------------------
function CrLua.RedBlobHex.round(hex)
local qi = math.floor(math.floor (0.5 + hex.q))
local ri = math.floor(math.floor (0.5 + hex.r))
local si = math.floor(math.floor (0.5 + hex.s))
local q_diff = math.abs(qi - hex.q)
local r_diff = math.abs(ri - hex.r)
local s_diff = math.abs(si - hex.s)
if q_diff > r_diff and q_diff > s_diff then
qi = -ri - si
else
if r_diff > s_diff then
ri = -qi - si
else
si = -qi - ri
end
end
return CrLua.RedBlobHex.hex(qi, ri, si)
end
function CrLua.RedBlobHex._testRound()
local hex = CrLua.RedBlobHex.hex(1.1, 2.1, -3.2)
local rounded = CrLua.RedBlobHex.round(hex)
assert(rounded.q == 1 and rounded.r == 2 and rounded.s == -3, 'q=..' .. rounded.q .. ', r=' .. rounded.r .. ', s=' .. rounded.s)
local hex = CrLua.RedBlobHex.hex(0.9, 1.9, -2.8)
local rounded = CrLua.RedBlobHex.round(hex)
assert(rounded.q == 1 and rounded.r == 2 and rounded.s == -3, 'q=..' .. rounded.q .. ', r=' .. rounded.r .. ', s=' .. rounded.s)
end
-------------------------------------------------------------------------------
--- Get the world 2D point locations for hex corners.
-- @param layout : transform between world 2D and hex space.
-- @param hex table : {q,r,s} keys.
-- @return table : list of world-space corner 2D points.
-------------------------------------------------------------------------------
function CrLua.RedBlobHex.corners(layout, hex)
local M = layout.orientation
local size = layout.size
local function hexCornerOffset(corner)
local angle = 2.0 * math.pi * (M.start_angle - corner) / 6.0
return CrLua.RedBlobHex.point(size.x * math.cos(angle), size.y * math.sin(angle))
end
local corners = {}
local center = CrLua.RedBlobHex.toPoint(layout, hex)
for i = 0, 5 do
local offset = hexCornerOffset(i)
table.insert(corners, CrLua.RedBlobHex.point(center.x + offset.x, center.y + offset.y))
end
return corners
end
function CrLua.RedBlobHex._testCorners()
local orientation = CrLua.RedBlobHex.ORIENTATION.FLAT_TOP
local hexSize = CrLua.RedBlobHex.point(1, 1)
local origin = CrLua.RedBlobHex.point(0, 0)
local layout = CrLua.RedBlobHex.layout(orientation, hexSize, origin)
local hex = CrLua.RedBlobHex.hex(0, 0, 0)
local corners = CrLua.RedBlobHex.corners(layout, hex)
assert(#corners == 6)
end
-------------------------------------------------------------------------------
--- Get hex neighbors.
-- @param hex table : {q,r,s} keys.
-- @return table : list of neighbor hexes.
-------------------------------------------------------------------------------
function CrLua.RedBlobHex.neighbors(hex)
return {
CrLua.RedBlobHex.hex(hex.q + 1.0, hex.r + 0.0, hex.s - 1.0),
CrLua.RedBlobHex.hex(hex.q + 1.0, hex.r - 1.0, hex.s + 0.0),
CrLua.RedBlobHex.hex(hex.q + 0.0, hex.r - 1.0, hex.s + 1.0),
CrLua.RedBlobHex.hex(hex.q - 1.0, hex.r + 0.0, hex.s + 1.0),
CrLua.RedBlobHex.hex(hex.q - 1.0, hex.r + 1.0, hex.s + 0.0),
CrLua.RedBlobHex.hex(hex.q + 0.0, hex.r + 1.0, hex.s - 1.0)
}
end
function CrLua.RedBlobHex._testNeighbors()
local hex = CrLua.RedBlobHex.hex(0, 0, 0)
local neighbors = CrLua.RedBlobHex.neighbors(hex)
assert(#neighbors == 6)
end