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.

192 lines
5.7 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. ----- [[ AXIAL/CUBE COORDINATE SYSTEM FOR AMULET/LUA]] -------------------------
  2. --[[ author@churchianity.ca
  3. -- INTRODUCTION
  4. this is a hexagonal grid library for amulet/lua.
  5. it uses axial coordinates or cube/hex coordinates when necessary.
  6. by amulet convention, hexes are either vec2(s, t) or vec3(s, t, z)
  7. but nearly always the former.
  8. in some rare cases, coordinates will be passed individually, usually
  9. because they are only passed internally and should never be adjusted
  10. directly.
  11. in amulet, vector arithmetic already works via: + - * /
  12. additional things such as equality, and distance are implemented here.
  13. +support for parallelogram, triangular, hexagonal and rectangular maps.
  14. +support for simple irregular hexagons (horizontal and vertical stretching).
  15. classes are used sparsely. maps implement a few constructors, for storing
  16. your maps elsewhere.
  17. -- RESOURCES USED TO DEVELOP THIS LIBRARY
  18. https://redblobgames.com/grid/hexagons - simply amazing.
  19. http://amulet.xyz/doc - amulet documentation
  20. TODO that place that had the inner circle/outer circle ratio??
  21. ]]
  22. ----- [[ GENERALLY USEFUL FUNCTIONS ]] -----------------------------------------
  23. -- just incase you don't already have a rounding function.
  24. local function round(n)
  25. return n % 1 >= 0.5 and math.ceil(n) or math.floor(n)
  26. end
  27. ---- [[ HEX CONSTANTS ]] -------------------------------------------------------
  28. -- all possible vector directions from a given hex by edge
  29. local HEX_DIRECTIONS = {vec2( 1 , 0),
  30. vec2( 1 , -1),
  31. vec2( 0 , -1),
  32. vec2(-1 , 0),
  33. vec2(-1 , 1),
  34. vec2( 0 , 1)}
  35. -- HEX UTILITY FUNCTIONS -------------------------------------------------------
  36. function hex_equals(a, b)
  37. return a.s == b.s and a.t == b.t
  38. end
  39. local function hex_round(s, t)
  40. rs = round(s)
  41. rt = round(t)
  42. rz = round(-s - t)
  43. sdelta = math.abs(rs - s)
  44. tdelta = math.abs(rt - t)
  45. zdelta = math.abs(rz - (-s - t))
  46. if sdelta > tdelta and sdelta > zdelta then
  47. rs = -rt - rz
  48. elseif tdelta > zdelta then
  49. rt = -rs - rz
  50. else
  51. rz = -rs - rt
  52. end
  53. return vec2(rs, rt)
  54. end
  55. ----- [[ LAYOUT, ORIENTATION & COORDINATE CONVERSION ]] -----------------------
  56. -- forward & inverse matrices used for the flat orientation.
  57. local FLAT = {3.0/2.0, 0.0, 3.0^0.5/2.0, 3.0^0.5,
  58. 2.0/3.0, 0.0, -1.0/3.0 , 3.0^0.5/3.0}
  59. -- forward & inverse matrices used for the pointy orientation.
  60. local POINTY = {3.0^0.5, 3.0^0.5/2.0, 0.0, 3.0/2.0,
  61. 3.0^0.5/3.0, -1.0/3.0, 0.0, 2.0/3.0}
  62. -- layout.
  63. function layout_init(origin, size, orientation)
  64. return {origin = origin or vec2(0),
  65. size = size or vec2(11),
  66. orientation = orientation or FLAT}
  67. end
  68. -- hex to screen
  69. function hex_to_pixel(hex, layout)
  70. M = layout.orientation
  71. x = (M[1] * hex.s + M[2] * hex.t) * layout.size.x
  72. y = (M[3] * hex.s + M[4] * hex.t) * layout.size.y
  73. return vec2(x + layout.origin.x, y + layout.origin.y)
  74. end
  75. -- screen to hex
  76. function pixel_to_hex(pix, layout)
  77. M = layout.orientation
  78. pix = (pix - layout.origin) / layout.size
  79. s = M[5] * pix.x + M[6] * pix.y
  80. t = M[7] * pix.x + M[8] * pix.y
  81. return hex_round(s, t)
  82. end
  83. ----- [[ MAP STORAGE & RETRIEVAL ]] --------------------------------------------
  84. --[[ _init functions return a table of tables;
  85. a map of points in a chosen shape and specified layout.
  86. the shape, as well as the layout used is stored in a metatable
  87. for reuse.
  88. ]]
  89. -- returns parallelogram-shaped map.
  90. function grammap_init(layout, width, height)
  91. map = {}
  92. setmetatable(map, {__index={layout=layout,
  93. width=width,
  94. height=height,
  95. shape="parallelogram"}})
  96. for s = 0, width do
  97. for t = 0, height do
  98. table.insert(map, hex_to_pixel(vec2(s, t), layout))
  99. end
  100. end
  101. return map
  102. end
  103. -- returns triangular map.
  104. function trimap_init(layout, size)
  105. map = {}
  106. setmetatable(map, {__index={layout=layout,
  107. size=size,
  108. shape="triangular"}})
  109. for s = 0, size do
  110. for t = size - s, size do
  111. table.insert(map, hex_to_pixel(vec2(s, t), layout))
  112. end
  113. end
  114. return map
  115. end
  116. -- returns hexagonal map. length of map is radius * 2 + 1
  117. function hexmap_init(layout, radius)
  118. map = {}
  119. setmetatable(map, {__index={layout=layout,
  120. radius=radius,
  121. shape="hexagonal"}})
  122. for s = -radius, radius do
  123. t1 = math.max(-radius, -s - radius)
  124. t2 = math.min(radius, -s + radius)
  125. for t = t1, t2 do
  126. table.insert(map, hex_to_pixel(vec2(s, t), layout))
  127. end
  128. end
  129. return map
  130. end
  131. -- returns rectangular map.
  132. function rectmap_init(layout, width, height)
  133. map = {}
  134. mt = {__index={layout=layout, width=width, height=height,
  135. -- get hex in map from pixel coordinate
  136. retrieve=function(pix)
  137. return pixel_to_hex(pix, layout)
  138. end,
  139. -- store pixel in map from hex coordinate
  140. store=function(hex)
  141. map[hex]=hex_to_pixel(hex - vec2(0, math.floor(hex.s/2)), layout)
  142. end
  143. }}
  144. setmetatable(map, mt)
  145. for s = 0, width do
  146. for t = 0, height do
  147. map.store(vec2(s, t))
  148. end
  149. end
  150. return map
  151. end