hexyz is tower defense game, and a lua library for dealing with hexagonal grids
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

557 lines
17 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
6 years ago
6 years ago
4 years ago
6 years ago
4 years ago
6 years ago
6 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
6 years ago
4 years ago
4 years ago
4 years ago
6 years ago
4 years ago
4 years ago
6 years ago
4 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
6 years ago
4 years ago
4 years ago
4 years ago
4 years ago
6 years ago
6 years ago
4 years ago
4 years ago
4 years ago
4 years ago
6 years ago
6 years ago
5 years ago
6 years ago
6 years ago
6 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
6 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
6 years ago
  1. if not math.round then
  2. math.round = function(n) return math.floor(n + 0.5) end
  3. else
  4. log("clobbering a math.round function.")
  5. end
  6. -- wherever 'orientation' appears as an argument, use one of these two, or set a default just below
  7. ORIENTATION = {
  8. -- Forward & Inverse Matrices used for the Flat Orientation
  9. FLAT = {
  10. M = mat2(3.0/2.0, 0.0, 3.0^0.5/2.0, 3.0^0.5 ),
  11. W = mat2(2.0/3.0, 0.0, -1.0/3.0 , 3.0^0.5/3.0),
  12. angle = 0.0
  13. },
  14. -- Forward & Inverse Matrices used for the Pointy Orientation
  15. POINTY = {
  16. M = mat2(3.0^0.5, 3.0^0.5/2.0, 0.0, 3.0/2.0),
  17. W = mat2(3.0^0.5/3.0, -1.0/3.0, 0.0, 2.0/3.0),
  18. angle = 0.5
  19. }
  20. }
  21. -- whenver |orientation| appears as an argument, if it isn't provided, this is used instead.
  22. local DEFAULT_ORIENTATION = ORIENTATION.FLAT
  23. -- whenever |size| for a hexagon appears as an argument, if it isn't provided, use this
  24. -- 'size' here is distance from the centerpoint to any vertex in pixel
  25. local DEFAULT_HEX_SIZE = vec2(20)
  26. -- actual width (longest contained horizontal line) of the hexagon
  27. function hex_width(size, orientation)
  28. local orientation = orientation or DEFAULT_ORIENTATION
  29. if orientation == ORIENTATION.FLAT then
  30. return size * 2
  31. elseif orientation == ORIENTATION.POINTY then
  32. return math.sqrt(3) * size
  33. end
  34. end
  35. -- actual height (tallest contained vertical line) of the hexagon
  36. function hex_height(size, orientation)
  37. local orientation = orientation or DEFAULT_ORIENTATION
  38. if orientation == ORIENTATION.FLAT then
  39. return math.sqrt(3) * size
  40. elseif orientation == ORIENTATION.POINTY then
  41. return size * 2
  42. end
  43. end
  44. -- returns actual width and height of a hexagon given it's |size| which is the distance from the centerpoint to any vertex in pixels
  45. function hex_dimensions(size, orientation)
  46. local orientation = orientation or DEFAULT_ORIENTATION
  47. return vec2(hex_width(size, orientation), hex_height(size, orientation))
  48. end
  49. -- distance between two horizontally adjacent hexagon centerpoints
  50. function hex_horizontal_spacing(size, orientation)
  51. local orientation = orientation or DEFAULT_ORIENTATION
  52. if orientation == ORIENTATION.FLAT then
  53. return hex_width(size, orientation) * 3/4
  54. elseif orientation == ORIENTATION.POINTY then
  55. return hex_height(size, orientation)
  56. end
  57. end
  58. -- distance between two vertically adjacent hexagon centerpoints
  59. function hex_vertical_spacing(size, orientation)
  60. local orientation = orientation or DEFAULT_ORIENTATION
  61. if orientation == ORIENTATION.FLAT then
  62. return hex_height(size, orientation)
  63. elseif orientation == ORIENTATION.POINTY then
  64. return hex_width(size, orientation) * 3/4
  65. end
  66. end
  67. -- returns the distance between adjacent hexagon centers in a grid
  68. function hex_spacing(size, orientation)
  69. local orientation = orientation or DEFAULT_ORIENTATION
  70. return vec2(hex_horizontal_spacing(size, orientation), hex_vertical_spacing(size, orientation))
  71. end
  72. -- All Non-Diagonal Vector Directions from a Given Hex by Edge
  73. HEX_DIRECTIONS = { vec2( 1 , -1), vec2( 1 , 0), vec2(0 , 1),
  74. vec2(-1 , 1), vec2(-1 , 0), vec2(0 , -1) }
  75. -- Return Hex Vector Direction via Integer Index |direction|
  76. function hex_direction(direction)
  77. return HEX_DIRECTIONS[(direction % 6) % 6 + 1]
  78. end
  79. -- Return Hexagon Adjacent to |hex| in Integer Index |direction|
  80. function hex_neighbour(hex, direction)
  81. return hex + HEX_DIRECTIONS[(direction % 6) % 6 + 1]
  82. end
  83. -- Collect All 6 Neighbours in a Table
  84. function hex_neighbours(hex)
  85. local neighbours = {}
  86. for i = 1, 6 do
  87. table.insert(neighbours, hex_neighbour(hex, i))
  88. end
  89. return neighbours
  90. end
  91. -- Returns a vec2 Which is the Nearest |x, y| to Float Trio |x, y, z|
  92. -- assumes you have a working math.round function (should be guarded at top of this file)
  93. local function hex_round(x, y, z)
  94. local rx = math.round(x)
  95. local ry = math.round(y)
  96. local rz = math.round(z) or math.round(-x - y)
  97. local xdelta = math.abs(rx - x)
  98. local ydelta = math.abs(ry - y)
  99. local zdelta = math.abs(rz - z or math.round(-x - y))
  100. if xdelta > ydelta and xdelta > zdelta then
  101. rx = -ry - rz
  102. elseif ydelta > zdelta then
  103. ry = -rx - rz
  104. else
  105. rz = -rx - ry
  106. end
  107. return vec2(rx, ry)
  108. end
  109. -- Hex to Screen -- Orientation Must be Either POINTY or FLAT
  110. function hex_to_pixel(hex, size, orientation)
  111. local M = orientation and orientation.M or DEFAULT_ORIENTATION.M
  112. local x = (M[1][1] * hex[1] + M[1][2] * hex[2]) * (size and size[1] or DEFAULT_HEX_SIZE[1])
  113. local y = (M[2][1] * hex[1] + M[2][2] * hex[2]) * (size and size[2] or DEFAULT_HEX_SIZE[2])
  114. return vec2(x, y)
  115. end
  116. -- Screen to Hex -- Orientation Must be Either POINTY or FLAT
  117. function pixel_to_hex(pix, size, orientation)
  118. local W = orientation and orientation.W or DEFAULT_ORIENTATION.W
  119. local pix = pix / (size or vec2(DEFAULT_HEX_SIZE))
  120. local x = W[1][1] * pix[1] + W[1][2] * pix[2]
  121. local y = W[2][1] * pix[1] + W[2][2] * pix[2]
  122. return hex_round(x, y, -x - y)
  123. end
  124. -- TODO test, learn am.draw
  125. function hex_corner_offset(corner, size, orientation)
  126. local orientation = orientation or DEFAULT_ORIENTATION
  127. local angle = 2.0 * math.pi * orientation.angle + corner / 6
  128. return vec2(size[1] * math.cos(angle), size[2] * math.sin(angle))
  129. end
  130. -- TODO test this thing
  131. function hex_corners(hex, size, orientation)
  132. local orientation = orientation or DEFAULT_ORIENTATION
  133. local corners = {}
  134. local center = hex_to_pixel(hex, size, orientation)
  135. for i = 0, 5 do
  136. local offset = hex_corner_offset(i, size, orientation)
  137. table.insert(corners, center + offset)
  138. end
  139. return corners
  140. end
  141. -- @TODO test
  142. function hex_to_oddr(hex)
  143. local z = -hex.x - hex.y
  144. return vec2(hex.x + (z - (z % 2)) / 2)
  145. end
  146. -- @TODO test
  147. function oddr_to_hex(oddr)
  148. return vec2(hex.x - (hex.y - (hex.y % 2)) / 2, -hex.x - hex.y)
  149. end
  150. -- @TODO test
  151. function hex_to_evenr(hex)
  152. local z = -hex.x - hex.y
  153. return vec2(hex.x + (z + (z % 2)) / 2, z)
  154. end
  155. -- @TODO test
  156. function evenr_to_hex(evenr)
  157. return vec2(hex.x - (hex.y + (hex.y % 2)) / 2, -hex.x - hex.y)
  158. end
  159. -- @TODO test
  160. function hex_to_oddq(hex)
  161. return vec2(hex.x, -hex.x - hex.y + (hex.x - (hex.x % 2)) / 2)
  162. end
  163. -- @TODO test
  164. function oddq_to_hex(oddq)
  165. return vec2(hex.x, -hex.x - (hex.y - (hex.x - (hex.y % 2)) / 2))
  166. end
  167. function hex_to_evenq(hex)
  168. return vec2(hex.x, (-hex.x - hex.y) + (hex.x + (hex.x % 2)) / 2)
  169. end
  170. function evenq_to_hex(evenq)
  171. return vec2(evenq.x, -evenq.x - (evenq.y - (evenq.x + (evenq.x % 2)) / 2))
  172. end
  173. --============================================================================
  174. -- MAPS & STORAGE
  175. -- Returns Ordered Ring-Shaped Map of |radius| from |center|
  176. function ring_map(center, radius)
  177. local map = {}
  178. local walk = center + HEX_DIRECTIONS[6] * radius
  179. for i = 1, 6 do
  180. for j = 1, radius do
  181. table.insert(map, walk)
  182. walk = hex_neighbour(walk, i)
  183. end
  184. end
  185. return setmetatable(map, {__index={center=center, radius=radius}})
  186. end
  187. -- Returns Ordered Spiral Hexagonal Map of |radius| Rings from |center|
  188. function spiral_map(center, radius)
  189. local map = {center}
  190. for i = 1, radius do
  191. table.append(map, ring_map(center, i))
  192. end
  193. return setmetatable(map, {__index={center=center, radius=radius}})
  194. end
  195. local function map_get(t, x, y)
  196. return t[x] and t[x][y]
  197. end
  198. local function map_set(t, x, y, v)
  199. if t[x] then
  200. t[x][y] = v
  201. else
  202. t[x] = {}
  203. t[x][y] = v
  204. end
  205. return t
  206. end
  207. local function map_traverse(t, callback)
  208. for i,_ in pairs(t) do
  209. for _,entry in pairs(t[i]) do
  210. callback(entry)
  211. end
  212. end
  213. end
  214. -- @NOTE probably shouldn't use this...
  215. local function map_partial_set(t, x, y, k, v)
  216. local entry = map_get(t, x, y)
  217. if not entry then
  218. map_set(t, x, y, { k = v })
  219. else
  220. entry.k = v
  221. end
  222. return t
  223. end
  224. -- Returns Unordered Parallelogram-Shaped Map of |width| and |height| with Simplex Noise
  225. function parallelogram_map(width, height, seed)
  226. local seed = seed or math.random(width * height)
  227. local map = {}
  228. for i = 0, width do
  229. map[i] = {}
  230. for j = 0, height do
  231. -- Calculate Noise
  232. local idelta = i / width
  233. local jdelta = j / height
  234. local noise = 0
  235. for oct = 1, 6 do
  236. local f = 1/4^oct
  237. local l = 2^oct
  238. local pos = vec2(idelta + seed * width, jdelta + seed * height)
  239. noise = noise + f * math.simplex(pos * l)
  240. end
  241. map[i][j] = noise
  242. end
  243. end
  244. return setmetatable(map, { __index = {
  245. width = width,
  246. height = height,
  247. seed = seed,
  248. get = function(x, y) return map_get(map, x, y) end,
  249. set = function(x, y, v) return map_set(map, x, y, v) end,
  250. partial = function(x, y, k, v) return map_partial_set(map, x, y, k, v) end,
  251. traverse = function(callback) return map_traverse(map, callback) end,
  252. neighbours = function(hex)
  253. return table.filter(hex_neighbours(hex), function(_hex)
  254. return map.get(_hex.x, _hex.y)
  255. end)
  256. end
  257. }})
  258. end
  259. -- Returns Unordered Triangular (Equilateral) Map of |size| with Simplex Noise
  260. function triangular_map(size, seed)
  261. local seed = seed or math.random(size * math.cos(size) / 2)
  262. local map = {}
  263. for i = 0, size do
  264. map[i] = {}
  265. for j = size - i, size do
  266. -- Generate Noise
  267. local idelta = i / size
  268. local jdelta = j / size
  269. local noise = 0
  270. for oct = 1, 6 do
  271. local f = 1/3^oct
  272. local l = 2^oct
  273. local pos = vec2(idelta + seed * size, jdelta + seed * size)
  274. noise = noise + f * math.simplex(pos * l)
  275. end
  276. map[i][j] = noise
  277. end
  278. end
  279. return setmetatable(map, { __index = {
  280. size = size,
  281. seed = seed,
  282. get = function(x, y) return map_get(map, x, y) end,
  283. set = function(x, y, v) return map_set(map, x, y, v) end,
  284. partial = function(x, y, k, v) return map_partial_set(map, x, y, k, v) end,
  285. traverse = function(callback) return map_traverse(map, callback) end,
  286. neighbours = function(hex)
  287. return table.filter(hex_neighbours(hex), function(_hex)
  288. return map.get(_hex.x, _hex.y)
  289. end)
  290. end
  291. }})
  292. end
  293. -- Returns Unordered Hexagonal Map of |radius| with Simplex Noise
  294. function hexagonal_map(radius, seed)
  295. local seed = seed or math.random(radius * 2 * math.pi)
  296. local map = {}
  297. for i = -radius, radius do
  298. map[i] = {}
  299. local j1 = math.max(-radius, -i - radius)
  300. local j2 = math.min(radius, -i + radius)
  301. for j = j1, j2 do
  302. -- Calculate Noise
  303. local idelta = i / radius
  304. local jdelta = j / radius
  305. local noise = 0
  306. for oct = 1, 6 do
  307. local f = 2/3^oct
  308. local l = 2^oct
  309. local pos = vec2(idelta + seed * radius, jdelta + seed * radius)
  310. noise = noise + f * math.simplex(pos * l)
  311. end
  312. map[i][j] = noise
  313. end
  314. end
  315. return setmetatable(map, { __index = {
  316. radius = radius,
  317. seed = seed,
  318. get = function(x, y) return map_get(map, x, y) end,
  319. set = function(x, y, v) return map_set(map, x, y, v) end,
  320. partial = function(x, y, k, v) return map_partial_set(map, x, y, k, v) end,
  321. traverse = function(callback) return map_traverse(map, callback) end,
  322. neighbours = function(hex)
  323. return table.filter(hex_neighbours(hex), function(_hex)
  324. return map.get(_hex.x, _hex.y)
  325. end)
  326. end
  327. }})
  328. end
  329. -- Returns Unordered Rectangular Map of |width| and |height| with Simplex Noise
  330. function rectangular_map(width, height, seed)
  331. local seed = seed or math.random(width * height)
  332. local map = {}
  333. for i = 0, width - 1 do
  334. map[i] = {}
  335. for j = 0, height - 1 do
  336. -- Begin to Calculate Noise
  337. local idelta = i / width
  338. local jdelta = j / height
  339. local noise = 0
  340. for oct = 1, 6 do
  341. local f = 2/3^oct
  342. local l = 2^oct
  343. local pos = vec2(idelta + seed * width, jdelta + seed * height)
  344. noise = noise + f * math.simplex(pos * l)
  345. end
  346. j = j - math.floor(i/2) -- this is what makes it rectangular
  347. map[i][j] = noise
  348. end
  349. end
  350. return setmetatable(map, { __index = {
  351. width = width,
  352. height = height,
  353. seed = seed,
  354. get = function(x, y) return map_get(map, x, y) end,
  355. set = function(x, y, v) return map_set(map, x, y, v) end,
  356. partial = function(x, y, k, v) return map_partial_set(map, x, y, k, v) end,
  357. traverse = function(callback) return map_traverse(map, callback) end,
  358. neighbours = function(hex)
  359. return table.filter(hex_neighbours(hex), function(_hex)
  360. return map.get(_hex.x, _hex.y)
  361. end)
  362. end
  363. }})
  364. end
  365. --============================================================================
  366. -- PATHFINDING
  367. function breadth_first(map, start)
  368. local frontier = {}
  369. frontier[1] = start
  370. local distance = {}
  371. distance[start.x] = {}
  372. distance[start.x][start.y] = 0
  373. while not (#frontier == 0) do
  374. local current = table.remove(frontier, 1)
  375. for _,neighbour in pairs(map.neighbours(current)) do
  376. local d = map_get(distance, neighbour.x, neighbour.y)
  377. if not d then
  378. table.insert(frontier, neighbour)
  379. local current_distance = map_get(distance, current.x, current.y)
  380. map_set(distance, neighbour.x, neighbour.y, current_distance + 1)
  381. end
  382. end
  383. end
  384. return distance
  385. end
  386. function dijkstra(map, start, goal, cost_f)
  387. local frontier = {}
  388. frontier[1] = { hex = start, priority = 0 }
  389. local came_from = {}
  390. came_from[start.x] = {}
  391. came_from[start.x][start.y] = false
  392. local cost_so_far = {}
  393. cost_so_far[start.x] = {}
  394. cost_so_far[start.x][start.y] = 0
  395. while not (#frontier == 0) do
  396. local current = table.remove(frontier, 1)
  397. if goal and current.hex == goal then
  398. break
  399. end
  400. for _,neighbour in pairs(map.neighbours(current.hex)) do
  401. local new_cost = map_get(cost_so_far, current.hex.x, current.hex.y) + cost_f(map, current.hex, neighbour)
  402. local neighbour_cost = map_get(cost_so_far, neighbour.x, neighbour.y)
  403. if not neighbour_cost or (new_cost < neighbour_cost) then
  404. map_set(cost_so_far, neighbour.x, neighbour.y, new_cost)
  405. local priority = new_cost + math.distance(start, neighbour)
  406. table.insert(frontier, { hex = neighbour, priority = priority })
  407. map_set(came_from, neighbour.x, neighbour.y, current)
  408. end
  409. end
  410. end
  411. return came_from
  412. end
  413. -- generic A* pathfinding
  414. --
  415. -- |heuristic| has the form:
  416. -- function(source, target) -- source and target are vec2's
  417. -- return some numeric value
  418. --
  419. -- |cost_f| has the form:
  420. -- function (from, to) -- from and to are vec2's
  421. -- return some numeric value
  422. --
  423. -- returns a map that has map[hex.x][hex.y] = { hex = vec2, priority = number },
  424. -- where the hex is the spot it thinks you should go to from the indexed hex, and priority is the cost of that decision,
  425. -- as well as 'made_it' a bool that tells you if we were successful in reaching |goal|
  426. function Astar(map, start, goal, heuristic, cost_f)
  427. local path = {}
  428. path[start.x] = {}
  429. path[start.x][start.y] = false
  430. local frontier = {}
  431. frontier[1] = { hex = start, priority = 0 }
  432. local path_so_far = {}
  433. path_so_far[start.x] = {}
  434. path_so_far[start.x][start.y] = 0
  435. local made_it = false
  436. while not (#frontier == 0) do
  437. local current = table.remove(frontier, 1)
  438. if current.hex == goal then
  439. made_it = true
  440. break
  441. end
  442. for _,next_ in pairs(map.neighbours(current.hex)) do
  443. local new_cost = map_get(path_so_far, current.hex.x, current.hex.y) + cost_f(map, current.hex, next_)
  444. local next_cost = map_get(path_so_far, next_.x, next_.y)
  445. if not next_cost or new_cost < next_cost then
  446. map_set(path_so_far, next_.x, next_.y, new_cost)
  447. local priority = new_cost + heuristic(goal, next_)
  448. table.insert(frontier, { hex = next_, priority = priority })
  449. map_set(path, next_.x, next_.y, current)
  450. end
  451. end
  452. end
  453. return path, made_it
  454. end